Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 84e7f5e

Browse files
authored
Merge pull request #54 from arduino/per1234/name-header-mismatch-checks
Add check for library.properties name value mismatch with primary header filename
2 parents 71d0120 + f1607e9 commit 84e7f5e

File tree

6 files changed

+70
-0
lines changed

6 files changed

+70
-0
lines changed

‎check/checkconfigurations/checkconfigurations.go‎

Lines changed: 15 additions & 0 deletions
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",

‎check/checkdata/library.go‎

Lines changed: 13 additions & 0 deletions
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.

‎check/checkfunctions/library.go‎

Lines changed: 23 additions & 0 deletions
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 {

‎check/checkfunctions/library_test.go‎

Lines changed: 10 additions & 0 deletions
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, ""},
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=NameHeaderMismatch
2+
version=1.0.0
3+
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
4+
maintainer=Cristian Maglie <c.maglie@example.com>
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

‎check/checkfunctions/testdata/libraries/NameHeaderMismatch/src/NotNameHeaderMismatch.h‎

Whitespace-only changes.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /