Skip to content

Commit e2656e4

Browse files
authored
Merge pull request #55 from arduino/per1234/includes-in-library-check
Add check for includes missing from library
2 parents 34d6956 + a90ff20 commit e2656e4

File tree

7 files changed

+163
-14
lines changed

7 files changed

+163
-14
lines changed

Diff for: check/checkconfigurations/checkconfigurations.go

+15
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,21 @@ var configurations = []Type{
716716
ErrorModes: []checkmode.Type{checkmode.Default},
717717
CheckFunction: checkfunctions.LibraryPropertiesIncludesFieldLTMinLength,
718718
},
719+
{
720+
ProjectType: projecttype.Library,
721+
Category: "library.properties",
722+
Subcategory: "includes field",
723+
ID: "",
724+
Brief: "includes file not in library",
725+
Description: `People often think this is the way to define their library's dependencies, which breaks the "Sketch > Include Library" feature for that library.`,
726+
MessageTemplate: "library.properties includes field item(s) {{.}} not found in library.",
727+
DisableModes: nil,
728+
EnableModes: []checkmode.Type{checkmode.All},
729+
InfoModes: nil,
730+
WarningModes: []checkmode.Type{checkmode.Permissive},
731+
ErrorModes: []checkmode.Type{checkmode.Default},
732+
CheckFunction: checkfunctions.LibraryPropertiesIncludesFieldItemNotFound,
733+
},
719734
{
720735
ProjectType: projecttype.Library,
721736
Category: "library.properties",

Diff for: check/checkfunctions/library.go

+41
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,47 @@ func LibraryPropertiesIncludesFieldLTMinLength() (result checkresult.Type, outpu
746746
return checkresult.Pass, ""
747747
}
748748

749+
// LibraryPropertiesIncludesFieldItemNotFound checks whether the header files specified in the library.properties `includes` field are in the library.
750+
func LibraryPropertiesIncludesFieldItemNotFound() (result checkresult.Type, output string) {
751+
if checkdata.LibraryPropertiesLoadError() != nil {
752+
return checkresult.NotRun, ""
753+
}
754+
755+
includes, ok := checkdata.LibraryProperties().GetOk("includes")
756+
if !ok {
757+
return checkresult.NotRun, ""
758+
}
759+
760+
includesList, err := properties.SplitQuotedString(includes, "", false)
761+
if err != nil {
762+
panic(err)
763+
}
764+
765+
findInclude := func(include string) bool {
766+
for _, header := range checkdata.SourceHeaders() {
767+
logrus.Tracef("Comparing include %s with header file %s", include, header)
768+
if include == header {
769+
logrus.Tracef("match!")
770+
return true
771+
}
772+
}
773+
return false
774+
}
775+
776+
includesNotInLibrary := []string{}
777+
for _, include := range includesList {
778+
if !findInclude(include) {
779+
includesNotInLibrary = append(includesNotInLibrary, include)
780+
}
781+
}
782+
783+
if len(includesNotInLibrary) > 0 {
784+
return checkresult.Fail, strings.Join(includesNotInLibrary, ", ")
785+
}
786+
787+
return checkresult.Pass, ""
788+
}
789+
749790
// LibraryPropertiesPrecompiledFieldInvalid checks for invalid value in the library.properties "precompiled" field.
750791
func LibraryPropertiesPrecompiledFieldInvalid() (result checkresult.Type, output string) {
751792
if checkdata.LibraryPropertiesLoadError() != nil {

Diff for: check/checkfunctions/library_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,17 @@ func TestLibraryPropertiesDotALinkageFieldTrueWithFlatLayout(t *testing.T) {
168168
checkCheckFunction(LibraryPropertiesDotALinkageFieldTrueWithFlatLayout, testTables, t)
169169
}
170170

171+
func TestLibraryPropertiesIncludesFieldItemNotFound(t *testing.T) {
172+
testTables := []checkFunctionTestTable{
173+
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
174+
{"Not defined", "MissingFields", checkresult.NotRun, ""},
175+
{"Missing includes", "MissingIncludes", checkresult.Fail, "^Nonexistent.h$"},
176+
{"Present includes", "Recursive", checkresult.Pass, ""},
177+
}
178+
179+
checkCheckFunction(LibraryPropertiesIncludesFieldItemNotFound, testTables, t)
180+
}
181+
171182
func TestLibraryPropertiesPrecompiledFieldEnabledWithFlatLayout(t *testing.T) {
172183
testTables := []checkFunctionTestTable{
173184
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=MissingIncludes
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
10+
includes=Nonexistent.h

Diff for: check/checkfunctions/testdata/libraries/MissingIncludes/src/MissingIncludes.h

Whitespace-only changes.

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/arduino/arduino-check
33
go 1.14
44

55
require (
6-
github.com/arduino/arduino-cli v0.0.0-20200922073731-53e3230c4f71
6+
github.com/arduino/arduino-cli v0.0.0-20201124150942-8d026eddbfb4
77
github.com/arduino/go-paths-helper v1.3.2
88
github.com/arduino/go-properties-orderedmap v1.4.0
99
github.com/client9/misspell v0.3.4

0 commit comments

Comments
 (0)