Skip to content

Add check for library.properties name value mismatch with primary header filename #54

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 1 commit into from
Nov 25, 2020
Merged
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
15 changes: 15 additions & 0 deletions check/checkconfigurations/checkconfigurations.go
Original file line number Diff line number Diff line change
@@ -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",
13 changes: 13 additions & 0 deletions check/checkdata/library.go
Original file line number Diff line number Diff line change
@@ -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.
23 changes: 23 additions & 0 deletions check/checkfunctions/library.go
Original file line number Diff line number Diff line change
@@ -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 {
10 changes: 10 additions & 0 deletions check/checkfunctions/library_test.go
Original file line number Diff line number Diff line change
@@ -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, ""},
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=NameHeaderMismatch
version=1.0.0
author=Cristian Maglie <[email protected]>, Pippo Pluto <[email protected]>
maintainer=Cristian Maglie <[email protected]>
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
Empty file.