From f3024198aee0b495f38644f1e2dc53ea439b6dab Mon Sep 17 00:00:00 2001 From: per1234 Date: Wed, 26 May 2021 20:48:05 -0700 Subject: [PATCH] Add min length rule for library.properties url field Previously, the permissive compliance mode only required that the library.properties url field was present. The field was allowed to be empty in this mode. Investigation revealed that the Arduino Library Manager indexer system, which Arduino Lint's permissive compliance mode is intended to match, requires the url field value to be at least one character long: https://github.com/arduino/libraries-repository-engine/blob/c395aaf14e7568f585a6a96350b584682a3c7feb/libraries/metadata/metadata.go#L110 --- ...o-library-properties-definitions-schema.json | 3 +++ .../rule/ruleconfiguration/ruleconfiguration.go | 16 ++++++++++++++++ internal/rule/rulefunction/library.go | 17 +++++++++++++++++ internal/rule/rulefunction/library_test.go | 11 +++++++++++ .../libraries/UrlLTMinLength/library.properties | 10 ++++++++++ .../UrlLTMinLength/src/UrlLTMinLength.h | 0 internal/rule/schema/schemadata/bindata.go | 3 +++ 7 files changed, 60 insertions(+) create mode 100644 internal/rule/rulefunction/testdata/libraries/UrlLTMinLength/library.properties create mode 100644 internal/rule/rulefunction/testdata/libraries/UrlLTMinLength/src/UrlLTMinLength.h diff --git a/etc/schemas/arduino-library-properties-definitions-schema.json b/etc/schemas/arduino-library-properties-definitions-schema.json index acb39870..c9d8b938 100644 --- a/etc/schemas/arduino-library-properties-definitions-schema.json +++ b/etc/schemas/arduino-library-properties-definitions-schema.json @@ -413,6 +413,9 @@ "allOf": [ { "type": "string" + }, + { + "minLength": 1 } ] } diff --git a/internal/rule/ruleconfiguration/ruleconfiguration.go b/internal/rule/ruleconfiguration/ruleconfiguration.go index 660329ae..414da657 100644 --- a/internal/rule/ruleconfiguration/ruleconfiguration.go +++ b/internal/rule/ruleconfiguration/ruleconfiguration.go @@ -889,6 +889,22 @@ var configurations = []Type{ ErrorModes: []rulemode.Type{rulemode.Default}, RuleFunction: rulefunction.LibraryPropertiesUrlFieldMissing, }, + { + ProjectType: projecttype.Library, + SuperprojectType: projecttype.All, + Category: "library.properties", + Subcategory: "url field", + ID: "LP056", + Brief: "url < min length", + Description: "", + MessageTemplate: "library.properties url value is less than minimum length.", + DisableModes: nil, + EnableModes: []rulemode.Type{rulemode.Default}, + InfoModes: nil, + WarningModes: nil, + ErrorModes: []rulemode.Type{rulemode.Default}, + RuleFunction: rulefunction.LibraryPropertiesUrlFieldLTMinLength, + }, { ProjectType: projecttype.Library, SuperprojectType: projecttype.All, diff --git a/internal/rule/rulefunction/library.go b/internal/rule/rulefunction/library.go index 663721da..db72dd8d 100644 --- a/internal/rule/rulefunction/library.go +++ b/internal/rule/rulefunction/library.go @@ -944,6 +944,23 @@ func LibraryPropertiesUrlFieldMissing() (result ruleresult.Type, output string) return ruleresult.Pass, "" } +// LibraryPropertiesUrlFieldLTMinLength checks if the library.properties "url" value is less than the minimum length. +func LibraryPropertiesUrlFieldLTMinLength() (result ruleresult.Type, output string) { + if projectdata.LibraryPropertiesLoadError() != nil { + return ruleresult.NotRun, "Couldn't load library.properties" + } + + if !projectdata.LibraryProperties().ContainsKey("url") { + return ruleresult.NotRun, "Field not present" + } + + if schema.PropertyLessThanMinLength("url", projectdata.LibraryPropertiesSchemaValidationResult()[compliancelevel.Permissive]) { + return ruleresult.Fail, "" + } + + return ruleresult.Pass, "" +} + // LibraryPropertiesUrlFieldInvalid checks whether the library.properties "url" value has a valid URL format. func LibraryPropertiesUrlFieldInvalid() (result ruleresult.Type, output string) { if projectdata.LibraryPropertiesLoadError() != nil { diff --git a/internal/rule/rulefunction/library_test.go b/internal/rule/rulefunction/library_test.go index f39b5d31..fb2f2a3d 100644 --- a/internal/rule/rulefunction/library_test.go +++ b/internal/rule/rulefunction/library_test.go @@ -714,6 +714,17 @@ func TestLibraryPropertiesUrlFieldMissing(t *testing.T) { checkLibraryRuleFunction(LibraryPropertiesUrlFieldMissing, testTables, t) } +func TestLibraryPropertiesUrlFieldLTMinLength(t *testing.T) { + testTables := []libraryRuleFunctionTestTable{ + {"Invalid", "InvalidLibraryProperties", ruleresult.NotRun, ""}, + {"Legacy", "Legacy", ruleresult.NotRun, ""}, + {"url field too short", "UrlLTMinLength", ruleresult.Fail, ""}, + {"Valid", "Recursive", ruleresult.Pass, ""}, + } + + checkLibraryRuleFunction(LibraryPropertiesUrlFieldLTMinLength, testTables, t) +} + func TestLibraryPropertiesUrlFieldInvalid(t *testing.T) { testTables := []libraryRuleFunctionTestTable{ {"Invalid", "InvalidLibraryProperties", ruleresult.NotRun, ""}, diff --git a/internal/rule/rulefunction/testdata/libraries/UrlLTMinLength/library.properties b/internal/rule/rulefunction/testdata/libraries/UrlLTMinLength/library.properties new file mode 100644 index 00000000..d4613990 --- /dev/null +++ b/internal/rule/rulefunction/testdata/libraries/UrlLTMinLength/library.properties @@ -0,0 +1,10 @@ +name=UrlLTMinLength +version=1.0.0 +author=Cristian Maglie , Pippo Pluto +maintainer=Cristian Maglie +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= +architectures=avr +includes=UrlLTMinLength.h diff --git a/internal/rule/rulefunction/testdata/libraries/UrlLTMinLength/src/UrlLTMinLength.h b/internal/rule/rulefunction/testdata/libraries/UrlLTMinLength/src/UrlLTMinLength.h new file mode 100644 index 00000000..e69de29b diff --git a/internal/rule/schema/schemadata/bindata.go b/internal/rule/schema/schemadata/bindata.go index 43eeb5da..16cc58ce 100644 --- a/internal/rule/schema/schemadata/bindata.go +++ b/internal/rule/schema/schemadata/bindata.go @@ -1832,6 +1832,9 @@ var _arduinoLibraryPropertiesDefinitionsSchemaJson = []byte(`{ "allOf": [ { "type": "string" + }, + { + "minLength": 1 } ] }