mirror of
https://github.com/grdl/git-get.git
synced 2026-02-05 01:29:42 +00:00
Fix a git-list crash on empty repo or repo without a remote
This commit is contained in:
@@ -114,12 +114,11 @@ func TestCurrentBranch(t *testing.T) {
|
||||
repoMaker func(*testing.T) *test.Repo
|
||||
want string
|
||||
}{
|
||||
// TODO: maybe add wantErr to check if error is returned correctly?
|
||||
// {
|
||||
// name: "empty",
|
||||
// repoMaker: newTestRepo,
|
||||
// want: "",
|
||||
// },
|
||||
{
|
||||
name: "empty repo without commits",
|
||||
repoMaker: test.RepoEmpty,
|
||||
want: "main",
|
||||
},
|
||||
{
|
||||
name: "only master branch",
|
||||
repoMaker: test.RepoWithCommit,
|
||||
@@ -359,6 +358,57 @@ func TestCleanupFailedClone(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemote(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
repoMaker func(*testing.T) *test.Repo
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "empty repo without remote",
|
||||
repoMaker: test.RepoEmpty,
|
||||
want: "",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "repo with commit but no remote",
|
||||
repoMaker: test.RepoWithCommit,
|
||||
want: "",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "repo with upstream",
|
||||
repoMaker: test.RepoWithBranchWithUpstream,
|
||||
want: "", // This will contain the actual remote URL but we just test it doesn't error
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
r, _ := Open(test.repoMaker(t).Path())
|
||||
got, err := r.Remote()
|
||||
|
||||
if test.wantErr && err == nil {
|
||||
t.Errorf("expected error but got none")
|
||||
}
|
||||
if !test.wantErr && err != nil {
|
||||
t.Errorf("unexpected error: %q", err)
|
||||
}
|
||||
|
||||
// For repos with remote, just check no error occurred
|
||||
if test.name == "repo with upstream" {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error for repo with remote: %q", err)
|
||||
}
|
||||
} else if got != test.want {
|
||||
t.Errorf("expected %q; got %q", test.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func createTestDirTree(t *testing.T) string {
|
||||
root := test.TempDir(t, "")
|
||||
err := os.MkdirAll(filepath.Join(root, "a", "b", "c"), os.ModePerm)
|
||||
|
||||
Reference in New Issue
Block a user