forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupgit_test.go
More file actions
206 lines (186 loc) · 5.47 KB
/
setupgit_test.go
File metadata and controls
206 lines (186 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package setupgit
import (
"bytes"
"errors"
"fmt"
"io"
"testing"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/gh"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/google/shlex"
"github.com/stretchr/testify/require"
)
type gitCredentialsConfigurerSpy struct {
hosts []string
setupErr error
}
func (gf *gitCredentialsConfigurerSpy) ConfigureOurs(hostname string) error {
gf.hosts = append(gf.hosts, hostname)
return gf.setupErr
}
func TestNewCmdSetupGit(t *testing.T) {
tests := []struct {
name string
cli string
wantsErr bool
errMsg string
}{
{
name: "--force without hostname",
cli: "--force",
wantsErr: true,
errMsg: "`--force` must be used in conjunction with `--hostname`",
},
{
name: "no error when --force used with hostname",
cli: "--force --hostname ghe.io",
wantsErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &cmdutil.Factory{}
argv, err := shlex.Split(tt.cli)
require.NoError(t, err)
cmd := NewCmdSetupGit(f, func(opts *SetupGitOptions) error {
return nil
})
// TODO cobra hack-around
cmd.Flags().BoolP("help", "x", false, "")
cmd.SetArgs(argv)
cmd.SetIn(&bytes.Buffer{})
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
_, err = cmd.ExecuteC()
if tt.wantsErr {
require.Error(t, err)
require.Equal(t, err.Error(), tt.errMsg)
return
}
require.NoError(t, err)
})
}
}
func Test_setupGitRun(t *testing.T) {
tests := []struct {
name string
opts *SetupGitOptions
setupErr error
cfgStubs func(*testing.T, gh.Config)
expectedHostsSetup []string
expectedErr error
expectedErrOut string
}{
{
name: "opts.Config returns an error",
opts: &SetupGitOptions{
Config: func() (gh.Config, error) {
return nil, fmt.Errorf("oops")
},
},
expectedErr: errors.New("oops"),
},
{
name: "when an unknown hostname is provided without forcing, return an error",
opts: &SetupGitOptions{
Hostname: "ghe.io",
},
cfgStubs: func(t *testing.T, cfg gh.Config) {
login(t, cfg, "github.com", "test-user", "gho_ABCDEFG", "https", false)
},
expectedErr: errors.New("You are not logged into the GitHub host \"ghe.io\". Run gh auth login -h ghe.io to authenticate or provide `--force`"),
},
{
name: "when an unknown hostname is provided with forcing, set it up",
opts: &SetupGitOptions{
Hostname: "ghe.io",
Force: true,
},
expectedHostsSetup: []string{"ghe.io"},
},
{
name: "when a known hostname is provided without forcing, set it up",
opts: &SetupGitOptions{
Hostname: "ghe.io",
},
cfgStubs: func(t *testing.T, cfg gh.Config) {
login(t, cfg, "ghe.io", "test-user", "gho_ABCDEFG", "https", false)
},
expectedHostsSetup: []string{"ghe.io"},
},
{
name: "when a hostname is provided but setting it up errors, that error is bubbled",
opts: &SetupGitOptions{
Hostname: "ghe.io",
},
setupErr: fmt.Errorf("broken"),
cfgStubs: func(t *testing.T, cfg gh.Config) {
login(t, cfg, "ghe.io", "test-user", "gho_ABCDEFG", "https", false)
},
expectedErr: errors.New("failed to set up git credential helper: broken"),
expectedErrOut: "",
},
{
name: "when there are no known hosts and no hostname is provided, return an error",
opts: &SetupGitOptions{},
expectedErr: cmdutil.SilentError,
expectedErrOut: "You are not logged into any GitHub hosts. Run gh auth login to authenticate.\n",
},
{
name: "when there are known hosts, and no hostname is provided, set them all up",
opts: &SetupGitOptions{},
cfgStubs: func(t *testing.T, cfg gh.Config) {
login(t, cfg, "ghe.io", "test-user", "gho_ABCDEFG", "https", false)
login(t, cfg, "github.com", "test-user", "gho_ABCDEFG", "https", false)
},
expectedHostsSetup: []string{"github.com", "ghe.io"},
},
{
name: "when no hostname is provided but setting one up errors, that error is bubbled",
opts: &SetupGitOptions{},
setupErr: fmt.Errorf("broken"),
cfgStubs: func(t *testing.T, cfg gh.Config) {
login(t, cfg, "ghe.io", "test-user", "gho_ABCDEFG", "https", false)
},
expectedErr: errors.New("failed to set up git credential helper: broken"),
expectedErrOut: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ios, _, _, stderr := iostreams.Test()
ios.SetStdinTTY(true)
ios.SetStderrTTY(true)
ios.SetStdoutTTY(true)
tt.opts.IO = ios
cfg, _ := config.NewIsolatedTestConfig(t)
if tt.cfgStubs != nil {
tt.cfgStubs(t, cfg)
}
if tt.opts.Config == nil {
tt.opts.Config = func() (gh.Config, error) {
return cfg, nil
}
}
credentialsConfigurerSpy := &gitCredentialsConfigurerSpy{setupErr: tt.setupErr}
tt.opts.CredentialsHelperConfig = credentialsConfigurerSpy
err := setupGitRun(tt.opts)
if tt.expectedErr != nil {
require.Equal(t, err, tt.expectedErr)
} else {
require.NoError(t, err)
}
if tt.expectedHostsSetup != nil {
require.Equal(t, tt.expectedHostsSetup, credentialsConfigurerSpy.hosts)
}
require.Equal(t, tt.expectedErrOut, stderr.String())
})
}
}
func login(t *testing.T, c gh.Config, hostname, username, token, gitProtocol string, secureStorage bool) {
t.Helper()
_, err := c.Authentication().Login(hostname, username, token, gitProtocol, secureStorage)
require.NoError(t, err)
}