Skip to content

Commit 31bc32a

Browse files
committed
Add function for checking whether a repository URL has the right format
The existing code for doing this was exceptionally convoluted, so rather than exporting the components of that interface, I added a simple function to serve as the exported interface for this functionality of the package.
1 parent 1e261a1 commit 31bc32a

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

internal/libraries/repolist.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ func (repoMatcherIfDotGit) Match(url string) bool {
7474
return strings.Index(url, "https://") == 0 && strings.LastIndex(url, ".git") == len(url)-len(".git")
7575
}
7676

77+
// RepoURLValid returns whether the given URL has a valid format.
78+
func RepoURLValid(url string) bool {
79+
return repoMatcherIfDotGit{}.Match(url)
80+
}
81+
7782
// GitURLsError is the type for the unknown or unsupported repositories data.
7883
type GitURLsError struct {
7984
Repos []*Repo

internal/libraries/repolist_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,30 @@
2424
package libraries
2525

2626
import (
27+
"fmt"
2728
"testing"
2829

30+
"github.com/stretchr/testify/assert"
2931
"github.com/stretchr/testify/require"
3032
)
3133

34+
func TestRepoURLValid(t *testing.T) {
35+
testTables := []struct {
36+
url string
37+
assertion assert.BoolAssertionFunc
38+
}{
39+
{"example.com", assert.False},
40+
{"example.com/foo.git", assert.False},
41+
{"http://example.com/foo.git", assert.False},
42+
{"https://example.com/foo", assert.False},
43+
{"https://example/com/foo.git", assert.True},
44+
}
45+
46+
for _, testTable := range testTables {
47+
testTable.assertion(t, RepoURLValid(testTable.url), fmt.Sprintf("URL: %s", testTable.url))
48+
}
49+
}
50+
3251
func TestRepoFolderPathDetermination(t *testing.T) {
3352
repo := &Repo{URL: "https://github.com/arduino-libraries/Servo.git"}
3453
f, err := repo.AsFolder()

0 commit comments

Comments
 (0)