Skip to content

Commit 53a8751

Browse files
committed
Added unit tests from library-registry repo
1 parent 14e1576 commit 53a8751

File tree

9 files changed

+100
-16
lines changed

9 files changed

+100
-16
lines changed

internal/cli/check-registry/check-registry.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package checkregistry
2525

2626
import (
27+
"errors"
2728
"fmt"
2829
"os"
2930
"reflect"
@@ -33,32 +34,34 @@ import (
3334

3435
// CheckRegistry runs the check-registry action
3536
func CheckRegistry(reposFile string) {
37+
if err := runcheck(reposFile); err != nil {
38+
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
39+
os.Exit(1)
40+
}
41+
}
42+
43+
func runcheck(reposFile string) error {
3644
info, err := os.Stat(reposFile)
3745
if err != nil {
38-
fmt.Fprintf(os.Stderr, "error: While loading registry data file: %s\n", err)
39-
os.Exit(1)
46+
return fmt.Errorf("while loading registry data file: %w", err)
4047
}
4148

4249
if info.IsDir() {
43-
fmt.Fprintf(os.Stderr, "error: Registry data file argument %s is a folder, not a file\n", reposFile)
44-
os.Exit(1)
50+
return fmt.Errorf("registry data file argument %s is a folder, not a file", reposFile)
4551
}
4652

4753
rawRepos, err := libraries.LoadRepoListFromFile(reposFile)
4854
if err != nil {
49-
fmt.Fprintf(os.Stderr, "error: While loading registry data file: %s\n", err)
50-
os.Exit(1)
55+
return fmt.Errorf("while loading registry data file: %w", err)
5156
}
5257

5358
filteredRepos, err := libraries.ListRepos(reposFile)
5459
if err != nil {
55-
fmt.Fprintf(os.Stderr, "error: While filtering registry data file: %s\n", err)
56-
os.Exit(1)
60+
return fmt.Errorf("while filtering registry data file: %w", err)
5761
}
5862

5963
if !reflect.DeepEqual(rawRepos, filteredRepos) {
60-
fmt.Fprintln(os.Stderr, "error: Registry data file contains duplicate URLs")
61-
os.Exit(1)
64+
return errors.New("registry data file contains duplicate URLs")
6265
}
6366

6467
validTypes := map[string]bool{
@@ -73,21 +76,19 @@ func CheckRegistry(reposFile string) {
7376
for _, entry := range rawRepos {
7477
// Check entry types
7578
if len(entry.Types) == 0 {
76-
fmt.Fprintf(os.Stderr, "error: Type not specified for library '%s'\n", entry.LibraryName)
77-
os.Exit(1)
79+
return fmt.Errorf("type not specified for library '%s'", entry.LibraryName)
7880
}
7981
for _, entryType := range entry.Types {
8082
if _, valid := validTypes[entryType]; !valid {
81-
fmt.Fprintf(os.Stderr, "error: Invalid type '%s' used by library '%s'\n", entryType, entry.LibraryName)
82-
os.Exit(1)
83+
return fmt.Errorf("invalid type '%s' used by library '%s'", entryType, entry.LibraryName)
8384
}
8485
}
8586

8687
// Check library name of the entry
8788
if _, found := nameMap[entry.LibraryName]; found {
88-
fmt.Fprintf(os.Stderr, "error: Registry data file contains duplicates of name '%s'\n", entry.LibraryName)
89-
os.Exit(1)
89+
return fmt.Errorf("registry data file contains duplicates of name '%s'", entry.LibraryName)
9090
}
9191
nameMap[entry.LibraryName] = true
9292
}
93+
return nil
9394
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// This file is part of libraries-repository-engine.
2+
//
3+
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
//
18+
// You can be released from the requirements of the above licenses by purchasing
19+
// a commercial license. Buying such a license is mandatory if you want to
20+
// modify or otherwise use the software for commercial activities involving the
21+
// Arduino software without disclosing the source code of your own applications.
22+
// To purchase a commercial license, send an email to license@arduino.cc.
23+
24+
package checkregistry
25+
26+
import (
27+
"path/filepath"
28+
"testing"
29+
30+
"github.com/stretchr/testify/require"
31+
)
32+
33+
func TestRegistryValidation(t *testing.T) {
34+
type testcase struct {
35+
Name string
36+
TestFile string
37+
ExpectedResult string
38+
}
39+
tests := []testcase{
40+
{"EmptyArg", "", "registry data file argument testdata is a folder, not a file"},
41+
{"NonExistentFile", "nonexistent.txt", "while loading registry data file: stat testdata/nonexistent.txt: no such file or directory"},
42+
//{"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"},
43+
{"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"},
44+
{"MissingType", "no-type.txt", "invalid type '' used by library 'SD'"},
45+
{"InvalidType", "invalid-type.txt", "invalid type 'foo' used by library 'SD'"},
46+
{"DuplicateRepoURL", "duplicate-url.txt", "registry data file contains duplicate URLs"},
47+
{"DuplicateLibName", "duplicate-name.txt", "registry data file contains duplicates of name 'SD'"},
48+
{"ValidList", "valid.txt", ""},
49+
}
50+
for _, test := range tests {
51+
t.Run(test.Name, func(t *testing.T) {
52+
err := runcheck(filepath.Join("testdata", test.TestFile))
53+
if test.ExpectedResult == "" {
54+
require.NoError(t, err)
55+
} else {
56+
require.EqualError(t, err, test.ExpectedResult)
57+
}
58+
})
59+
}
60+
}
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)