cli/context/remote_test.go
Mislav Marohnić 8453bf679c Fix parsing non-GitHub remotes
Otherwise, the remote URL translation mechanism had crashed when
encountering a non-Github.com git remote.
2020-01-28 19:51:44 +01:00

59 lines
1.3 KiB
Go

package context
import (
"errors"
"net/url"
"testing"
"github.com/cli/cli/git"
)
func Test_Remotes_FindByName(t *testing.T) {
list := Remotes{
&Remote{Remote: &git.Remote{Name: "mona"}, Owner: "monalisa", Repo: "myfork"},
&Remote{Remote: &git.Remote{Name: "origin"}, Owner: "monalisa", Repo: "octo-cat"},
&Remote{Remote: &git.Remote{Name: "upstream"}, Owner: "hubot", Repo: "tools"},
}
r, err := list.FindByName("upstream", "origin")
eq(t, err, nil)
eq(t, r.Name, "upstream")
r, err = list.FindByName("nonexist", "*")
eq(t, err, nil)
eq(t, r.Name, "mona")
_, err = list.FindByName("nonexist")
eq(t, err, errors.New(`no GitHub remotes found`))
}
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,
},
}
identityURL := func(u *url.URL) *url.URL {
return u
}
result := translateRemotes(gitRemotes, identityURL)
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())
}
}