Skip to content

Commit 19e1178

Browse files
phanenbabakks
andauthored
fix: gh gist edit panic when no file in a gist (cli#10627)
* fix: `gh gist edit` panic when no file in a gist * fix: improve error message Signed-off-by: Babak K. Shandiz <babakks@github.com> --------- Signed-off-by: Babak K. Shandiz <babakks@github.com> Co-authored-by: Babak K. Shandiz <babakks@github.com>
1 parent c927ea4 commit 19e1178

4 files changed

Lines changed: 56 additions & 0 deletions

File tree

pkg/cmd/gist/edit/edit.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ func editRun(opts *EditOptions) error {
236236
if filename == "" {
237237
if len(candidates) == 1 {
238238
filename = candidates[0]
239+
} else if len(candidates) == 0 {
240+
return errors.New("no file in the gist")
239241
} else {
240242
if !opts.IO.CanPrompt() {
241243
return errors.New("unsure what file to edit; either specify --filename or run interactively")

pkg/cmd/gist/edit/edit_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,30 @@ func Test_editRun(t *testing.T) {
557557
},
558558
wantErr: "gist ID or URL required when not running interactively",
559559
},
560+
{
561+
name: "edit no-file gist (#10626)",
562+
opts: &EditOptions{
563+
Selector: "1234",
564+
},
565+
mockGist: &shared.Gist{
566+
ID: "1234",
567+
Files: map[string]*shared.GistFile{},
568+
Owner: &shared.GistOwner{Login: "octocat"},
569+
},
570+
wantErr: "no file in the gist",
571+
},
572+
{
573+
name: "edit no-file gist, nil map (#10626)",
574+
opts: &EditOptions{
575+
Selector: "1234",
576+
},
577+
mockGist: &shared.Gist{
578+
ID: "1234",
579+
Files: nil,
580+
Owner: &shared.GistOwner{Login: "octocat"},
581+
},
582+
wantErr: "no file in the gist",
583+
},
560584
}
561585

562586
for _, tt := range tests {

pkg/cmd/gist/shared/shared.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ func (g Gist) Filename() string {
4444
for fn := range g.Files {
4545
filenames = append(filenames, fn)
4646
}
47+
if len(filenames) == 0 {
48+
return ""
49+
}
4750
sort.Strings(filenames)
4851
return filenames[0]
4952
}

pkg/cmd/gist/shared/shared_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,33 @@ func TestPromptGists(t *testing.T) {
163163
response: `{ "data": { "viewer": { "gists": { "nodes": [] } } } }`,
164164
wantOut: Gist{},
165165
},
166+
{
167+
name: "prompt list contains no-file gist (#10626)",
168+
prompterStubs: func(pm *prompter.MockPrompter) {
169+
pm.RegisterSelect("Select a gist",
170+
[]string{" about 6 hours ago", "gistfile0.txt about 6 hours ago"},
171+
func(_, _ string, opts []string) (int, error) {
172+
return prompter.IndexFor(opts, " about 6 hours ago")
173+
})
174+
},
175+
response: `{ "data": { "viewer": { "gists": { "nodes": [
176+
{
177+
"name": "1234",
178+
"files": [],
179+
"description": "",
180+
"updatedAt": "%[1]v",
181+
"isPublic": true
182+
},
183+
{
184+
"name": "5678",
185+
"files": [{ "name": "gistfile0.txt" }],
186+
"description": "",
187+
"updatedAt": "%[1]v",
188+
"isPublic": true
189+
}
190+
] } } } }`,
191+
wantOut: Gist{ID: "1234", Files: map[string]*GistFile{}, UpdatedAt: sixHoursAgo, Public: true},
192+
},
166193
}
167194

168195
ios, _, _, _ := iostreams.Test()

0 commit comments

Comments
 (0)