github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/context/remote_test.go (about) 1 package context 2 3 import ( 4 "net/url" 5 "testing" 6 7 "github.com/cli/cli/git" 8 "github.com/cli/cli/internal/ghrepo" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func Test_Remotes_FindByName(t *testing.T) { 13 list := Remotes{ 14 &Remote{Remote: &git.Remote{Name: "mona"}, Repo: ghrepo.New("monalisa", "myfork")}, 15 &Remote{Remote: &git.Remote{Name: "origin"}, Repo: ghrepo.New("monalisa", "octo-cat")}, 16 &Remote{Remote: &git.Remote{Name: "upstream"}, Repo: ghrepo.New("hubot", "tools")}, 17 } 18 19 r, err := list.FindByName("upstream", "origin") 20 assert.NoError(t, err) 21 assert.Equal(t, "upstream", r.Name) 22 23 r, err = list.FindByName("nonexistent", "*") 24 assert.NoError(t, err) 25 assert.Equal(t, "mona", r.Name) 26 27 _, err = list.FindByName("nonexistent") 28 assert.Error(t, err, "no GitHub remotes found") 29 } 30 31 func Test_translateRemotes(t *testing.T) { 32 publicURL, _ := url.Parse("https://github.com/monalisa/hello") 33 originURL, _ := url.Parse("http://example.com/repo") 34 35 gitRemotes := git.RemoteSet{ 36 &git.Remote{ 37 Name: "origin", 38 FetchURL: originURL, 39 }, 40 &git.Remote{ 41 Name: "public", 42 FetchURL: publicURL, 43 }, 44 } 45 46 identityURL := func(u *url.URL) *url.URL { 47 return u 48 } 49 result := TranslateRemotes(gitRemotes, identityURL) 50 51 if len(result) != 1 { 52 t.Errorf("got %d results", len(result)) 53 } 54 if result[0].Name != "public" { 55 t.Errorf("got %q", result[0].Name) 56 } 57 if result[0].RepoName() != "hello" { 58 t.Errorf("got %q", result[0].RepoName()) 59 } 60 } 61 62 func Test_FilterByHosts(t *testing.T) { 63 r1 := &Remote{Remote: &git.Remote{Name: "mona"}, Repo: ghrepo.NewWithHost("monalisa", "myfork", "test.com")} 64 r2 := &Remote{Remote: &git.Remote{Name: "origin"}, Repo: ghrepo.NewWithHost("monalisa", "octo-cat", "example.com")} 65 r3 := &Remote{Remote: &git.Remote{Name: "upstream"}, Repo: ghrepo.New("hubot", "tools")} 66 list := Remotes{r1, r2, r3} 67 f := list.FilterByHosts([]string{"example.com", "test.com"}) 68 assert.Equal(t, 2, len(f)) 69 assert.Equal(t, r1, f[0]) 70 assert.Equal(t, r2, f[1]) 71 }