@@ -4,24 +4,27 @@ import (
4
4
"errors"
5
5
"fmt"
6
6
"net/http"
7
+ "os"
7
8
"testing"
8
9
9
10
"github.com/cli/cli/v2/context"
10
11
"github.com/cli/cli/v2/git"
11
12
"github.com/cli/cli/v2/internal/ghrepo"
12
- "github.com/cli/cli/v2/pkg/httpmock"
13
13
"github.com/cli/cli/v2/pkg/iostreams"
14
14
"github.com/stretchr/testify/assert"
15
15
)
16
16
17
17
func Test_defaultRun (t * testing.T ) {
18
+ setGitDir (t , "../../../../git/fixtures/simple.git" )
18
19
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
23
25
}{
24
26
{
27
+ name : "Base repo set with view option" ,
25
28
opts : DefaultOptions {
26
29
Remotes : func () (context.Remotes , error ) {
27
30
return []* context.Remote {
@@ -39,35 +42,50 @@ func Test_defaultRun(t *testing.T) {
39
42
wantedStdOut : "hubot/Spoon-Knife" ,
40
43
},
41
44
{
45
+ name : "Base repo not set with view option" ,
42
46
opts : DefaultOptions {
43
47
Remotes : func () (context.Remotes , error ) {
44
48
return []* context.Remote {
45
49
{
46
50
Remote : & git.Remote {
47
51
Name : "origin" ,
48
52
},
49
- Repo : ghrepo .New ("hubot" , "Spoon-Knife" ),
50
53
},
51
54
}, nil
52
55
},
53
56
ViewFlag : true ,
54
57
},
55
58
wantedErr : errors .New ("a default repo has not been set, use `gh repo default` to set a default repo" ),
56
59
},
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
+ },
57
80
}
58
81
59
82
for _ , tt := range tests {
60
83
t .Run (tt .name , func (t * testing.T ) {
61
84
io , _ , stdout , stderr := iostreams .Test ()
62
85
63
- reg := httpmock.Registry {}
64
- defer reg .Verify (t )
65
-
66
86
opts := tt .opts
67
87
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 }
71
89
72
90
err := runDefault (& opts )
73
91
if tt .wantedErr != nil {
@@ -81,6 +99,29 @@ func Test_defaultRun(t *testing.T) {
81
99
assert .Equal (t , "" , stderr .String ())
82
100
}
83
101
}
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
+ }
84
116
})
85
117
}
86
118
}
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