On first run in a git repository, `BaseRepo()` will now prompt the user which repository should be queried as base repository if there are multiple git remotes or when we are in the context of a fork. In non-interactive mode, the prompt is skipped and we default to the first git remote instead. After the base repo is resolved, the result is cached in the local repository using `git config` so that RepositoryNetwork API lookups can be avoided in the future.
68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package context
|
|
|
|
import (
|
|
"errors"
|
|
"net/url"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/cli/cli/git"
|
|
"github.com/cli/cli/internal/ghrepo"
|
|
)
|
|
|
|
func eq(t *testing.T, got interface{}, expected interface{}) {
|
|
t.Helper()
|
|
if !reflect.DeepEqual(got, expected) {
|
|
t.Errorf("expected: %v, got: %v", expected, got)
|
|
}
|
|
}
|
|
|
|
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")
|
|
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())
|
|
}
|
|
}
|