Skip to content

Add checks for library layout incompatibility with library.properties configuration #49

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 24, 2020
Merged
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions check/checkconfigurations/checkconfigurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,36 @@ var configurations = []Type{
ErrorModes: []checkmode.Type{checkmode.Default},
CheckFunction: checkfunctions.LibraryPropertiesDotALinkageFieldInvalid,
},
{
ProjectType: projecttype.Library,
Category: "library.properties",
Subcategory: "dot_a_linkage field",
ID: "",
Brief: `"true" with "1.5" library format`,
Description: `dot_a_linkage feature is only supported for the "1.5" or "recursive" library format.`,
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`,
DisableModes: nil,
EnableModes: []checkmode.Type{checkmode.All},
InfoModes: nil,
WarningModes: []checkmode.Type{checkmode.Permissive},
ErrorModes: []checkmode.Type{checkmode.Default},
CheckFunction: checkfunctions.LibraryPropertiesDotALinkageFieldTrueWithFlatLayout,
},
{
ProjectType: projecttype.Library,
Category: "library.properties",
Subcategory: "precompiled field",
ID: "",
Brief: "precompiled with flat layout",
Description: `precompiled feature is only supported for the "1.5" or "recursive" library format.`,
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`,
DisableModes: nil,
EnableModes: []checkmode.Type{checkmode.All},
InfoModes: nil,
WarningModes: []checkmode.Type{checkmode.Permissive},
ErrorModes: []checkmode.Type{checkmode.Default},
CheckFunction: checkfunctions.LibraryPropertiesPrecompiledFieldEnabledWithFlatLayout,
},
{
ProjectType: projecttype.Sketch,
Category: "structure",
Expand Down
16 changes: 16 additions & 0 deletions check/checkdata/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/arduino/arduino-check/project"
"github.com/arduino/arduino-check/project/library/libraryproperties"
"github.com/arduino/arduino-check/result/feedback"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/client9/misspell"
Expand All @@ -34,6 +35,8 @@ import (

// Initialize gathers the library check data for the specified project.
func InitializeForLibrary(project project.Type, schemasPath *paths.Path) {
var err error

libraryProperties, libraryPropertiesLoadError = libraryproperties.Properties(project.Path)
if libraryPropertiesLoadError != nil {
logrus.Errorf("Error loading library.properties from %s: %s", project.Path, libraryPropertiesLoadError)
Expand All @@ -43,6 +46,12 @@ func InitializeForLibrary(project project.Type, schemasPath *paths.Path) {
libraryPropertiesSchemaValidationResult = libraryproperties.Validate(libraryProperties, schemasPath)
}

loadedLibrary, err = libraries.Load(project.Path, libraries.User)
if err != nil {
logrus.Errorf("Error loading library from %s: %s", project.Path, err)
loadedLibrary = nil
}

if libraryManagerIndex == nil { // Only download the Library Manager index once
url := "http://downloads.arduino.cc/libraries/library_index.json"
httpResponse, err := http.Get(url)
Expand Down Expand Up @@ -90,6 +99,13 @@ func LibraryPropertiesSchemaValidationResult() map[compliancelevel.Type]*jsonsch
return libraryPropertiesSchemaValidationResult
}

var loadedLibrary *libraries.Library

// LoadedLibrary returns the library object generated by Arduino CLI.
func LoadedLibrary() *libraries.Library {
return loadedLibrary
}

var libraryManagerIndex map[string]interface{}

// LibraryManagerIndex returns the Library Manager index data.
Expand Down
36 changes: 36 additions & 0 deletions check/checkfunctions/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/arduino/arduino-check/check/checkdata/schema/compliancelevel"
"github.com/arduino/arduino-check/check/checkresult"
"github.com/arduino/arduino-check/configuration"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/go-properties-orderedmap"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -688,6 +689,41 @@ func LibraryPropertiesDotALinkageFieldInvalid() (result checkresult.Type, output
return checkresult.Pass, ""
}

// LibraryPropertiesDotALinkageFieldTrueWithFlatLayout checks whether a library using the "dot_a_linkage" feature has the required recursive layout type.
func LibraryPropertiesDotALinkageFieldTrueWithFlatLayout() (result checkresult.Type, output string) {
if checkdata.LoadedLibrary() == nil {
return checkresult.NotRun, ""
}

if !checkdata.LibraryProperties().ContainsKey("dot_a_linkage") {
return checkresult.NotRun, ""
}

if checkdata.LoadedLibrary().DotALinkage && checkdata.LoadedLibrary().Layout == libraries.FlatLayout {
return checkresult.Fail, ""
}

return checkresult.Pass, ""
}

// LibraryPropertiesPrecompiledFieldEnabledWithFlatLayout checks whether a precompiled library has the required recursive layout type.
func LibraryPropertiesPrecompiledFieldEnabledWithFlatLayout() (result checkresult.Type, output string) {
if checkdata.LoadedLibrary() == nil || checkdata.LibraryPropertiesLoadError() != nil {
return checkresult.NotRun, ""
}

precompiled, ok := checkdata.LibraryProperties().GetOk("precompiled")
if !ok {
return checkresult.NotRun, ""
}

if checkdata.LoadedLibrary().Precompiled && checkdata.LoadedLibrary().Layout == libraries.FlatLayout {
return checkresult.Fail, precompiled
}

return checkresult.Pass, ""
}

// spellCheckLibraryPropertiesFieldValue returns the value of the provided library.properties field with commonly misspelled words corrected.
func spellCheckLibraryPropertiesFieldValue(fieldName string) (result checkresult.Type, output string) {
if checkdata.LibraryPropertiesLoadError() != nil {
Expand Down
24 changes: 24 additions & 0 deletions check/checkfunctions/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,27 @@ func TestLibraryPropertiesDependsFieldNotInIndex(t *testing.T) {

checkCheckFunction(LibraryPropertiesDependsFieldNotInIndex, testTables, t)
}

func TestLibraryPropertiesDotALinkageFieldTrueWithFlatLayout(t *testing.T) {
testTables := []checkFunctionTestTable{
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
{"Not defined", "MissingFields", checkresult.NotRun, ""},
{"Flat layout", "DotALinkageFlat", checkresult.Fail, ""},
{"Recursive layout", "DotALinkage", checkresult.Pass, ""},
}

checkCheckFunction(LibraryPropertiesDotALinkageFieldTrueWithFlatLayout, testTables, t)
}

func TestLibraryPropertiesPrecompiledFieldEnabledWithFlatLayout(t *testing.T) {
testTables := []checkFunctionTestTable{
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
{"Not defined", "MissingFields", checkresult.NotRun, ""},
{"Flat layout", "PrecompiledFlat", checkresult.Fail, "^true$"},
{"Recursive layout", "Precompiled", checkresult.Pass, ""},
{"Recursive, not precompiled", "NotPrecompiled", checkresult.NotRun, ""},
{"Flat, not precompiled", "NotPrecompiledFlat", checkresult.NotRun, ""},
}

checkCheckFunction(LibraryPropertiesPrecompiledFieldEnabledWithFlatLayout, testTables, t)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=DotALinkage
version=1.0.0
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
maintainer=Cristian Maglie <c.maglie@example.com>
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
dot_a_linkage=true
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=DotALinkageFlat
version=1.0.0
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
maintainer=Cristian Maglie <c.maglie@example.com>
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
dot_a_linkage=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=NotPrecompiled
version=1.0.0
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
maintainer=Cristian Maglie <c.maglie@example.com>
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
includes=Recursive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=NotPrecompiledFlat
version=1.0.0
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
maintainer=Cristian Maglie <c.maglie@example.com>
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name=Precompiled
version=1.0.0
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
maintainer=Cristian Maglie <c.maglie@example.com>
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
includes=Recursive.h
precompiled=true
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=PrecompiledFlat
version=1.0.0
author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>
maintainer=Cristian Maglie <c.maglie@example.com>
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
precompiled=true
Loading