forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_test.go
More file actions
419 lines (389 loc) · 10.2 KB
/
create_test.go
File metadata and controls
419 lines (389 loc) · 10.2 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package create
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"path"
"strings"
"testing"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/run"
"github.com/cli/cli/v2/pkg/cmd/gist/shared"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
)
func Test_processFiles(t *testing.T) {
fakeStdin := strings.NewReader("hey cool how is it going")
files, err := processFiles(ioutil.NopCloser(fakeStdin), "", []string{"-"})
if err != nil {
t.Fatalf("unexpected error processing files: %s", err)
}
assert.Equal(t, 1, len(files))
assert.Equal(t, "hey cool how is it going", files["gistfile0.txt"].Content)
}
func Test_guessGistName_stdin(t *testing.T) {
files := map[string]*shared.GistFile{
"gistfile0.txt": {Content: "sample content"},
}
gistName := guessGistName(files)
assert.Equal(t, "", gistName)
}
func Test_guessGistName_userFiles(t *testing.T) {
files := map[string]*shared.GistFile{
"fig.txt": {Content: "I am a fig"},
"apple.txt": {Content: "I am an apple"},
"gistfile0.txt": {Content: "sample content"},
}
gistName := guessGistName(files)
assert.Equal(t, "apple.txt", gistName)
}
func TestNewCmdCreate(t *testing.T) {
tests := []struct {
name string
cli string
factory func(*cmdutil.Factory) *cmdutil.Factory
wants CreateOptions
wantsErr bool
}{
{
name: "no arguments",
cli: "",
wants: CreateOptions{
Description: "",
Public: false,
Filenames: []string{""},
},
wantsErr: false,
},
{
name: "no arguments with TTY stdin",
factory: func(f *cmdutil.Factory) *cmdutil.Factory {
f.IOStreams.SetStdinTTY(true)
return f
},
cli: "",
wants: CreateOptions{
Description: "",
Public: false,
Filenames: []string{""},
},
wantsErr: true,
},
{
name: "stdin argument",
cli: "-",
wants: CreateOptions{
Description: "",
Public: false,
Filenames: []string{"-"},
},
wantsErr: false,
},
{
name: "with description",
cli: `-d "my new gist" -`,
wants: CreateOptions{
Description: "my new gist",
Public: false,
Filenames: []string{"-"},
},
wantsErr: false,
},
{
name: "public",
cli: `--public -`,
wants: CreateOptions{
Description: "",
Public: true,
Filenames: []string{"-"},
},
wantsErr: false,
},
{
name: "list of files",
cli: "file1.txt file2.txt",
wants: CreateOptions{
Description: "",
Public: false,
Filenames: []string{"file1.txt", "file2.txt"},
},
wantsErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
io, _, _, _ := iostreams.Test()
f := &cmdutil.Factory{
IOStreams: io,
}
if tt.factory != nil {
f = tt.factory(f)
}
argv, err := shlex.Split(tt.cli)
assert.NoError(t, err)
var gotOpts *CreateOptions
cmd := NewCmdCreate(f, func(opts *CreateOptions) error {
gotOpts = opts
return nil
})
cmd.SetArgs(argv)
cmd.SetIn(&bytes.Buffer{})
cmd.SetOut(&bytes.Buffer{})
cmd.SetErr(&bytes.Buffer{})
_, err = cmd.ExecuteC()
if tt.wantsErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.wants.Description, gotOpts.Description)
assert.Equal(t, tt.wants.Public, gotOpts.Public)
})
}
}
func Test_createRun(t *testing.T) {
tempDir := t.TempDir()
fixtureFile := path.Join(tempDir, "fixture.txt")
assert.NoError(t, ioutil.WriteFile(fixtureFile, []byte("{}"), 0644))
emptyFile := path.Join(tempDir, "empty.txt")
assert.NoError(t, ioutil.WriteFile(emptyFile, []byte(" \t\n"), 0644))
tests := []struct {
name string
opts *CreateOptions
stdin string
wantOut string
wantStderr string
wantParams map[string]interface{}
wantErr bool
wantBrowse string
responseStatus int
}{
{
name: "public",
opts: &CreateOptions{
Public: true,
Filenames: []string{fixtureFile},
},
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
wantStderr: "- Creating gist fixture.txt\n✓ Created gist fixture.txt\n",
wantErr: false,
wantParams: map[string]interface{}{
"description": "",
"updated_at": "0001-01-01T00:00:00Z",
"public": true,
"files": map[string]interface{}{
"fixture.txt": map[string]interface{}{
"content": "{}",
},
},
},
responseStatus: http.StatusOK,
},
{
name: "with description",
opts: &CreateOptions{
Description: "an incredibly interesting gist",
Filenames: []string{fixtureFile},
},
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
wantStderr: "- Creating gist fixture.txt\n✓ Created gist fixture.txt\n",
wantErr: false,
wantParams: map[string]interface{}{
"description": "an incredibly interesting gist",
"updated_at": "0001-01-01T00:00:00Z",
"public": false,
"files": map[string]interface{}{
"fixture.txt": map[string]interface{}{
"content": "{}",
},
},
},
responseStatus: http.StatusOK,
},
{
name: "multiple files",
opts: &CreateOptions{
Filenames: []string{fixtureFile, "-"},
},
stdin: "cool stdin content",
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
wantStderr: "- Creating gist with multiple files\n✓ Created gist fixture.txt\n",
wantErr: false,
wantParams: map[string]interface{}{
"description": "",
"updated_at": "0001-01-01T00:00:00Z",
"public": false,
"files": map[string]interface{}{
"fixture.txt": map[string]interface{}{
"content": "{}",
},
"gistfile1.txt": map[string]interface{}{
"content": "cool stdin content",
},
},
},
responseStatus: http.StatusOK,
},
{
name: "file with empty content",
opts: &CreateOptions{
Filenames: []string{emptyFile},
},
wantOut: "",
wantStderr: heredoc.Doc(`
- Creating gist empty.txt
X Failed to create gist: a gist file cannot be blank
`),
wantErr: true,
wantParams: map[string]interface{}{
"description": "",
"updated_at": "0001-01-01T00:00:00Z",
"public": false,
"files": map[string]interface{}{
"empty.txt": map[string]interface{}{"content": " \t\n"},
},
},
responseStatus: http.StatusUnprocessableEntity,
},
{
name: "stdin arg",
opts: &CreateOptions{
Filenames: []string{"-"},
},
stdin: "cool stdin content",
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
wantStderr: "- Creating gist...\n✓ Created gist\n",
wantErr: false,
wantParams: map[string]interface{}{
"description": "",
"updated_at": "0001-01-01T00:00:00Z",
"public": false,
"files": map[string]interface{}{
"gistfile0.txt": map[string]interface{}{
"content": "cool stdin content",
},
},
},
responseStatus: http.StatusOK,
},
{
name: "web arg",
opts: &CreateOptions{
WebMode: true,
Filenames: []string{fixtureFile},
},
wantOut: "Opening gist.github.com/aa5a315d61ae9438b18d in your browser.\n",
wantStderr: "- Creating gist fixture.txt\n✓ Created gist fixture.txt\n",
wantErr: false,
wantBrowse: "https://gist.github.com/aa5a315d61ae9438b18d",
wantParams: map[string]interface{}{
"description": "",
"updated_at": "0001-01-01T00:00:00Z",
"public": false,
"files": map[string]interface{}{
"fixture.txt": map[string]interface{}{
"content": "{}",
},
},
},
responseStatus: http.StatusOK,
},
}
for _, tt := range tests {
reg := &httpmock.Registry{}
if tt.responseStatus == http.StatusOK {
reg.Register(
httpmock.REST("POST", "gists"),
httpmock.StringResponse(`{
"html_url": "https://gist.github.com/aa5a315d61ae9438b18d"
}`))
} else {
reg.Register(
httpmock.REST("POST", "gists"),
httpmock.StatusStringResponse(tt.responseStatus, "{}"))
}
mockClient := func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
}
tt.opts.HttpClient = mockClient
tt.opts.Config = func() (config.Config, error) {
return config.NewBlankConfig(), nil
}
io, stdin, stdout, stderr := iostreams.Test()
tt.opts.IO = io
browser := &cmdutil.TestBrowser{}
tt.opts.Browser = browser
_, teardown := run.Stub()
defer teardown(t)
t.Run(tt.name, func(t *testing.T) {
stdin.WriteString(tt.stdin)
if err := createRun(tt.opts); (err != nil) != tt.wantErr {
t.Errorf("createRun() error = %v, wantErr %v", err, tt.wantErr)
}
bodyBytes, _ := ioutil.ReadAll(reg.Requests[0].Body)
reqBody := make(map[string]interface{})
err := json.Unmarshal(bodyBytes, &reqBody)
if err != nil {
t.Fatalf("error decoding JSON: %v", err)
}
assert.Equal(t, tt.wantOut, stdout.String())
assert.Equal(t, tt.wantStderr, stderr.String())
assert.Equal(t, tt.wantParams, reqBody)
reg.Verify(t)
browser.Verify(t, tt.wantBrowse)
})
}
}
func Test_detectEmptyFiles(t *testing.T) {
tests := []struct {
content string
isEmptyFile bool
}{
{
content: "{}",
isEmptyFile: false,
},
{
content: "\n\t",
isEmptyFile: true,
},
}
for _, tt := range tests {
files := map[string]*shared.GistFile{}
files["file"] = &shared.GistFile{
Content: tt.content,
}
isEmptyFile := detectEmptyFiles(files)
assert.Equal(t, tt.isEmptyFile, isEmptyFile)
}
}
func Test_CreateRun_reauth(t *testing.T) {
reg := &httpmock.Registry{}
reg.Register(httpmock.REST("POST", "gists"), func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 404,
Request: req,
Header: map[string][]string{
"X-Oauth-Scopes": {"repo, read:org"},
},
Body: ioutil.NopCloser(bytes.NewBufferString("oh no")),
}, nil
})
io, _, _, _ := iostreams.Test()
opts := &CreateOptions{
IO: io,
HttpClient: func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
},
Config: func() (config.Config, error) {
return config.NewBlankConfig(), nil
},
}
err := createRun(opts)
assert.EqualError(t, err, "This command requires the 'gist' OAuth scope.\nPlease re-authenticate with: gh auth refresh -h github.com -s gist")
}