Skip to content

Commit 8bd2f66

Browse files
authored
Merge pull request #49 from arduino/per1234/incompatible-layout-checks
Add checks for library layout incompatibility with library.properties configuration
2 parents f830a43 + 0d88a95 commit 8bd2f66

File tree

17 files changed

+184
-0
lines changed

17 files changed

+184
-0
lines changed

check/checkconfigurations/checkconfigurations.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,36 @@ var configurations = []Type{
671671
ErrorModes: []checkmode.Type{checkmode.Default},
672672
CheckFunction: checkfunctions.LibraryPropertiesDotALinkageFieldInvalid,
673673
},
674+
{
675+
ProjectType: projecttype.Library,
676+
Category: "library.properties",
677+
Subcategory: "dot_a_linkage field",
678+
ID: "",
679+
Brief: `"true" with "1.5" library format`,
680+
Description: `dot_a_linkage feature is only supported for the "1.5" or "recursive" library format.`,
681+
MessageTemplate: `library.properties dot_a_linkage field enabled but library is not in "1.5" format. See: https://arduino.github.io/arduino-cli/latest/library-specification/#source-code`,
682+
DisableModes: nil,
683+
EnableModes: []checkmode.Type{checkmode.All},
684+
InfoModes: nil,
685+
WarningModes: []checkmode.Type{checkmode.Permissive},
686+
ErrorModes: []checkmode.Type{checkmode.Default},
687+
CheckFunction: checkfunctions.LibraryPropertiesDotALinkageFieldTrueWithFlatLayout,
688+
},
689+
{
690+
ProjectType: projecttype.Library,
691+
Category: "library.properties",
692+
Subcategory: "precompiled field",
693+
ID: "",
694+
Brief: "precompiled with flat layout",
695+
Description: `precompiled feature is only supported for the "1.5" or "recursive" library format.`,
696+
MessageTemplate: `library.properties precompiled field value {{.}}, is not supported with "1.0" format. See: https://arduino.github.io/arduino-cli/latest/library-specification/#libraryproperties-file-format`,
697+
DisableModes: nil,
698+
EnableModes: []checkmode.Type{checkmode.All},
699+
InfoModes: nil,
700+
WarningModes: []checkmode.Type{checkmode.Permissive},
701+
ErrorModes: []checkmode.Type{checkmode.Default},
702+
CheckFunction: checkfunctions.LibraryPropertiesPrecompiledFieldEnabledWithFlatLayout,
703+
},
674704
{
675705
ProjectType: projecttype.Sketch,
676706
Category: "structure",

check/checkdata/library.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/arduino/arduino-check/project"
2626
"github.com/arduino/arduino-check/project/library/libraryproperties"
2727
"github.com/arduino/arduino-check/result/feedback"
28+
"github.com/arduino/arduino-cli/arduino/libraries"
2829
"github.com/arduino/go-paths-helper"
2930
"github.com/arduino/go-properties-orderedmap"
3031
"github.com/client9/misspell"
@@ -34,6 +35,8 @@ import (
3435

3536
// Initialize gathers the library check data for the specified project.
3637
func InitializeForLibrary(project project.Type, schemasPath *paths.Path) {
38+
var err error
39+
3740
libraryProperties, libraryPropertiesLoadError = libraryproperties.Properties(project.Path)
3841
if libraryPropertiesLoadError != nil {
3942
logrus.Errorf("Error loading library.properties from %s: %s", project.Path, libraryPropertiesLoadError)
@@ -43,6 +46,12 @@ func InitializeForLibrary(project project.Type, schemasPath *paths.Path) {
4346
libraryPropertiesSchemaValidationResult = libraryproperties.Validate(libraryProperties, schemasPath)
4447
}
4548

49+
loadedLibrary, err = libraries.Load(project.Path, libraries.User)
50+
if err != nil {
51+
logrus.Errorf("Error loading library from %s: %s", project.Path, err)
52+
loadedLibrary = nil
53+
}
54+
4655
if libraryManagerIndex == nil { // Only download the Library Manager index once
4756
url := "http://downloads.arduino.cc/libraries/library_index.json"
4857
httpResponse, err := http.Get(url)
@@ -90,6 +99,13 @@ func LibraryPropertiesSchemaValidationResult() map[compliancelevel.Type]*jsonsch
9099
return libraryPropertiesSchemaValidationResult
91100
}
92101

102+
var loadedLibrary *libraries.Library
103+
104+
// LoadedLibrary returns the library object generated by Arduino CLI.
105+
func LoadedLibrary() *libraries.Library {
106+
return loadedLibrary
107+
}
108+
93109
var libraryManagerIndex map[string]interface{}
94110

95111
// LibraryManagerIndex returns the Library Manager index data.

check/checkfunctions/library.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/arduino/arduino-check/check/checkdata/schema/compliancelevel"
2727
"github.com/arduino/arduino-check/check/checkresult"
2828
"github.com/arduino/arduino-check/configuration"
29+
"github.com/arduino/arduino-cli/arduino/libraries"
2930
"github.com/arduino/go-properties-orderedmap"
3031
"github.com/sirupsen/logrus"
3132
)
@@ -688,6 +689,41 @@ func LibraryPropertiesDotALinkageFieldInvalid() (result checkresult.Type, output
688689
return checkresult.Pass, ""
689690
}
690691

692+
// LibraryPropertiesDotALinkageFieldTrueWithFlatLayout checks whether a library using the "dot_a_linkage" feature has the required recursive layout type.
693+
func LibraryPropertiesDotALinkageFieldTrueWithFlatLayout() (result checkresult.Type, output string) {
694+
if checkdata.LoadedLibrary() == nil {
695+
return checkresult.NotRun, ""
696+
}
697+
698+
if !checkdata.LibraryProperties().ContainsKey("dot_a_linkage") {
699+
return checkresult.NotRun, ""
700+
}
701+
702+
if checkdata.LoadedLibrary().DotALinkage && checkdata.LoadedLibrary().Layout == libraries.FlatLayout {
703+
return checkresult.Fail, ""
704+
}
705+
706+
return checkresult.Pass, ""
707+
}
708+
709+
// LibraryPropertiesPrecompiledFieldEnabledWithFlatLayout checks whether a precompiled library has the required recursive layout type.
710+
func LibraryPropertiesPrecompiledFieldEnabledWithFlatLayout() (result checkresult.Type, output string) {
711+
if checkdata.LoadedLibrary() == nil || checkdata.LibraryPropertiesLoadError() != nil {
712+
return checkresult.NotRun, ""
713+
}
714+
715+
precompiled, ok := checkdata.LibraryProperties().GetOk("precompiled")
716+
if !ok {
717+
return checkresult.NotRun, ""
718+
}
719+
720+
if checkdata.LoadedLibrary().Precompiled && checkdata.LoadedLibrary().Layout == libraries.FlatLayout {
721+
return checkresult.Fail, precompiled
722+
}
723+
724+
return checkresult.Pass, ""
725+
}
726+
691727
// spellCheckLibraryPropertiesFieldValue returns the value of the provided library.properties field with commonly misspelled words corrected.
692728
func spellCheckLibraryPropertiesFieldValue(fieldName string) (result checkresult.Type, output string) {
693729
if checkdata.LibraryPropertiesLoadError() != nil {

check/checkfunctions/library_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,27 @@ func TestLibraryPropertiesDependsFieldNotInIndex(t *testing.T) {
146146

147147
checkCheckFunction(LibraryPropertiesDependsFieldNotInIndex, testTables, t)
148148
}
149+
150+
func TestLibraryPropertiesDotALinkageFieldTrueWithFlatLayout(t *testing.T) {
151+
testTables := []checkFunctionTestTable{
152+
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
153+
{"Not defined", "MissingFields", checkresult.NotRun, ""},
154+
{"Flat layout", "DotALinkageFlat", checkresult.Fail, ""},
155+
{"Recursive layout", "DotALinkage", checkresult.Pass, ""},
156+
}
157+
158+
checkCheckFunction(LibraryPropertiesDotALinkageFieldTrueWithFlatLayout, testTables, t)
159+
}
160+
161+
func TestLibraryPropertiesPrecompiledFieldEnabledWithFlatLayout(t *testing.T) {
162+
testTables := []checkFunctionTestTable{
163+
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
164+
{"Not defined", "MissingFields", checkresult.NotRun, ""},
165+
{"Flat layout", "PrecompiledFlat", checkresult.Fail, "^true$"},
166+
{"Recursive layout", "Precompiled", checkresult.Pass, ""},
167+
{"Recursive, not precompiled", "NotPrecompiled", checkresult.NotRun, ""},
168+
{"Flat, not precompiled", "NotPrecompiledFlat", checkresult.NotRun, ""},
169+
}
170+
171+
checkCheckFunction(LibraryPropertiesPrecompiledFieldEnabledWithFlatLayout, testTables, t)
172+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=DotALinkage
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+
dot_a_linkage=true

check/checkfunctions/testdata/libraries/DotALinkage/src/DotALinkage.h

Whitespace-only changes.

check/checkfunctions/testdata/libraries/DotALinkageFlat/DotALinkageFlat.h

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=DotALinkageFlat
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+
dot_a_linkage=true
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=NotPrecompiled
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=Recursive.h

check/checkfunctions/testdata/libraries/NotPrecompiled/src/NotPrecompiled.h

Whitespace-only changes.

check/checkfunctions/testdata/libraries/NotPrecompiledFlat/NotPrecompiledFlat.h

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=NotPrecompiledFlat
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
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name=Precompiled
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=Recursive.h
11+
precompiled=true

check/checkfunctions/testdata/libraries/Precompiled/src/Precompiled.h

Whitespace-only changes.

check/checkfunctions/testdata/libraries/PrecompiledFlat/PrecompiledFlat.h

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=PrecompiledFlat
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+
precompiled=true

0 commit comments

Comments
 (0)