X Tutup
Skip to content

Commit a8aa5fe

Browse files
committed
Test remote parsing
1 parent 8016d80 commit a8aa5fe

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

context/context.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ func (c *fsContext) SetBranch(b string) {
9494

9595
func (c *fsContext) Remotes() (Remotes, error) {
9696
if c.remotes == nil {
97-
rem, err := parseRemotes()
97+
gitRemotes, err := git.Remotes()
98+
if err != nil {
99+
return nil, err
100+
}
101+
rem, err := parseRemotes(gitRemotes)
98102
if err != nil {
99103
return nil, err
100104
}

context/remote.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,9 @@ type GitHubRepository struct {
4242
Owner string
4343
}
4444

45-
func parseRemotes() (remotes Remotes, err error) {
45+
func parseRemotes(gitRemotes []string) (remotes Remotes, err error) {
4646
re := regexp.MustCompile(`(.+)\s+(.+)\s+\((push|fetch)\)`)
4747

48-
gitRemotes, err := git.Remotes()
49-
if err != nil {
50-
return
51-
}
52-
5348
remotesMap := make(map[string]map[string]string)
5449
for _, r := range gitRemotes {
5550
if re.MatchString(r) {

context/remote_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,41 @@ func Test_repoFromURL(t *testing.T) {
1616
t.Errorf("got Name: %q", r.Name)
1717
}
1818
}
19+
20+
func Test_parseRemotes(t *testing.T) {
21+
remoteList := []string{
22+
"mona\tgit@github.com:monalisa/myfork.git (fetch)",
23+
"origin\thttps://github.com/monalisa/octo-cat.git (fetch)",
24+
"origin\thttps://github.com/monalisa/octo-cat-push.git (push)",
25+
"upstream\thttps://example.com/nowhere.git (fetch)",
26+
"upstream\thttps://github.com/hubot/tools (push)",
27+
}
28+
r, err := parseRemotes(remoteList)
29+
if err != nil {
30+
t.Error(err)
31+
}
32+
33+
mona, err := r.FindByName("mona")
34+
if err != nil {
35+
t.Error(err)
36+
}
37+
if mona.Owner != "monalisa" || mona.Repo != "myfork" {
38+
t.Errorf("got %s/%s", mona.Owner, mona.Repo)
39+
}
40+
41+
origin, err := r.FindByName("origin")
42+
if err != nil {
43+
t.Error(err)
44+
}
45+
if origin.Owner != "monalisa" || origin.Repo != "octo-cat" {
46+
t.Errorf("got %s/%s", origin.Owner, origin.Repo)
47+
}
48+
49+
upstream, err := r.FindByName("upstream")
50+
if err != nil {
51+
t.Error(err)
52+
}
53+
if upstream.Owner != "hubot" || upstream.Repo != "tools" {
54+
t.Errorf("got %s/%s", upstream.Owner, upstream.Repo)
55+
}
56+
}

0 commit comments

Comments
 (0)
X Tutup