Skip to content

Commit f1607e9

Browse files
committed
Add check for library.properties name value mismatch with primary header filename
1 parent 71d0120 commit f1607e9

File tree

6 files changed

+70
-0
lines changed

6 files changed

+70
-0
lines changed

Diff for: check/checkconfigurations/checkconfigurations.go

+15
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,21 @@ var configurations = []Type{
251251
ErrorModes: []checkmode.Type{checkmode.All},
252252
CheckFunction: checkfunctions.LibraryPropertiesNameFieldNotInIndex,
253253
},
254+
{
255+
ProjectType: projecttype.Library,
256+
Category: "structure",
257+
Subcategory: "general",
258+
ID: "",
259+
Brief: "name-header mismatch",
260+
Description: `The name value determines the installation folder name and the folder match to the filename in the #include directive influences "folder name priority".`,
261+
MessageTemplate: "No header file found matching library name ({{.}}). Best practices are for primary header filename to match library name.",
262+
DisableModes: nil,
263+
EnableModes: []checkmode.Type{checkmode.All},
264+
InfoModes: []checkmode.Type{checkmode.Permissive},
265+
WarningModes: []checkmode.Type{checkmode.Default},
266+
ErrorModes: nil,
267+
CheckFunction: checkfunctions.LibraryPropertiesNameFieldHeaderMismatch,
268+
},
254269
{
255270
ProjectType: projecttype.Library,
256271
Category: "library.properties",

Diff for: check/checkdata/library.go

+13
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ func InitializeForLibrary(project project.Type, schemasPath *paths.Path) {
5050
if err != nil {
5151
logrus.Errorf("Error loading library from %s: %s", project.Path, err)
5252
loadedLibrary = nil
53+
sourceHeaders = nil
54+
} else {
55+
sourceHeaders, err = loadedLibrary.SourceHeaders()
56+
if err != nil {
57+
panic(err)
58+
}
5359
}
5460

5561
if libraryManagerIndex == nil { // Only download the Library Manager index once
@@ -106,6 +112,13 @@ func LoadedLibrary() *libraries.Library {
106112
return loadedLibrary
107113
}
108114

115+
var sourceHeaders []string
116+
117+
// SourceHeaders returns the list of library source header filenames discovered by Arduino CLI.
118+
func SourceHeaders() []string {
119+
return sourceHeaders
120+
}
121+
109122
var libraryManagerIndex map[string]interface{}
110123

111124
// LibraryManagerIndex returns the Library Manager index data.

Diff for: check/checkfunctions/library.go

+23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package checkfunctions
1919

2020
import (
2121
"net/http"
22+
"path/filepath"
2223
"strings"
2324

2425
"github.com/arduino/arduino-check/check/checkdata"
@@ -27,6 +28,7 @@ import (
2728
"github.com/arduino/arduino-check/check/checkresult"
2829
"github.com/arduino/arduino-check/configuration"
2930
"github.com/arduino/arduino-cli/arduino/libraries"
31+
"github.com/arduino/arduino-cli/arduino/utils"
3032
"github.com/arduino/go-properties-orderedmap"
3133
"github.com/sirupsen/logrus"
3234
)
@@ -242,6 +244,27 @@ func LibraryPropertiesNameFieldNotInIndex() (result checkresult.Type, output str
242244
return checkresult.Fail, name
243245
}
244246

247+
// LibraryPropertiesNameFieldHeaderMismatch checks whether the filename of one of the library's header files matches the Library Manager installation folder name.
248+
func LibraryPropertiesNameFieldHeaderMismatch() (result checkresult.Type, output string) {
249+
if checkdata.LibraryPropertiesLoadError() != nil {
250+
return checkresult.NotRun, ""
251+
}
252+
253+
name, ok := checkdata.LibraryProperties().GetOk("name")
254+
if !ok {
255+
return checkresult.NotRun, ""
256+
}
257+
258+
sanitizedName := utils.SanitizeName(name)
259+
for _, header := range checkdata.SourceHeaders() {
260+
if strings.TrimSuffix(header, filepath.Ext(header)) == sanitizedName {
261+
return checkresult.Pass, ""
262+
}
263+
}
264+
265+
return checkresult.Fail, sanitizedName + ".h"
266+
}
267+
245268
// LibraryPropertiesVersionFieldMissing checks for missing library.properties "version" field.
246269
func LibraryPropertiesVersionFieldMissing() (result checkresult.Type, output string) {
247270
if checkdata.LibraryPropertiesLoadError() != nil {

Diff for: check/checkfunctions/library_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ func TestLibraryPropertiesNameFieldNotInIndex(t *testing.T) {
9393
checkCheckFunction(LibraryPropertiesNameFieldNotInIndex, testTables, t)
9494
}
9595

96+
func TestLibraryPropertiesNameFieldHeaderMismatch(t *testing.T) {
97+
testTables := []checkFunctionTestTable{
98+
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
99+
{"Mismatch", "NameHeaderMismatch", checkresult.Fail, "^NameHeaderMismatch.h$"},
100+
{"Match", "Recursive", checkresult.Pass, ""},
101+
}
102+
103+
checkCheckFunction(LibraryPropertiesNameFieldHeaderMismatch, testTables, t)
104+
}
105+
96106
func TestLibraryPropertiesSentenceFieldSpellCheck(t *testing.T) {
97107
testTables := []checkFunctionTestTable{
98108
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=NameHeaderMismatch
2+
version=1.0.0
3+
author=Cristian Maglie <[email protected]>, Pippo Pluto <[email protected]>
4+
maintainer=Cristian Maglie <[email protected]>
5+
sentence=A library that makes coding a web server a breeze.
6+
paragraph=Supports HTTP1.1 and you can do GET and POST.
7+
category=Communication
8+
url=http://example.com/
9+
architectures=avr

Diff for: check/checkfunctions/testdata/libraries/NameHeaderMismatch/src/NotNameHeaderMismatch.h

Whitespace-only changes.

0 commit comments

Comments
 (0)