Skip to content

Commit 8665851

Browse files
bchadwicsamcoe
authored andcommitted
Created more test cases, added in env section
1 parent 08b02da commit 8665851

File tree

3 files changed

+74
-13
lines changed

3 files changed

+74
-13
lines changed

git/fixtures/simple.git/config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@
77
[user]
88
name = Mona the Cat
99
email = monalisa@github.com
10+
[remote "origin"]
11+
url = git@github.com:monathecat/cli.git
12+
fetch = +refs/heads/*:refs/remotes/origin/*
13+
[remote "upstream"]
14+
url = git@github.com:cli/cli.git
15+
fetch = +refs/heads/trunk:refs/remotes/upstream/trunk

pkg/cmd/repo/default/default.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,22 @@ func NewCmdDefault(f *cmdutil.Factory) *cobra.Command {
3636
repository gh should automatically point to.
3737
`),
3838
Example: heredoc.Doc(`
39-
$ gh repo default cli/cli
39+
$ gh repo default
40+
#=> prompts remote options
41+
42+
$ gh repo default -v
43+
#=> cli/cli
4044
`),
45+
Annotations: map[string]string{
46+
"help:environment": heredoc.Doc(`
47+
To manually configure a remote for gh to use, modify your local repo's git config
48+
49+
; Ex: setting gh to use the upstream remote
50+
[remote "upstream"]
51+
gh-resolved = base
52+
...
53+
`),
54+
},
4155
Args: cobra.NoArgs,
4256
RunE: func(cmd *cobra.Command, args []string) error {
4357
return runDefault(opts)

pkg/cmd/repo/default/default_test.go

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,27 @@ import (
44
"errors"
55
"fmt"
66
"net/http"
7+
"os"
78
"testing"
89

910
"github.com/cli/cli/v2/context"
1011
"github.com/cli/cli/v2/git"
1112
"github.com/cli/cli/v2/internal/ghrepo"
12-
"github.com/cli/cli/v2/pkg/httpmock"
1313
"github.com/cli/cli/v2/pkg/iostreams"
1414
"github.com/stretchr/testify/assert"
1515
)
1616

1717
func Test_defaultRun(t *testing.T) {
18+
setGitDir(t, "../../../../git/fixtures/simple.git")
1819
tests := []struct {
19-
name string
20-
opts DefaultOptions
21-
wantedErr error
22-
wantedStdOut string
20+
name string
21+
opts DefaultOptions
22+
wantedErr error
23+
wantedStdOut string
24+
wantedResolvedName string
2325
}{
2426
{
27+
name: "Base repo set with view option",
2528
opts: DefaultOptions{
2629
Remotes: func() (context.Remotes, error) {
2730
return []*context.Remote{
@@ -39,35 +42,50 @@ func Test_defaultRun(t *testing.T) {
3942
wantedStdOut: "hubot/Spoon-Knife",
4043
},
4144
{
45+
name: "Base repo not set with view option",
4246
opts: DefaultOptions{
4347
Remotes: func() (context.Remotes, error) {
4448
return []*context.Remote{
4549
{
4650
Remote: &git.Remote{
4751
Name: "origin",
4852
},
49-
Repo: ghrepo.New("hubot", "Spoon-Knife"),
5053
},
5154
}, nil
5255
},
5356
ViewFlag: true,
5457
},
5558
wantedErr: errors.New("a default repo has not been set, use `gh repo default` to set a default repo"),
5659
},
60+
{
61+
name: "Base repo not set, assign non-interactively",
62+
opts: DefaultOptions{
63+
Remotes: func() (context.Remotes, error) {
64+
return []*context.Remote{
65+
{
66+
Remote: &git.Remote{
67+
Name: "origin",
68+
},
69+
},
70+
{
71+
Remote: &git.Remote{
72+
Name: "upstream",
73+
},
74+
},
75+
}, nil
76+
},
77+
},
78+
wantedResolvedName: "upstream",
79+
},
5780
}
5881

5982
for _, tt := range tests {
6083
t.Run(tt.name, func(t *testing.T) {
6184
io, _, stdout, stderr := iostreams.Test()
6285

63-
reg := httpmock.Registry{}
64-
defer reg.Verify(t)
65-
6686
opts := tt.opts
6787
opts.IO = io
68-
opts.HttpClient = func() (*http.Client, error) {
69-
return &http.Client{Transport: &reg}, nil
70-
}
88+
opts.HttpClient = func() (*http.Client, error) { return nil, nil }
7189

7290
err := runDefault(&opts)
7391
if tt.wantedErr != nil {
@@ -81,6 +99,29 @@ func Test_defaultRun(t *testing.T) {
8199
assert.Equal(t, "", stderr.String())
82100
}
83101
}
102+
if tt.wantedResolvedName != "" {
103+
resolvedAmount := 0
104+
remotes, err := git.Remotes()
105+
if err != nil {
106+
panic(err)
107+
}
108+
for _, r := range remotes {
109+
if r.Resolved == "base" {
110+
assert.Equal(t, r.Name, tt.wantedResolvedName)
111+
resolvedAmount++
112+
}
113+
}
114+
assert.Equal(t, 1, resolvedAmount)
115+
}
84116
})
85117
}
86118
}
119+
120+
func setGitDir(t *testing.T, dir string) {
121+
old_GIT_DIR := os.Getenv("GIT_DIR")
122+
os.Setenv("GIT_DIR", dir)
123+
t.Cleanup(func() {
124+
git.UnsetRemoteResolution("upstream")
125+
os.Setenv("GIT_DIR", old_GIT_DIR)
126+
})
127+
}

0 commit comments

Comments
 (0)