Skip to content

Commit fcc4d62

Browse files
committed
Fixed depends check (yet another try)
1 parent 41f069b commit fcc4d62

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/arduino.cc/repository/libraries/db/dependencies_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func TestDependencyExtract(t *testing.T) {
3131
[]string{">=1.2.3", "", "=1.0.0"})
3232
invalid("MyLib (>=1.2.3)()")
3333
invalid("MyLib (>=1.2.3),_aaaa")
34+
invalid("MyLib,,AnotherLib")
3435
invalid("MyLib (>=1.2.3)(),AnotherLib, YetAnotherLib (=1.0.0)")
3536
check("Arduino Uno WiFi Dev Ed Library, LoRa Node (^2.1.2)",
3637
[]string{"Arduino Uno WiFi Dev Ed Library", "LoRa Node"},
@@ -41,4 +42,5 @@ func TestDependencyExtract(t *testing.T) {
4142
check("Arduino_OAuth, ArduinoHttpClient (<0.3.0), NonExistentLib",
4243
[]string{"Arduino_OAuth", "ArduinoHttpClient", "NonExistentLib"},
4344
[]string{"", "<0.3.0", ""})
45+
check("", []string{}, []string{})
4446
}

src/arduino.cc/repository/libraries/db/library.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ var re = regexp.MustCompile("^([a-zA-Z0-9](?:[a-zA-Z0-9._\\- ]*[a-zA-Z0-9])?) *(
4747

4848
// ExtractDependenciesList extracts dependencies from the "depends" field of library.properties
4949
func ExtractDependenciesList(depends string) ([]*Dependency, error) {
50+
// TODO: merge this method with metadata.IsValidDependency
5051
deps := []*Dependency{}
52+
depends = strings.TrimSpace(depends)
53+
if depends == "" {
54+
return deps, nil
55+
}
5156
for _, dep := range strings.Split(depends, ",") {
5257
dep = strings.TrimSpace(dep)
5358
if dep == "" {
54-
continue
59+
return nil, fmt.Errorf("invalid dep: %s", dep)
5560
}
5661
matches := re.FindAllStringSubmatch(dep, -1)
5762
if matches == nil {

src/arduino.cc/repository/libraries/metadata/metadata.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ import (
1717
"context"
1818
"encoding/base64"
1919
"errors"
20+
"regexp"
2021
"strings"
2122

22-
"arduino.cc/repository/libraries/db"
23-
2423
"github.com/google/go-github/github"
2524
ini "github.com/vaughan0/go-ini"
2625
)
@@ -123,10 +122,26 @@ func IsValidLibraryName(name string) bool {
123122
return true
124123
}
125124

125+
var re = regexp.MustCompile("^([a-zA-Z0-9](?:[a-zA-Z0-9._\\- ]*[a-zA-Z0-9])?) *(?: \\(([^()]*)\\))?$")
126+
126127
// IsValidDependency checks if the `depends` field of library.properties is correctly formatted
127128
func IsValidDependency(depends string) bool {
128-
_, err := db.ExtractDependenciesList(depends)
129-
return err == nil
129+
// TODO: merge this method with db.ExtractDependenciesList
130+
depends = strings.TrimSpace(depends)
131+
if depends == "" {
132+
return true
133+
}
134+
for _, dep := range strings.Split(depends, ",") {
135+
dep = strings.TrimSpace(dep)
136+
if dep == "" {
137+
return false
138+
}
139+
matches := re.FindAllStringSubmatch(dep, -1)
140+
if matches == nil {
141+
return false
142+
}
143+
}
144+
return true
130145
}
131146

132147
// ParsePullRequest makes a LibraryMetadata by reading library.properties from a github.PullRequest

0 commit comments

Comments
 (0)