Skip to content

Commit 7e8313f

Browse files
committed
Added unit tests from library-registry repo
1 parent d0b926b commit 7e8313f

9 files changed

+77
-16
lines changed
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package checkregistry
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"reflect"
@@ -9,32 +10,34 @@ import (
910
)
1011

1112
func CheckRegistry(reposFile string) {
13+
if err := runcheck(reposFile); err != nil {
14+
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
15+
os.Exit(1)
16+
}
17+
}
18+
19+
func runcheck(reposFile string) error {
1220
info, err := os.Stat(reposFile)
1321
if err != nil {
14-
fmt.Fprintf(os.Stderr, "error: While loading registry data file: %s\n", err)
15-
os.Exit(1)
22+
return fmt.Errorf("while loading registry data file: %w", err)
1623
}
1724

1825
if info.IsDir() {
19-
fmt.Fprintf(os.Stderr, "error: Registry data file argument %s is a folder, not a file\n", reposFile)
20-
os.Exit(1)
26+
return fmt.Errorf("registry data file argument %s is a folder, not a file", reposFile)
2127
}
2228

2329
rawRepos, err := libraries.LoadRepoListFromFile(reposFile)
2430
if err != nil {
25-
fmt.Fprintf(os.Stderr, "error: While loading registry data file: %s\n", err)
26-
os.Exit(1)
31+
return fmt.Errorf("while loading registry data file: %w", err)
2732
}
2833

2934
filteredRepos, err := libraries.ListRepos(reposFile)
3035
if err != nil {
31-
fmt.Fprintf(os.Stderr, "error: While filtering registry data file: %s\n", err)
32-
os.Exit(1)
36+
return fmt.Errorf("while filtering registry data file: %w", err)
3337
}
3438

3539
if !reflect.DeepEqual(rawRepos, filteredRepos) {
36-
fmt.Fprintln(os.Stderr, "error: Registry data file contains duplicate URLs")
37-
os.Exit(1)
40+
return errors.New("registry data file contains duplicate URLs")
3841
}
3942

4043
validTypes := map[string]bool{
@@ -49,21 +52,19 @@ func CheckRegistry(reposFile string) {
4952
for _, entry := range rawRepos {
5053
// Check entry types
5154
if len(entry.Types) == 0 {
52-
fmt.Fprintf(os.Stderr, "error: Type not specified for library '%s'\n", entry.LibraryName)
53-
os.Exit(1)
55+
return fmt.Errorf("type not specified for library '%s'", entry.LibraryName)
5456
}
5557
for _, entryType := range entry.Types {
5658
if _, valid := validTypes[entryType]; !valid {
57-
fmt.Fprintf(os.Stderr, "error: Invalid type '%s' used by library '%s'\n", entryType, entry.LibraryName)
58-
os.Exit(1)
59+
return fmt.Errorf("invalid type '%s' used by library '%s'", entryType, entry.LibraryName)
5960
}
6061
}
6162

6263
// Check library name of the entry
6364
if _, found := nameMap[entry.LibraryName]; found {
64-
fmt.Fprintf(os.Stderr, "error: Registry data file contains duplicates of name '%s'\n", entry.LibraryName)
65-
os.Exit(1)
65+
return fmt.Errorf("registry data file contains duplicates of name '%s'", entry.LibraryName)
6666
}
6767
nameMap[entry.LibraryName] = true
6868
}
69+
return nil
6970
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package checkregistry
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestRegistryValidation(t *testing.T) {
11+
type testcase struct {
12+
Name string
13+
TestFile string
14+
ExpectedResult string
15+
}
16+
tests := []testcase{
17+
{"EmptyArg", "", "registry data file argument testdata is a folder, not a file"},
18+
{"NonExistentFile", "nonexistent.txt", "while loading registry data file: stat testdata/nonexistent.txt: no such file or directory"},
19+
//{"InvalidDataFormat", "invalid-data-format.txt", "while loading registry data file: invalid line format (3 fields are required): https://github.com/arduino-libraries/SD.git|Partner;SD"},
20+
{"InvalidUrlFormat", "invalid-url-format.txt", "while filtering registry data file: Following URL are unknown or unsupported git repos:\nhttps://github.com/arduino-libraries/SD\n"},
21+
{"MissingType", "no-type.txt", "invalid type '' used by library 'SD'"},
22+
{"InvalidType", "invalid-type.txt", "invalid type 'foo' used by library 'SD'"},
23+
{"DuplicateRepoURL", "duplicate-url.txt", "registry data file contains duplicate URLs"},
24+
{"DuplicateLibName", "duplicate-name.txt", "registry data file contains duplicates of name 'SD'"},
25+
{"ValidList", "valid.txt", ""},
26+
}
27+
for _, test := range tests {
28+
t.Run(test.Name, func(t *testing.T) {
29+
err := runcheck(filepath.Join("testdata", test.TestFile))
30+
if test.ExpectedResult == "" {
31+
require.NoError(t, err)
32+
} else {
33+
require.EqualError(t, err, test.ExpectedResult)
34+
}
35+
})
36+
}
37+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD.git|Partner|SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo
4+
https://github.com/arduino-libraries/Foo.git|Contributed|SD
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD.git|Partner|SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo
4+
https://github.com/arduino-libraries/SD.git|Contributed|Foo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD.git|Partner;SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD.git|foo|SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD|Partner|SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD.git||SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/arduino-libraries/Scheduler.git|Arduino|Scheduler
2+
https://github.com/arduino-libraries/SD.git|Partner|SD
3+
https://github.com/arduino-libraries/Servo.git|Recommended|Servo

0 commit comments

Comments
 (0)