From 1e002830f67dad59645568353e3f693acc62664b Mon Sep 17 00:00:00 2001 From: per1234 Date: Thu, 26 May 2022 00:35:01 -0700 Subject: [PATCH 1/2] Support parentheses in `depends` field constraints Library dependencies may be specified in the `depends` field of the library.properties library metadata file. A regular expression is used to separate the dependency name and optional version constraint from each element of the field during parsing of the file to add new releases to the DB. The supported version constraint syntax for use in this field was recently expanded. This included adding support for grouping constraints using parentheses. The previous regular expression did not allow parentheses in the constraint, which unnecessarily limited the capabilities of the library dependencies system. The updated regular expression will match against invalid constraint syntax (e.g., `depends=FooLib (>1.2.3)(<2.3.4)`). However, this is not a problem because the syntax is already validated separately via the dedicated tool for such things: Arduino Lint. The DB update only occurs after the release has passed full validation by Arduino Lint. Given the data which has already been validated by Arduino Lint, this new regular expression will reliably extract the dependency components from the field. --- internal/libraries/db/dependencies_test.go | 11 +++++++++-- internal/libraries/db/library.go | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/libraries/db/dependencies_test.go b/internal/libraries/db/dependencies_test.go index bff2343d..39ecc1b0 100644 --- a/internal/libraries/db/dependencies_test.go +++ b/internal/libraries/db/dependencies_test.go @@ -48,14 +48,21 @@ func TestDependencyExtract(t *testing.T) { invalid("-invalidname") invalid("_invalidname") check("ciao", []string{"ciao"}, []string{""}) + check("MyLib (>1.2.3)", []string{"MyLib"}, []string{">1.2.3"}) check("MyLib (>=1.2.3)", []string{"MyLib"}, []string{">=1.2.3"}) + check("MyLib (<1.2.3)", []string{"MyLib"}, []string{"<1.2.3"}) + check("MyLib (<=1.2.3)", []string{"MyLib"}, []string{"<=1.2.3"}) + check("MyLib (!=1.2.3)", []string{"MyLib"}, []string{"!=1.2.3"}) + check("MyLib (>1.0.0 && <2.1.0)", []string{"MyLib"}, []string{">1.0.0 && <2.1.0"}) + check("MyLib (<1.0.0 || >2.0.0)", []string{"MyLib"}, []string{"<1.0.0 || >2.0.0"}) + check("MyLib ((>0.1.0 && <2.0.0) || >2.1.0)", []string{"MyLib"}, []string{"(>0.1.0 && <2.0.0) || >2.1.0"}) + check("MyLib ()", []string{"MyLib"}, []string{""}) check("MyLib (>=1.2.3),AnotherLib, YetAnotherLib (=1.0.0)", []string{"MyLib", "AnotherLib", "YetAnotherLib"}, []string{">=1.2.3", "", "=1.0.0"}) - invalid("MyLib (>=1.2.3)()") invalid("MyLib (>=1.2.3),_aaaa") invalid("MyLib,,AnotherLib") - invalid("MyLib (>=1.2.3)(),AnotherLib, YetAnotherLib (=1.0.0)") + invalid("MyLib(=1.2.3)") check("Arduino Uno WiFi Dev Ed Library, LoRa Node (^2.1.2)", []string{"Arduino Uno WiFi Dev Ed Library", "LoRa Node"}, []string{"", "^2.1.2"}) diff --git a/internal/libraries/db/library.go b/internal/libraries/db/library.go index e9e572eb..cdcb0b73 100644 --- a/internal/libraries/db/library.go +++ b/internal/libraries/db/library.go @@ -66,7 +66,7 @@ func extractStringList(value string) []string { return res } -var re = regexp.MustCompile("^([a-zA-Z0-9](?:[a-zA-Z0-9._\\- ]*[a-zA-Z0-9])?) *(?: \\(([^()]*)\\))?$") +var re = regexp.MustCompile("^([a-zA-Z0-9](?:[a-zA-Z0-9._\\- ]*[a-zA-Z0-9])?) *(?: \\((.*)\\))?$") // ExtractDependenciesList extracts dependencies from the "depends" field of library.properties func ExtractDependenciesList(depends string) ([]*Dependency, error) { From a545bb1073b9638eb13fccc021117e5262407734 Mon Sep 17 00:00:00 2001 From: per1234 Date: Thu, 26 May 2022 05:55:54 -0700 Subject: [PATCH 2/2] Remove redundant name validation from `depends` extraction regex Library dependencies may be specified in the `depends` field of the library.properties library metadata file. A regular expression is used to separate the dependency name and optional version constraint from each element of the field during parsing of the file to add new releases to the DB. A check is done to make sure the specified library names have a valid format. This project was originally solely responsible for such checks. Since that time, a dedicated tool was created for validation of the library releases: Arduino Lint. Since Arduino Lint already does this check, and the DB entries are only added for releases which pass that check, the validation via the extraction regular expression is superfluous and only increases the maintenance burden for no benefit. The regular expression has a single purpose: extracting the components of the depends field elements. This more simple regular expression will accomplish that, given the data which has already been validated by Arduino Lint. --- internal/libraries/db/dependencies_test.go | 4 +--- internal/libraries/db/library.go | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/internal/libraries/db/dependencies_test.go b/internal/libraries/db/dependencies_test.go index 39ecc1b0..9d904dbb 100644 --- a/internal/libraries/db/dependencies_test.go +++ b/internal/libraries/db/dependencies_test.go @@ -45,8 +45,6 @@ func TestDependencyExtract(t *testing.T) { require.Nil(t, dep) require.Error(t, err) } - invalid("-invalidname") - invalid("_invalidname") check("ciao", []string{"ciao"}, []string{""}) check("MyLib (>1.2.3)", []string{"MyLib"}, []string{">1.2.3"}) check("MyLib (>=1.2.3)", []string{"MyLib"}, []string{">=1.2.3"}) @@ -60,8 +58,8 @@ func TestDependencyExtract(t *testing.T) { check("MyLib (>=1.2.3),AnotherLib, YetAnotherLib (=1.0.0)", []string{"MyLib", "AnotherLib", "YetAnotherLib"}, []string{">=1.2.3", "", "=1.0.0"}) - invalid("MyLib (>=1.2.3),_aaaa") invalid("MyLib,,AnotherLib") + invalid("(MyLib)") invalid("MyLib(=1.2.3)") check("Arduino Uno WiFi Dev Ed Library, LoRa Node (^2.1.2)", []string{"Arduino Uno WiFi Dev Ed Library", "LoRa Node"}, diff --git a/internal/libraries/db/library.go b/internal/libraries/db/library.go index cdcb0b73..1d79333f 100644 --- a/internal/libraries/db/library.go +++ b/internal/libraries/db/library.go @@ -66,7 +66,7 @@ func extractStringList(value string) []string { return res } -var re = regexp.MustCompile("^([a-zA-Z0-9](?:[a-zA-Z0-9._\\- ]*[a-zA-Z0-9])?) *(?: \\((.*)\\))?$") +var re = regexp.MustCompile("^([^()]+?) *(?: \\((.*)\\))?$") // ExtractDependenciesList extracts dependencies from the "depends" field of library.properties func ExtractDependenciesList(depends string) ([]*Dependency, error) {