Skip to content

Commit 7db532a

Browse files
luxassBagToad
andcommitted
test(gist/edit): add tests for single file edit and interactive multi-file selection
Co-authored-by: Kynan Ware <47394200+BagToad@users.noreply.github.com>
1 parent 8babe59 commit 7db532a

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

pkg/cmd/gist/edit/edit_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,33 @@ func Test_editRun(t *testing.T) {
237237
},
238238
},
239239
},
240+
{
241+
name: "single file edit flag sends only edited file",
242+
opts: &EditOptions{
243+
Selector: "1234",
244+
EditFilename: "unix.md",
245+
},
246+
mockGist: &shared.Gist{
247+
ID: "1234",
248+
Files: map[string]*shared.GistFile{
249+
"cicada.txt": {Filename: "cicada.txt", Content: "bwhiizzzbwhuiiizzzz", Type: "text/plain"},
250+
"unix.md": {Filename: "unix.md", Content: "meow", Type: "text/markdown"},
251+
},
252+
Owner: &shared.GistOwner{Login: "octocat"},
253+
},
254+
httpStubs: func(reg *httpmock.Registry) {
255+
reg.Register(httpmock.REST("POST", "gists/1234"), httpmock.StatusStringResponse(201, "{}"))
256+
},
257+
wantLastRequestParameters: map[string]interface{}{
258+
"description": "",
259+
"files": map[string]interface{}{
260+
"unix.md": map[string]interface{}{
261+
"content": "new file content",
262+
"filename": "unix.md",
263+
},
264+
},
265+
},
266+
},
240267
{
241268
name: "multiple files, cancel, with TTY",
242269
isTTY: true,
@@ -645,6 +672,48 @@ func Test_editRun(t *testing.T) {
645672
},
646673
},
647674
},
675+
{
676+
name: "interactive truncated multi-file gist fetches only selected file raw content the first time",
677+
isTTY: true,
678+
opts: &EditOptions{Selector: "1234"},
679+
prompterStubs: func(pm *prompter.MockPrompter) {
680+
pm.RegisterSelect("Edit which file?", []string{"also-truncated.txt", "large.txt"}, func(_, _ string, opts []string) (int, error) {
681+
return prompter.IndexFor(opts, "large.txt")
682+
})
683+
pm.RegisterSelect("What next?", editNextOptions, func(_, _ string, opts []string) (int, error) {
684+
return prompter.IndexFor(opts, "Edit another file")
685+
})
686+
// Editing large.txt twice to ensure that fetch for the raw URL happens only once
687+
pm.RegisterSelect("Edit which file?", []string{"also-truncated.txt", "large.txt"}, func(_, _ string, opts []string) (int, error) {
688+
return prompter.IndexFor(opts, "large.txt")
689+
})
690+
pm.RegisterSelect("What next?", editNextOptions, func(_, _ string, opts []string) (int, error) {
691+
return prompter.IndexFor(opts, "Submit")
692+
})
693+
},
694+
mockGist: &shared.Gist{
695+
ID: "1234",
696+
Files: map[string]*shared.GistFile{
697+
"large.txt": {Filename: "large.txt", Content: "This is truncated content...", Type: "text/plain", Truncated: true, RawURL: "https://gist.githubusercontent.com/user/1234/raw/large.txt"},
698+
"also-truncated.txt": {Filename: "also-truncated.txt", Content: "stuff...", Type: "text/plain", Truncated: true, RawURL: "https://gist.githubusercontent.com/user/1234/raw/also-truncated.txt"},
699+
},
700+
Owner: &shared.GistOwner{Login: "octocat"},
701+
},
702+
httpStubs: func(reg *httpmock.Registry) {
703+
reg.Register(httpmock.REST("POST", "gists/1234"), httpmock.StatusStringResponse(201, "{}"))
704+
// Explicity exclude also-truncated.txt raw URL to ensure it is not fetched since we did not select it.
705+
reg.Exclude(t, httpmock.REST("GET", "user/1234/raw/also-truncated.txt"))
706+
},
707+
wantLastRequestParameters: map[string]interface{}{
708+
"description": "",
709+
"files": map[string]interface{}{
710+
"large.txt": map[string]interface{}{
711+
"content": "new file content",
712+
"filename": "large.txt",
713+
},
714+
},
715+
},
716+
},
648717
}
649718

650719
for _, tt := range tests {

0 commit comments

Comments
 (0)