Skip to content

Commit d8ef8b8

Browse files
committed
🐛 Fix stubs
Signed-off-by: Matthew Gleich <email@mattglei.ch>
1 parent 89e1ee3 commit d8ef8b8

File tree

1 file changed

+222
-83
lines changed

1 file changed

+222
-83
lines changed

pkg/cmd/gist/list/list_test.go

Lines changed: 222 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package list
22

33
import (
44
"bytes"
5+
"fmt"
56
"net/http"
67
"testing"
78
"time"
89

9-
"github.com/cli/cli/pkg/cmd/gist/shared"
1010
"github.com/cli/cli/pkg/cmdutil"
1111
"github.com/cli/cli/pkg/httpmock"
1212
"github.com/cli/cli/pkg/iostreams"
@@ -88,115 +88,254 @@ func TestNewCmdList(t *testing.T) {
8888
}
8989

9090
func Test_listRun(t *testing.T) {
91+
const query = `query ListGists\b`
92+
sixHoursAgo, _ := time.ParseDuration("-6h")
93+
timeSixHoursAgo := time.Now().Add(sixHoursAgo).Format(time.RFC3339)
94+
blankTime := time.Time{}.Format(time.RFC3339)
9195
tests := []struct {
92-
name string
93-
opts *ListOptions
94-
wantOut string
95-
stubs func(*httpmock.Registry)
96-
nontty bool
97-
updatedAt *time.Time
96+
name string
97+
opts *ListOptions
98+
wantOut string
99+
stubs func(*httpmock.Registry)
100+
nontty bool
98101
}{
99102
{
100103
name: "no gists",
101104
opts: &ListOptions{},
102105
stubs: func(reg *httpmock.Registry) {
103-
reg.Register(httpmock.REST("GET", "gists"),
104-
httpmock.JSONResponse([]shared.Gist{}))
105-
106+
reg.Register(
107+
httpmock.GraphQL(query),
108+
httpmock.StringResponse(`{ "data": { "viewer": {
109+
"gists": { "nodes": [] }
110+
} } }`))
106111
},
107112
wantOut: "",
108113
},
109114
{
110-
name: "default behavior",
111-
opts: &ListOptions{},
115+
name: "default behavior",
116+
opts: &ListOptions{},
117+
stubs: func(reg *httpmock.Registry) {
118+
reg.Register(
119+
httpmock.GraphQL(query),
120+
httpmock.StringResponse(fmt.Sprintf(
121+
`{ "data": { "viewer": { "gists": { "nodes": [
122+
{
123+
"name": "1234567890",
124+
"files": [{ "name": "cool.txt", "languages": { "name": "None" }, "extension": ".txt" }],
125+
"description": "",
126+
"updatedAt": "%v",
127+
"isPublic": true
128+
},
129+
{
130+
"name": "4567890123",
131+
"files": [{ "name": "gistfile0.txt", "languages": { "name": "None" }, "extension": ".txt" }],
132+
"description": "",
133+
"updatedAt": "%v",
134+
"isPublic": true
135+
},
136+
{
137+
"name": "2345678901",
138+
"files": [
139+
{ "name": "gistfile0.txt", "languages": { "name": "None" }, "extension": ".txt" },
140+
{ "name": "gistfile1.txt", "languages": { "name": "None" }, "extension": ".txt" }
141+
],
142+
"description": "tea leaves thwart those who court catastrophe",
143+
"updatedAt": "%v",
144+
"isPublic": false
145+
},
146+
{
147+
"name": "3456789012",
148+
"files": [
149+
{ "name": "gistfile0.txt", "languages": { "name": "None" }, "extension": ".txt" },
150+
{ "name": "gistfile1.txt", "languages": { "name": "None" }, "extension": ".txt" },
151+
{ "name": "gistfile2.txt", "languages": { "name": "None" }, "extension": ".txt" },
152+
{ "name": "gistfile3.txt", "languages": { "name": "None" }, "extension": ".txt" },
153+
{ "name": "gistfile4.txt", "languages": { "name": "None" }, "extension": ".txt" },
154+
{ "name": "gistfile5.txt", "languages": { "name": "None" }, "extension": ".txt" },
155+
{ "name": "gistfile6.txt", "languages": { "name": "None" }, "extension": ".txt" },
156+
{ "name": "gistfile7.txt", "languages": { "name": "None" }, "extension": ".txt" },
157+
{ "name": "gistfile9.txt", "languages": { "name": "None" }, "extension": ".txt" },
158+
{ "name": "gistfile10.txt", "languages": { "name": "None" }, "extension": ".txt" },
159+
{ "name": "gistfile11.txt", "languages": { "name": "None" }, "extension": ".txt" }
160+
],
161+
"description": "short desc",
162+
"updatedAt": "%v",
163+
"isPublic": false
164+
}
165+
] } } } }`,
166+
timeSixHoursAgo,
167+
timeSixHoursAgo,
168+
timeSixHoursAgo,
169+
timeSixHoursAgo,
170+
)),
171+
)
172+
},
112173
wantOut: "1234567890 cool.txt 1 file public about 6 hours ago\n4567890123 1 file public about 6 hours ago\n2345678901 tea leaves thwart... 2 files secret about 6 hours ago\n3456789012 short desc 11 files secret about 6 hours ago\n",
113174
},
114175
{
115-
name: "with public filter",
116-
opts: &ListOptions{Visibility: "public"},
176+
name: "with public filter",
177+
opts: &ListOptions{Visibility: "public"},
178+
stubs: func(reg *httpmock.Registry) {
179+
reg.Register(
180+
httpmock.GraphQL(query),
181+
httpmock.StringResponse(fmt.Sprintf(
182+
`{ "data": { "viewer": { "gists": { "nodes": [
183+
{
184+
"name": "1234567890",
185+
"files": [{ "name": "cool.txt", "languages": { "name": "None" }, "extension": ".txt" }],
186+
"description": "",
187+
"updatedAt": "%v",
188+
"isPublic": true
189+
},
190+
{
191+
"name": "4567890123",
192+
"files": [{ "name": "gistfile0.txt", "languages": { "name": "None" }, "extension": ".txt" }],
193+
"description": "",
194+
"updatedAt": "%v",
195+
"isPublic": true
196+
}
197+
] } } } }`,
198+
timeSixHoursAgo,
199+
timeSixHoursAgo,
200+
)),
201+
)
202+
},
117203
wantOut: "1234567890 cool.txt 1 file public about 6 hours ago\n4567890123 1 file public about 6 hours ago\n",
118204
},
119205
{
120-
name: "with secret filter",
121-
opts: &ListOptions{Visibility: "secret"},
206+
name: "with secret filter",
207+
opts: &ListOptions{Visibility: "secret"},
208+
stubs: func(reg *httpmock.Registry) {
209+
reg.Register(
210+
httpmock.GraphQL(query),
211+
httpmock.StringResponse(fmt.Sprintf(
212+
`{ "data": { "viewer": { "gists": { "nodes": [
213+
{
214+
"name": "2345678901",
215+
"files": [
216+
{ "name": "gistfile0.txt", "languages": { "name": "None" }, "extension": ".txt" },
217+
{ "name": "gistfile1.txt", "languages": { "name": "None" }, "extension": ".txt" }
218+
],
219+
"description": "tea leaves thwart those who court catastrophe",
220+
"updatedAt": "%v",
221+
"isPublic": false
222+
},
223+
{
224+
"name": "3456789012",
225+
"files": [
226+
{ "name": "gistfile0.txt", "languages": { "name": "None" }, "extension": ".txt" },
227+
{ "name": "gistfile1.txt", "languages": { "name": "None" }, "extension": ".txt" },
228+
{ "name": "gistfile2.txt", "languages": { "name": "None" }, "extension": ".txt" },
229+
{ "name": "gistfile3.txt", "languages": { "name": "None" }, "extension": ".txt" },
230+
{ "name": "gistfile4.txt", "languages": { "name": "None" }, "extension": ".txt" },
231+
{ "name": "gistfile5.txt", "languages": { "name": "None" }, "extension": ".txt" },
232+
{ "name": "gistfile6.txt", "languages": { "name": "None" }, "extension": ".txt" },
233+
{ "name": "gistfile7.txt", "languages": { "name": "None" }, "extension": ".txt" },
234+
{ "name": "gistfile9.txt", "languages": { "name": "None" }, "extension": ".txt" },
235+
{ "name": "gistfile10.txt", "languages": { "name": "None" }, "extension": ".txt" },
236+
{ "name": "gistfile11.txt", "languages": { "name": "None" }, "extension": ".txt" }
237+
],
238+
"description": "short desc",
239+
"updatedAt": "%v",
240+
"isPublic": false
241+
}
242+
] } } } }`,
243+
timeSixHoursAgo,
244+
timeSixHoursAgo,
245+
)),
246+
)
247+
},
122248
wantOut: "2345678901 tea leaves thwart... 2 files secret about 6 hours ago\n3456789012 short desc 11 files secret about 6 hours ago\n",
123249
},
124250
{
125-
name: "with limit",
126-
opts: &ListOptions{Limit: 1},
251+
name: "with limit",
252+
opts: &ListOptions{Limit: 1},
253+
stubs: func(reg *httpmock.Registry) {
254+
reg.Register(
255+
httpmock.GraphQL(query),
256+
httpmock.StringResponse(fmt.Sprintf(
257+
`{ "data": { "viewer": { "gists": { "nodes": [
258+
{
259+
"name": "1234567890",
260+
"files": [{ "name": "cool.txt", "languages": { "name": "None" }, "extension": ".txt" }],
261+
"description": "",
262+
"updatedAt": "%v",
263+
"isPublic": true
264+
}
265+
] } } } }`,
266+
timeSixHoursAgo,
267+
)),
268+
)
269+
},
127270
wantOut: "1234567890 cool.txt 1 file public about 6 hours ago\n",
128271
},
129272
{
130-
name: "nontty output",
131-
opts: &ListOptions{},
132-
updatedAt: &time.Time{},
133-
wantOut: "1234567890\tcool.txt\t1 file\tpublic\t0001-01-01 00:00:00 +0000 UTC\n4567890123\t\t1 file\tpublic\t0001-01-01 00:00:00 +0000 UTC\n2345678901\ttea leaves thwart those who court catastrophe\t2 files\tsecret\t0001-01-01 00:00:00 +0000 UTC\n3456789012\tshort desc\t11 files\tsecret\t0001-01-01 00:00:00 +0000 UTC\n",
134-
nontty: true,
273+
name: "nontty output",
274+
opts: &ListOptions{},
275+
stubs: func(reg *httpmock.Registry) {
276+
reg.Register(
277+
httpmock.GraphQL(query),
278+
httpmock.StringResponse(fmt.Sprintf(
279+
`{ "data": { "viewer": { "gists": { "nodes": [
280+
{
281+
"name": "1234567890",
282+
"files": [{ "name": "cool.txt", "languages": { "name": "None" }, "extension": ".txt" }],
283+
"description": "",
284+
"updatedAt": "%v",
285+
"isPublic": true
286+
},
287+
{
288+
"name": "4567890123",
289+
"files": [{ "name": "gistfile0.txt", "languages": { "name": "None" }, "extension": ".txt" }],
290+
"description": "",
291+
"updatedAt": "%v",
292+
"isPublic": true
293+
},
294+
{
295+
"name": "2345678901",
296+
"files": [
297+
{ "name": "gistfile0.txt", "languages": { "name": "None" }, "extension": ".txt" },
298+
{ "name": "gistfile1.txt", "languages": { "name": "None" }, "extension": ".txt" }
299+
],
300+
"description": "tea leaves thwart those who court catastrophe",
301+
"updatedAt": "%v",
302+
"isPublic": false
303+
},
304+
{
305+
"name": "3456789012",
306+
"files": [
307+
{ "name": "gistfile0.txt", "languages": { "name": "None" }, "extension": ".txt" },
308+
{ "name": "gistfile1.txt", "languages": { "name": "None" }, "extension": ".txt" },
309+
{ "name": "gistfile2.txt", "languages": { "name": "None" }, "extension": ".txt" },
310+
{ "name": "gistfile3.txt", "languages": { "name": "None" }, "extension": ".txt" },
311+
{ "name": "gistfile4.txt", "languages": { "name": "None" }, "extension": ".txt" },
312+
{ "name": "gistfile5.txt", "languages": { "name": "None" }, "extension": ".txt" },
313+
{ "name": "gistfile6.txt", "languages": { "name": "None" }, "extension": ".txt" },
314+
{ "name": "gistfile7.txt", "languages": { "name": "None" }, "extension": ".txt" },
315+
{ "name": "gistfile9.txt", "languages": { "name": "None" }, "extension": ".txt" },
316+
{ "name": "gistfile10.txt", "languages": { "name": "None" }, "extension": ".txt" },
317+
{ "name": "gistfile11.txt", "languages": { "name": "None" }, "extension": ".txt" }
318+
],
319+
"description": "short desc",
320+
"updatedAt": "%v",
321+
"isPublic": false
322+
}
323+
] } } } }`,
324+
blankTime,
325+
blankTime,
326+
blankTime,
327+
blankTime,
328+
)),
329+
)
330+
},
331+
wantOut: "1234567890\tcool.txt\t1 file\tpublic\t0001-01-01 00:00:00 +0000 UTC\n4567890123\t\t1 file\tpublic\t0001-01-01 00:00:00 +0000 UTC\n2345678901\ttea leaves thwart those who court catastrophe\t2 files\tsecret\t0001-01-01 00:00:00 +0000 UTC\n3456789012\tshort desc\t11 files\tsecret\t0001-01-01 00:00:00 +0000 UTC\n",
332+
nontty: true,
135333
},
136334
}
137335

138336
for _, tt := range tests {
139-
sixHoursAgo, _ := time.ParseDuration("-6h")
140-
updatedAt := time.Now().Add(sixHoursAgo)
141-
if tt.updatedAt != nil {
142-
updatedAt = *tt.updatedAt
143-
}
144-
145337
reg := &httpmock.Registry{}
146-
if tt.stubs == nil {
147-
reg.Register(httpmock.REST("GET", "gists"),
148-
httpmock.JSONResponse([]shared.Gist{
149-
{
150-
ID: "1234567890",
151-
UpdatedAt: updatedAt,
152-
Description: "",
153-
Files: map[string]*shared.GistFile{
154-
"cool.txt": {},
155-
},
156-
Public: true,
157-
},
158-
{
159-
ID: "4567890123",
160-
UpdatedAt: updatedAt,
161-
Description: "",
162-
Files: map[string]*shared.GistFile{
163-
"gistfile0.txt": {},
164-
},
165-
Public: true,
166-
},
167-
{
168-
ID: "2345678901",
169-
UpdatedAt: updatedAt,
170-
Description: "tea leaves thwart those who court catastrophe",
171-
Files: map[string]*shared.GistFile{
172-
"gistfile0.txt": {},
173-
"gistfile1.txt": {},
174-
},
175-
Public: false,
176-
},
177-
{
178-
ID: "3456789012",
179-
UpdatedAt: updatedAt,
180-
Description: "short desc",
181-
Files: map[string]*shared.GistFile{
182-
"gistfile0.txt": {},
183-
"gistfile1.txt": {},
184-
"gistfile2.txt": {},
185-
"gistfile3.txt": {},
186-
"gistfile4.txt": {},
187-
"gistfile5.txt": {},
188-
"gistfile6.txt": {},
189-
"gistfile7.txt": {},
190-
"gistfile8.txt": {},
191-
"gistfile9.txt": {},
192-
"gistfile10.txt": {},
193-
},
194-
Public: false,
195-
},
196-
}))
197-
} else {
198-
tt.stubs(reg)
199-
}
338+
tt.stubs(reg)
200339

201340
tt.opts.HttpClient = func() (*http.Client, error) {
202341
return &http.Client{Transport: reg}, nil

0 commit comments

Comments
 (0)