From f1607e9da28038e70f22c51b1af023d6fd0e4651 Mon Sep 17 00:00:00 2001 From: per1234 Date: Tue, 24 Nov 2020 12:49:13 -0800 Subject: [PATCH] Add check for library.properties name value mismatch with primary header filename --- .../checkconfigurations.go | 15 ++++++++++++ check/checkdata/library.go | 13 +++++++++++ check/checkfunctions/library.go | 23 +++++++++++++++++++ check/checkfunctions/library_test.go | 10 ++++++++ .../NameHeaderMismatch/library.properties | 9 ++++++++ .../src/NotNameHeaderMismatch.h | 0 6 files changed, 70 insertions(+) create mode 100644 check/checkfunctions/testdata/libraries/NameHeaderMismatch/library.properties create mode 100644 check/checkfunctions/testdata/libraries/NameHeaderMismatch/src/NotNameHeaderMismatch.h diff --git a/check/checkconfigurations/checkconfigurations.go b/check/checkconfigurations/checkconfigurations.go index 9e896e3e..d4652f01 100644 --- a/check/checkconfigurations/checkconfigurations.go +++ b/check/checkconfigurations/checkconfigurations.go @@ -251,6 +251,21 @@ var configurations = []Type{ ErrorModes: []checkmode.Type{checkmode.All}, CheckFunction: checkfunctions.LibraryPropertiesNameFieldNotInIndex, }, + { + ProjectType: projecttype.Library, + Category: "structure", + Subcategory: "general", + ID: "", + Brief: "name-header mismatch", + Description: `The name value determines the installation folder name and the folder match to the filename in the #include directive influences "folder name priority".`, + MessageTemplate: "No header file found matching library name ({{.}}). Best practices are for primary header filename to match library name.", + DisableModes: nil, + EnableModes: []checkmode.Type{checkmode.All}, + InfoModes: []checkmode.Type{checkmode.Permissive}, + WarningModes: []checkmode.Type{checkmode.Default}, + ErrorModes: nil, + CheckFunction: checkfunctions.LibraryPropertiesNameFieldHeaderMismatch, + }, { ProjectType: projecttype.Library, Category: "library.properties", diff --git a/check/checkdata/library.go b/check/checkdata/library.go index 0c2cb69c..73a6213f 100644 --- a/check/checkdata/library.go +++ b/check/checkdata/library.go @@ -50,6 +50,12 @@ func InitializeForLibrary(project project.Type, schemasPath *paths.Path) { if err != nil { logrus.Errorf("Error loading library from %s: %s", project.Path, err) loadedLibrary = nil + sourceHeaders = nil + } else { + sourceHeaders, err = loadedLibrary.SourceHeaders() + if err != nil { + panic(err) + } } if libraryManagerIndex == nil { // Only download the Library Manager index once @@ -106,6 +112,13 @@ func LoadedLibrary() *libraries.Library { return loadedLibrary } +var sourceHeaders []string + +// SourceHeaders returns the list of library source header filenames discovered by Arduino CLI. +func SourceHeaders() []string { + return sourceHeaders +} + var libraryManagerIndex map[string]interface{} // LibraryManagerIndex returns the Library Manager index data. diff --git a/check/checkfunctions/library.go b/check/checkfunctions/library.go index 7e750566..8d6a396a 100644 --- a/check/checkfunctions/library.go +++ b/check/checkfunctions/library.go @@ -19,6 +19,7 @@ package checkfunctions import ( "net/http" + "path/filepath" "strings" "github.com/arduino/arduino-check/check/checkdata" @@ -27,6 +28,7 @@ import ( "github.com/arduino/arduino-check/check/checkresult" "github.com/arduino/arduino-check/configuration" "github.com/arduino/arduino-cli/arduino/libraries" + "github.com/arduino/arduino-cli/arduino/utils" "github.com/arduino/go-properties-orderedmap" "github.com/sirupsen/logrus" ) @@ -242,6 +244,27 @@ func LibraryPropertiesNameFieldNotInIndex() (result checkresult.Type, output str return checkresult.Fail, name } +// LibraryPropertiesNameFieldHeaderMismatch checks whether the filename of one of the library's header files matches the Library Manager installation folder name. +func LibraryPropertiesNameFieldHeaderMismatch() (result checkresult.Type, output string) { + if checkdata.LibraryPropertiesLoadError() != nil { + return checkresult.NotRun, "" + } + + name, ok := checkdata.LibraryProperties().GetOk("name") + if !ok { + return checkresult.NotRun, "" + } + + sanitizedName := utils.SanitizeName(name) + for _, header := range checkdata.SourceHeaders() { + if strings.TrimSuffix(header, filepath.Ext(header)) == sanitizedName { + return checkresult.Pass, "" + } + } + + return checkresult.Fail, sanitizedName + ".h" +} + // LibraryPropertiesVersionFieldMissing checks for missing library.properties "version" field. func LibraryPropertiesVersionFieldMissing() (result checkresult.Type, output string) { if checkdata.LibraryPropertiesLoadError() != nil { diff --git a/check/checkfunctions/library_test.go b/check/checkfunctions/library_test.go index b3feece0..9aa5ab4a 100644 --- a/check/checkfunctions/library_test.go +++ b/check/checkfunctions/library_test.go @@ -93,6 +93,16 @@ func TestLibraryPropertiesNameFieldNotInIndex(t *testing.T) { checkCheckFunction(LibraryPropertiesNameFieldNotInIndex, testTables, t) } +func TestLibraryPropertiesNameFieldHeaderMismatch(t *testing.T) { + testTables := []checkFunctionTestTable{ + {"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""}, + {"Mismatch", "NameHeaderMismatch", checkresult.Fail, "^NameHeaderMismatch.h$"}, + {"Match", "Recursive", checkresult.Pass, ""}, + } + + checkCheckFunction(LibraryPropertiesNameFieldHeaderMismatch, testTables, t) +} + func TestLibraryPropertiesSentenceFieldSpellCheck(t *testing.T) { testTables := []checkFunctionTestTable{ {"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""}, diff --git a/check/checkfunctions/testdata/libraries/NameHeaderMismatch/library.properties b/check/checkfunctions/testdata/libraries/NameHeaderMismatch/library.properties new file mode 100644 index 00000000..920864bb --- /dev/null +++ b/check/checkfunctions/testdata/libraries/NameHeaderMismatch/library.properties @@ -0,0 +1,9 @@ +name=NameHeaderMismatch +version=1.0.0 +author=Cristian Maglie , Pippo Pluto +maintainer=Cristian Maglie +sentence=A library that makes coding a web server a breeze. +paragraph=Supports HTTP1.1 and you can do GET and POST. +category=Communication +url=http://example.com/ +architectures=avr diff --git a/check/checkfunctions/testdata/libraries/NameHeaderMismatch/src/NotNameHeaderMismatch.h b/check/checkfunctions/testdata/libraries/NameHeaderMismatch/src/NotNameHeaderMismatch.h new file mode 100644 index 00000000..e69de29b