github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/context/remote_test.go (about) 1 package context 2 3 import ( 4 "net/url" 5 "testing" 6 7 "github.com/ungtb10d/cli/v2/git" 8 "github.com/ungtb10d/cli/v2/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 type identityTranslator struct{} 32 33 func (it identityTranslator) Translate(u *url.URL) *url.URL { 34 return u 35 } 36 37 func Test_translateRemotes(t *testing.T) { 38 publicURL, _ := url.Parse("https://github.com/monalisa/hello") 39 originURL, _ := url.Parse("http://example.com/repo") 40 41 gitRemotes := git.RemoteSet{ 42 &git.Remote{ 43 Name: "origin", 44 FetchURL: originURL, 45 }, 46 &git.Remote{ 47 Name: "public", 48 FetchURL: publicURL, 49 }, 50 } 51 52 result := TranslateRemotes(gitRemotes, identityTranslator{}) 53 54 if len(result) != 1 { 55 t.Errorf("got %d results", len(result)) 56 } 57 if result[0].Name != "public" { 58 t.Errorf("got %q", result[0].Name) 59 } 60 if result[0].RepoName() != "hello" { 61 t.Errorf("got %q", result[0].RepoName()) 62 } 63 } 64 65 func Test_FilterByHosts(t *testing.T) { 66 r1 := &Remote{Remote: &git.Remote{Name: "mona"}, Repo: ghrepo.NewWithHost("monalisa", "myfork", "test.com")} 67 r2 := &Remote{Remote: &git.Remote{Name: "origin"}, Repo: ghrepo.NewWithHost("monalisa", "octo-cat", "example.com")} 68 r3 := &Remote{Remote: &git.Remote{Name: "upstream"}, Repo: ghrepo.New("hubot", "tools")} 69 list := Remotes{r1, r2, r3} 70 f := list.FilterByHosts([]string{"example.com", "test.com"}) 71 assert.Equal(t, 2, len(f)) 72 assert.Equal(t, r1, f[0]) 73 assert.Equal(t, r2, f[1]) 74 }