-
Notifications
You must be signed in to change notification settings - Fork 47
/
gh_test.go
319 lines (289 loc) · 7.42 KB
/
gh_test.go
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package gh
import (
"fmt"
"net/http"
"os"
"strings"
"testing"
"github.com/cli/go-gh/pkg/api"
"github.com/cli/go-gh/pkg/config"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
func TestHelperProcess(t *testing.T) {
if os.Getenv("GH_WANT_HELPER_PROCESS") != "1" {
return
}
if err := func(args []string) error {
if args[len(args)-1] == "error" {
return fmt.Errorf("process exited with error")
}
fmt.Fprintf(os.Stdout, "%v", args)
return nil
}(os.Args[3:]); err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}
func TestRun(t *testing.T) {
stdOut, stdErr, err := run(os.Args[0],
[]string{"GH_WANT_HELPER_PROCESS=1"},
"-test.run=TestHelperProcess", "--", "gh", "issue", "list")
assert.NoError(t, err)
assert.Equal(t, "[gh issue list]", stdOut.String())
assert.Equal(t, "", stdErr.String())
}
func TestRunError(t *testing.T) {
stdOut, stdErr, err := run(os.Args[0],
[]string{"GH_WANT_HELPER_PROCESS=1"},
"-test.run=TestHelperProcess", "--", "gh", "issue", "list", "error")
assert.EqualError(t, err, "failed to run gh: process exited with error. error: exit status 1")
assert.Equal(t, "", stdOut.String())
assert.Equal(t, "process exited with error", stdErr.String())
}
func TestRESTClient(t *testing.T) {
stubConfig(t, testConfig())
t.Cleanup(gock.Off)
gock.New("https://api.github.com").
Get("/some/test/path").
MatchHeader("Authorization", "token abc123").
Reply(200).
JSON(`{"message": "success"}`)
client, err := RESTClient(nil)
assert.NoError(t, err)
res := struct{ Message string }{}
err = client.Do("GET", "some/test/path", nil, &res)
assert.NoError(t, err)
assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending()))
assert.Equal(t, "success", res.Message)
}
func TestGQLClient(t *testing.T) {
stubConfig(t, testConfig())
t.Cleanup(gock.Off)
gock.New("https://api.github.com").
Post("/graphql").
MatchHeader("Authorization", "token abc123").
BodyString(`{"query":"QUERY","variables":{"var":"test"}}`).
Reply(200).
JSON(`{"data":{"viewer":{"login":"hubot"}}}`)
client, err := GQLClient(nil)
assert.NoError(t, err)
vars := map[string]interface{}{"var": "test"}
res := struct{ Viewer struct{ Login string } }{}
err = client.Do("QUERY", vars, &res)
assert.NoError(t, err)
assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending()))
assert.Equal(t, "hubot", res.Viewer.Login)
}
func TestGQLClientError(t *testing.T) {
stubConfig(t, testConfig())
t.Cleanup(gock.Off)
gock.New("https://api.github.com").
Post("/graphql").
MatchHeader("Authorization", "token abc123").
BodyString(`{"query":"QUERY","variables":null}`).
Reply(200).
JSON(`{"errors":[{"type":"NOT_FOUND","path":["organization"],"message":"Could not resolve to an Organization with the login of 'cli'."}]}`)
client, err := GQLClient(nil)
assert.NoError(t, err)
res := struct{ Organization struct{ Name string } }{}
err = client.Do("QUERY", nil, &res)
assert.EqualError(t, err, "GraphQL: Could not resolve to an Organization with the login of 'cli'. (organization)")
assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending()))
}
func TestHTTPClient(t *testing.T) {
stubConfig(t, testConfig())
t.Cleanup(gock.Off)
gock.New("https://api.github.com").
Get("/some/test/path").
MatchHeader("Authorization", "token abc123").
Reply(200).
JSON(`{"message": "success"}`)
client, err := HTTPClient(nil)
assert.NoError(t, err)
res, err := client.Get("https://api.github.com/some/test/path")
assert.NoError(t, err)
assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending()))
assert.Equal(t, 200, res.StatusCode)
}
func TestResolveOptions(t *testing.T) {
stubConfig(t, testConfigWithSocket())
tests := []struct {
name string
opts *api.ClientOptions
wantAuthToken string
wantHost string
wantSocket string
}{
{
name: "honors consumer provided ClientOptions",
opts: &api.ClientOptions{
Host: "test.com",
AuthToken: "token_from_opts",
UnixDomainSocket: "socket_from_opts",
},
wantAuthToken: "token_from_opts",
wantHost: "test.com",
wantSocket: "socket_from_opts",
},
{
name: "uses config values if there are no consumer provided ClientOptions",
opts: &api.ClientOptions{},
wantAuthToken: "token",
wantHost: "github.com",
wantSocket: "socket",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := resolveOptions(tt.opts)
assert.NoError(t, err)
assert.Equal(t, tt.wantHost, tt.opts.Host)
assert.Equal(t, tt.wantAuthToken, tt.opts.AuthToken)
assert.Equal(t, tt.wantSocket, tt.opts.UnixDomainSocket)
})
}
}
func TestOptionsNeedResolution(t *testing.T) {
tests := []struct {
name string
opts *api.ClientOptions
out bool
}{
{
name: "Host, AuthToken, and UnixDomainSocket specified",
opts: &api.ClientOptions{
Host: "test.com",
AuthToken: "token",
UnixDomainSocket: "socket",
},
out: false,
},
{
name: "Host, AuthToken, and Transport specified",
opts: &api.ClientOptions{
Host: "test.com",
AuthToken: "token",
Transport: http.DefaultTransport,
},
out: false,
},
{
name: "Host, and AuthToken specified",
opts: &api.ClientOptions{
Host: "test.com",
AuthToken: "token",
},
out: true,
},
{
name: "Host, and UnixDomainSocket specified",
opts: &api.ClientOptions{
Host: "test.com",
UnixDomainSocket: "socket",
},
out: true,
},
{
name: "Host, and Transport specified",
opts: &api.ClientOptions{
Host: "test.com",
Transport: http.DefaultTransport,
},
out: true,
},
{
name: "AuthToken, and UnixDomainSocket specified",
opts: &api.ClientOptions{
AuthToken: "token",
UnixDomainSocket: "socket",
},
out: true,
},
{
name: "AuthToken, and Transport specified",
opts: &api.ClientOptions{
AuthToken: "token",
Transport: http.DefaultTransport,
},
out: true,
},
{
name: "Host specified",
opts: &api.ClientOptions{
Host: "test.com",
},
out: true,
},
{
name: "AuthToken specified",
opts: &api.ClientOptions{
AuthToken: "token",
},
out: true,
},
{
name: "UnixDomainSocket specified",
opts: &api.ClientOptions{
UnixDomainSocket: "socket",
},
out: true,
},
{
name: "Transport specified",
opts: &api.ClientOptions{
Transport: http.DefaultTransport,
},
out: true,
},
{
name: "SkipResolution specified",
opts: &api.ClientOptions{
SkipResolution: true,
},
out: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.out, optionsNeedResolution(tt.opts))
})
}
}
func printPendingMocks(mocks []gock.Mock) string {
paths := []string{}
for _, mock := range mocks {
paths = append(paths, mock.Request().URLStruct.String())
}
return fmt.Sprintf("%d unmatched mocks: %s", len(paths), strings.Join(paths, ", "))
}
func stubConfig(t *testing.T, cfgStr string) {
t.Helper()
old := config.Read
config.Read = func() (*config.Config, error) {
return config.ReadFromString(cfgStr), nil
}
t.Cleanup(func() {
config.Read = old
})
}
func testConfig() string {
return `
hosts:
github.com:
user: user1
oauth_token: abc123
git_protocol: ssh
`
}
func testConfigWithSocket() string {
return `
http_unix_socket: socket
hosts:
github.com:
user: user1
oauth_token: token
git_protocol: ssh
`
}