Skip to content

Define unique IDs for all check configurations #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
758 changes: 379 additions & 379 deletions check/checkconfigurations/checkconfigurations.go

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions check/checkconfigurations/checkconfigurations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
"github.com/arduino/arduino-lint/check/checkconfigurations"
"github.com/arduino/arduino-lint/check/checklevel"
"github.com/arduino/arduino-lint/configuration/checkmode"
"github.com/arduino/arduino-lint/project/projecttype"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConfigurationResolution(t *testing.T) {
Expand Down Expand Up @@ -65,3 +67,31 @@ func checkModeConflict(configurations ...[]checkmode.Type) (bool, checkmode.Type
}
return false, checkmode.Default
}

func TestIncorrectCheckIDPrefix(t *testing.T) {
for checkIndex, checkConfiguration := range checkconfigurations.Configurations() {
var IDPrefix byte
switch checkConfiguration.ProjectType {
case projecttype.Sketch:
IDPrefix = 'S'
case projecttype.Library:
IDPrefix = 'L'
case projecttype.Platform:
IDPrefix = 'P'
case projecttype.PackageIndex:
IDPrefix = 'I'
default:
panic(fmt.Errorf("No prefix configured for project type %s", checkConfiguration.ProjectType))
}
require.NotEmptyf(t, checkConfiguration.ID, "No check ID defined for check configuration #%v", checkIndex)
assert.Equalf(t, IDPrefix, checkConfiguration.ID[0], "Check ID %s has incorrect prefix for project type %s.", checkConfiguration.ID, checkConfiguration.ProjectType)
}
}

func TestDuplicateCheckID(t *testing.T) {
checkIDMap := make(map[string]bool)
for checkIndex, checkConfiguration := range checkconfigurations.Configurations() {
checkIDMap[checkConfiguration.ID] = true
require.Equalf(t, checkIndex+1, len(checkIDMap), "ID %s of check #%v is a duplicate", checkConfiguration.ID, checkIndex)
}
}
74 changes: 37 additions & 37 deletions check/checkfunctions/checkfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,6 @@ import (
// The `output` result is the contextual information that will be inserted into the check's message template.
type Type func() (result checkresult.Type, output string)

// validProjectPathBaseName checks whether the provided library folder or sketch filename contains prohibited characters.
func validProjectPathBaseName(name string) bool {
baseNameRegexp := regexp.MustCompile("^[a-zA-Z0-9][a-zA-Z0-9_.-]*$")
return baseNameRegexp.MatchString(name)
}

func containsMisspelledPathBaseName(pathList paths.PathList, correctBaseName string, misspellingQuery string) (*paths.Path, bool) {
misspellingRegexp := regexp.MustCompile(misspellingQuery)
for _, path := range pathList {
if path.Base() == correctBaseName {
return nil, false
}

if misspellingRegexp.MatchString(path.Base()) {
return path, true
}
}

return nil, false
}

func containsIncorrectPathBaseCase(pathList paths.PathList, correctBaseName string) (*paths.Path, bool) {
for _, path := range pathList {
if path.Base() == correctBaseName {
// There was a case-sensitive match (paths package's Exist() is not always case-sensitive, so can't be used here).
return nil, false
}

if strings.EqualFold(path.Base(), correctBaseName) {
// There was a case-insensitive match.
return path, true
}
}

return nil, false
}

// MissingReadme checks if the project has a readme that will be recognized by GitHub.
func MissingReadme() (result checkresult.Type, output string) {
if checkdata.ProjectType() != checkdata.SuperProjectType() {
Expand Down Expand Up @@ -138,6 +101,43 @@ func IncorrectArduinoDotHFileNameCase() (result checkresult.Type, output string)
return checkresult.Pass, ""
}

// validProjectPathBaseName checks whether the provided library folder or sketch filename contains prohibited characters.
func validProjectPathBaseName(name string) bool {
baseNameRegexp := regexp.MustCompile("^[a-zA-Z0-9][a-zA-Z0-9_.-]*$")
return baseNameRegexp.MatchString(name)
}

func containsMisspelledPathBaseName(pathList paths.PathList, correctBaseName string, misspellingQuery string) (*paths.Path, bool) {
misspellingRegexp := regexp.MustCompile(misspellingQuery)
for _, path := range pathList {
if path.Base() == correctBaseName {
return nil, false
}

if misspellingRegexp.MatchString(path.Base()) {
return path, true
}
}

return nil, false
}

func containsIncorrectPathBaseCase(pathList paths.PathList, correctBaseName string) (*paths.Path, bool) {
for _, path := range pathList {
if path.Base() == correctBaseName {
// There was a case-sensitive match (paths package's Exist() is not always case-sensitive, so can't be used here).
return nil, false
}

if strings.EqualFold(path.Base(), correctBaseName) {
// There was a case-insensitive match.
return path, true
}
}

return nil, false
}

// pathContainsRegexpMatch checks if the provided path contains a file name matching the given regular expression.
func pathContainsRegexpMatch(path *paths.Path, pathRegexp *regexp.Regexp) bool {
listing, err := path.ReadDir()
Expand Down
Loading