forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_test.go
139 lines (123 loc) · 3.61 KB
/
remote_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package context
import (
"net/url"
"testing"
"github.com/cli/cli/v2/git"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/stretchr/testify/assert"
)
func Test_Remotes_FindByName(t *testing.T) {
list := Remotes{
&Remote{Remote: &git.Remote{Name: "mona"}, Repo: ghrepo.New("monalisa", "myfork")},
&Remote{Remote: &git.Remote{Name: "origin"}, Repo: ghrepo.New("monalisa", "octo-cat")},
&Remote{Remote: &git.Remote{Name: "upstream"}, Repo: ghrepo.New("hubot", "tools")},
}
r, err := list.FindByName("upstream", "origin")
assert.NoError(t, err)
assert.Equal(t, "upstream", r.Name)
r, err = list.FindByName("nonexistent", "*")
assert.NoError(t, err)
assert.Equal(t, "mona", r.Name)
_, err = list.FindByName("nonexistent")
assert.Error(t, err, "no GitHub remotes found")
}
func Test_Remotes_FindByRepo(t *testing.T) {
list := Remotes{
&Remote{Remote: &git.Remote{Name: "remote-0"}, Repo: ghrepo.New("owner", "repo")},
&Remote{Remote: &git.Remote{Name: "remote-1"}, Repo: ghrepo.New("another-owner", "another-repo")},
}
tests := []struct {
name string
owner string
repo string
wantsRemote *Remote
wantsError string
}{
{
name: "exact match (owner/repo)",
owner: "owner",
repo: "repo",
wantsRemote: list[0],
},
{
name: "exact match (another-owner/another-repo)",
owner: "another-owner",
repo: "another-repo",
wantsRemote: list[1],
},
{
name: "case-insensitive match",
owner: "OWNER",
repo: "REPO",
wantsRemote: list[0],
},
{
name: "non-match (owner)",
owner: "unknown-owner",
repo: "repo",
wantsError: "no matching remote found; looking for unknown-owner/repo",
},
{
name: "non-match (repo)",
owner: "owner",
repo: "unknown-repo",
wantsError: "no matching remote found; looking for owner/unknown-repo",
},
{
name: "non-match (owner, repo)",
owner: "unknown-owner",
repo: "unknown-repo",
wantsError: "no matching remote found; looking for unknown-owner/unknown-repo",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r, err := list.FindByRepo(tt.owner, tt.repo)
if tt.wantsError != "" {
assert.Error(t, err, tt.wantsError)
assert.Nil(t, r)
} else {
assert.NoError(t, err)
assert.Equal(t, r, tt.wantsRemote)
}
})
}
}
type identityTranslator struct{}
func (it identityTranslator) Translate(u *url.URL) *url.URL {
return u
}
func Test_translateRemotes(t *testing.T) {
publicURL, _ := url.Parse("https://github.com/monalisa/hello")
originURL, _ := url.Parse("http://example.com/repo")
gitRemotes := git.RemoteSet{
&git.Remote{
Name: "origin",
FetchURL: originURL,
},
&git.Remote{
Name: "public",
FetchURL: publicURL,
},
}
result := TranslateRemotes(gitRemotes, identityTranslator{})
if len(result) != 1 {
t.Errorf("got %d results", len(result))
}
if result[0].Name != "public" {
t.Errorf("got %q", result[0].Name)
}
if result[0].RepoName() != "hello" {
t.Errorf("got %q", result[0].RepoName())
}
}
func Test_FilterByHosts(t *testing.T) {
r1 := &Remote{Remote: &git.Remote{Name: "mona"}, Repo: ghrepo.NewWithHost("monalisa", "myfork", "test.com")}
r2 := &Remote{Remote: &git.Remote{Name: "origin"}, Repo: ghrepo.NewWithHost("monalisa", "octo-cat", "example.com")}
r3 := &Remote{Remote: &git.Remote{Name: "upstream"}, Repo: ghrepo.New("hubot", "tools")}
list := Remotes{r1, r2, r3}
f := list.FilterByHosts([]string{"example.com", "test.com"})
assert.Equal(t, 2, len(f))
assert.Equal(t, r1, f[0])
assert.Equal(t, r2, f[1])
}