From cbaaf77822bd764dbce526acce4a0b51d772fcb9 Mon Sep 17 00:00:00 2001 From: Mateus Marquezini Date: Mon, 9 Dec 2024 08:35:01 -0300 Subject: [PATCH 01/12] fixing gh gist view prompts with no TTY --- pkg/cmd/gist/view/view.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/gist/view/view.go b/pkg/cmd/gist/view/view.go index 2a19f5958..a6f4d528a 100644 --- a/pkg/cmd/gist/view/view.go +++ b/pkg/cmd/gist/view/view.go @@ -55,7 +55,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman } if !opts.IO.IsStdoutTTY() { - opts.Raw = true + return cmdutil.FlagErrorf("run or gist ID required when not running interactively\n\nUsage: gh gist view [ | ] [flags]\n\nFlags:\n -f, --filename string Display a single file from the gist\n --files List file names from the gist\n -r, --raw Print raw instead of rendered gist contents\n -w, --web Open gist in the browser\n") } if runF != nil { From b740486b13046ad1560aa1c461947ef27c145eb2 Mon Sep 17 00:00:00 2001 From: Mateus Marquezini Date: Mon, 9 Dec 2024 10:59:09 -0300 Subject: [PATCH 02/12] #10042 fixed test --- pkg/cmd/gist/view/view_test.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/gist/view/view_test.go b/pkg/cmd/gist/view/view_test.go index 52e616f7c..2e80ee082 100644 --- a/pkg/cmd/gist/view/view_test.go +++ b/pkg/cmd/gist/view/view_test.go @@ -20,10 +20,11 @@ import ( func TestNewCmdView(t *testing.T) { tests := []struct { - name string - cli string - wants ViewOptions - tty bool + name string + cli string + wants ViewOptions + tty bool + wantsErr bool }{ { name: "tty no arguments", @@ -37,12 +38,14 @@ func TestNewCmdView(t *testing.T) { }, { name: "nontty no arguments", + tty: false, cli: "123", wants: ViewOptions{ Raw: true, Selector: "123", ListFiles: false, }, + wantsErr: true, }, { name: "filename passed", @@ -94,6 +97,12 @@ func TestNewCmdView(t *testing.T) { gotOpts = opts return nil }) + + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + return + } cmd.SetArgs(argv) cmd.SetIn(&bytes.Buffer{}) cmd.SetOut(&bytes.Buffer{}) From f4f8840c3f53587f398240571c5affebc149d8c1 Mon Sep 17 00:00:00 2001 From: Mateus Marquezini Date: Thu, 9 Jan 2025 12:07:15 -0300 Subject: [PATCH 03/12] #10042: Add error messages for 'gh gist view/edit' prompts when no TTY is detected --- pkg/cmd/gist/edit/edit.go | 3 +++ pkg/cmd/gist/view/view.go | 6 +++++- pkg/cmd/gist/view/view_test.go | 17 +++++------------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pkg/cmd/gist/edit/edit.go b/pkg/cmd/gist/edit/edit.go index d777e9581..ced995f63 100644 --- a/pkg/cmd/gist/edit/edit.go +++ b/pkg/cmd/gist/edit/edit.go @@ -108,6 +108,9 @@ func editRun(opts *EditOptions) error { if gistID == "" { cs := opts.IO.ColorScheme() if gistID == "" { + if !opts.IO.CanPrompt() { + return cmdutil.FlagErrorf("gist ID or URL required when not running interactively") + } gistID, err = shared.PromptGists(opts.Prompter, client, host, cs) if err != nil { return err diff --git a/pkg/cmd/gist/view/view.go b/pkg/cmd/gist/view/view.go index a6f4d528a..25feccdc1 100644 --- a/pkg/cmd/gist/view/view.go +++ b/pkg/cmd/gist/view/view.go @@ -55,7 +55,7 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman } if !opts.IO.IsStdoutTTY() { - return cmdutil.FlagErrorf("run or gist ID required when not running interactively\n\nUsage: gh gist view [ | ] [flags]\n\nFlags:\n -f, --filename string Display a single file from the gist\n --files List file names from the gist\n -r, --raw Print raw instead of rendered gist contents\n -w, --web Open gist in the browser\n") + opts.Raw = true } if runF != nil { @@ -89,6 +89,10 @@ func viewRun(opts *ViewOptions) error { cs := opts.IO.ColorScheme() if gistID == "" { + if !opts.IO.CanPrompt() { + return cmdutil.FlagErrorf("gist ID or URL required when not running interactively") + } + gistID, err = shared.PromptGists(opts.Prompter, client, hostname, cs) if err != nil { return err diff --git a/pkg/cmd/gist/view/view_test.go b/pkg/cmd/gist/view/view_test.go index 2e80ee082..e52a3b9cb 100644 --- a/pkg/cmd/gist/view/view_test.go +++ b/pkg/cmd/gist/view/view_test.go @@ -20,11 +20,10 @@ import ( func TestNewCmdView(t *testing.T) { tests := []struct { - name string - cli string - wants ViewOptions - tty bool - wantsErr bool + name string + cli string + wants ViewOptions + tty bool }{ { name: "tty no arguments", @@ -45,7 +44,6 @@ func TestNewCmdView(t *testing.T) { Selector: "123", ListFiles: false, }, - wantsErr: true, }, { name: "filename passed", @@ -98,11 +96,6 @@ func TestNewCmdView(t *testing.T) { return nil }) - _, err = cmd.ExecuteC() - if tt.wantsErr { - assert.Error(t, err) - return - } cmd.SetArgs(argv) cmd.SetIn(&bytes.Buffer{}) cmd.SetOut(&bytes.Buffer{}) @@ -166,7 +159,7 @@ func Test_viewRun(t *testing.T) { }, }, }, - wantOut: "test interactive mode\n", + wantErr: true, }, { name: "filename selected", From 4c2ba5681d4777dc364bad989eda6a68189ca6a5 Mon Sep 17 00:00:00 2001 From: Mateus Marquezini Date: Thu, 9 Jan 2025 13:14:27 -0300 Subject: [PATCH 04/12] #10042 removed unnecessary field --- pkg/cmd/gist/view/view_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/cmd/gist/view/view_test.go b/pkg/cmd/gist/view/view_test.go index e52a3b9cb..37dda42d1 100644 --- a/pkg/cmd/gist/view/view_test.go +++ b/pkg/cmd/gist/view/view_test.go @@ -37,7 +37,6 @@ func TestNewCmdView(t *testing.T) { }, { name: "nontty no arguments", - tty: false, cli: "123", wants: ViewOptions{ Raw: true, From c5d6ae6cf698553e47b1fcd92385d7975296f35f Mon Sep 17 00:00:00 2001 From: Mateus Marquezini Date: Sun, 12 Jan 2025 14:53:20 -0300 Subject: [PATCH 05/12] Update pkg/cmd/gist/edit/edit.go Update pkg/cmd/gist/edit/edit.go after code review Co-authored-by: Kynan Ware <47394200+BagToad@users.noreply.github.com> --- pkg/cmd/gist/edit/edit.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/cmd/gist/edit/edit.go b/pkg/cmd/gist/edit/edit.go index ced995f63..5f2815682 100644 --- a/pkg/cmd/gist/edit/edit.go +++ b/pkg/cmd/gist/edit/edit.go @@ -111,6 +111,7 @@ func editRun(opts *EditOptions) error { if !opts.IO.CanPrompt() { return cmdutil.FlagErrorf("gist ID or URL required when not running interactively") } + gistID, err = shared.PromptGists(opts.Prompter, client, host, cs) if err != nil { return err From 24e9fed7e0720a47870a495bd54e45cca2b4a5a5 Mon Sep 17 00:00:00 2001 From: Mateus Marquezini Date: Sun, 12 Jan 2025 14:57:24 -0300 Subject: [PATCH 06/12] removed unnecessary space --- pkg/cmd/gist/edit/edit.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/gist/edit/edit.go b/pkg/cmd/gist/edit/edit.go index 5f2815682..b201cdc14 100644 --- a/pkg/cmd/gist/edit/edit.go +++ b/pkg/cmd/gist/edit/edit.go @@ -111,7 +111,7 @@ func editRun(opts *EditOptions) error { if !opts.IO.CanPrompt() { return cmdutil.FlagErrorf("gist ID or URL required when not running interactively") } - + gistID, err = shared.PromptGists(opts.Prompter, client, host, cs) if err != nil { return err From 977e2326a25d48c7fb4250b2927760ca28f18a79 Mon Sep 17 00:00:00 2001 From: Mateus Marquezini Date: Mon, 13 Jan 2025 11:06:13 -0300 Subject: [PATCH 07/12] #10042: Applied code review suggestions to view_test.go --- pkg/cmd/gist/edit/edit_test.go | 7 ++++ pkg/cmd/gist/view/view_test.go | 58 +++++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/pkg/cmd/gist/edit/edit_test.go b/pkg/cmd/gist/edit/edit_test.go index 11a32bc92..0bf7cb51a 100644 --- a/pkg/cmd/gist/edit/edit_test.go +++ b/pkg/cmd/gist/edit/edit_test.go @@ -519,6 +519,11 @@ func Test_editRun(t *testing.T) { }, }, }, + { + name: "no arguments notty", + nontty: true, + wantErr: "gist ID or URL required when not running interactively", + }, } for _, tt := range tests { @@ -552,6 +557,8 @@ func Test_editRun(t *testing.T) { stdin.WriteString(tt.stdin) ios.SetStdoutTTY(!tt.nontty) ios.SetStdinTTY(!tt.nontty) + ios.SetStderrTTY(!tt.nontty) + tt.opts.IO = ios tt.opts.Selector = "1234" diff --git a/pkg/cmd/gist/view/view_test.go b/pkg/cmd/gist/view/view_test.go index 37dda42d1..820401f1e 100644 --- a/pkg/cmd/gist/view/view_test.go +++ b/pkg/cmd/gist/view/view_test.go @@ -16,6 +16,7 @@ import ( "github.com/cli/cli/v2/pkg/iostreams" "github.com/google/shlex" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNewCmdView(t *testing.T) { @@ -116,19 +117,22 @@ func Test_viewRun(t *testing.T) { opts *ViewOptions wantOut string gist *shared.Gist - wantErr bool + isTTY bool + wantErr string mockGistList bool }{ { - name: "no such gist", + name: "no such gist", + isTTY: false, opts: &ViewOptions{ Selector: "1234", ListFiles: false, }, - wantErr: true, + wantErr: "not found", }, { - name: "one file", + name: "one file", + isTTY: true, opts: &ViewOptions{ Selector: "1234", ListFiles: false, @@ -144,7 +148,8 @@ func Test_viewRun(t *testing.T) { wantOut: "bwhiizzzbwhuiiizzzz\n", }, { - name: "one file, no ID supplied", + name: "one file, no ID supplied", + isTTY: true, opts: &ViewOptions{ Selector: "", ListFiles: false, @@ -158,10 +163,16 @@ func Test_viewRun(t *testing.T) { }, }, }, - wantErr: true, + wantOut: "test interactive mode\n", }, { - name: "filename selected", + name: "no arguments notty", + isTTY: false, + wantErr: "gist ID or URL required when not running interactively", + }, + { + name: "filename selected", + isTTY: true, opts: &ViewOptions{ Selector: "1234", Filename: "cicada.txt", @@ -182,7 +193,8 @@ func Test_viewRun(t *testing.T) { wantOut: "bwhiizzzbwhuiiizzzz\n", }, { - name: "filename selected, raw", + name: "filename selected, raw", + isTTY: true, opts: &ViewOptions{ Selector: "1234", Filename: "cicada.txt", @@ -204,7 +216,8 @@ func Test_viewRun(t *testing.T) { wantOut: "bwhiizzzbwhuiiizzzz\n", }, { - name: "multiple files, no description", + name: "multiple files, no description", + isTTY: true, opts: &ViewOptions{ Selector: "1234", ListFiles: false, @@ -224,7 +237,8 @@ func Test_viewRun(t *testing.T) { wantOut: "cicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n\n # foo \n\n", }, { - name: "multiple files, trailing newlines", + name: "multiple files, trailing newlines", + isTTY: true, opts: &ViewOptions{ Selector: "1234", ListFiles: false, @@ -244,7 +258,8 @@ func Test_viewRun(t *testing.T) { wantOut: "cicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.txt\n\nbar\n", }, { - name: "multiple files, description", + name: "multiple files, description", + isTTY: true, opts: &ViewOptions{ Selector: "1234", ListFiles: false, @@ -265,7 +280,8 @@ func Test_viewRun(t *testing.T) { wantOut: "some files\n\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n\n \n • foo \n\n", }, { - name: "multiple files, raw", + name: "multiple files, raw", + isTTY: true, opts: &ViewOptions{ Selector: "1234", Raw: true, @@ -287,7 +303,8 @@ func Test_viewRun(t *testing.T) { wantOut: "some files\n\ncicada.txt\n\nbwhiizzzbwhuiiizzzz\n\nfoo.md\n\n- foo\n", }, { - name: "one file, list files", + name: "one file, list files", + isTTY: true, opts: &ViewOptions{ Selector: "1234", Raw: false, @@ -305,7 +322,8 @@ func Test_viewRun(t *testing.T) { wantOut: "cicada.txt\n", }, { - name: "multiple file, list files", + name: "multiple file, list files", + isTTY: true, opts: &ViewOptions{ Selector: "1234", Raw: false, @@ -377,16 +395,20 @@ func Test_viewRun(t *testing.T) { } ios, _, stdout, _ := iostreams.Test() - ios.SetStdoutTTY(true) + ios.SetStdoutTTY(tt.isTTY) + ios.SetStdinTTY(tt.isTTY) + ios.SetStderrTTY(tt.isTTY) + tt.opts.IO = ios t.Run(tt.name, func(t *testing.T) { err := viewRun(tt.opts) - if tt.wantErr { - assert.Error(t, err) + if tt.wantErr != "" { + require.EqualError(t, err, tt.wantErr) return + } else { + require.NoError(t, err) } - assert.NoError(t, err) assert.Equal(t, tt.wantOut, stdout.String()) reg.Verify(t) From c0f6eb0598bad1463f8dca4b574c69342d653aa0 Mon Sep 17 00:00:00 2001 From: Mateus Marquezini Date: Mon, 13 Jan 2025 11:39:48 -0300 Subject: [PATCH 08/12] #10042: Attempt to add a new test scenario for handling error messages when TTY is unavailable --- pkg/cmd/gist/edit/edit_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/gist/edit/edit_test.go b/pkg/cmd/gist/edit/edit_test.go index 0bf7cb51a..be610b6b4 100644 --- a/pkg/cmd/gist/edit/edit_test.go +++ b/pkg/cmd/gist/edit/edit_test.go @@ -520,8 +520,11 @@ func Test_editRun(t *testing.T) { }, }, { - name: "no arguments notty", - nontty: true, + name: "no arguments notty", + nontty: true, + opts: &EditOptions{ + Selector: "", + }, wantErr: "gist ID or URL required when not running interactively", }, } From 35d81e3ff596071005ce07d39c0e8e5a4d09fb50 Mon Sep 17 00:00:00 2001 From: Kynan Ware <47394200+BagToad@users.noreply.github.com> Date: Thu, 16 Jan 2025 10:03:48 -0700 Subject: [PATCH 09/12] Fix: Gist edit tests for interactivity This changes the gist edit tests to use the positive `istty` instead of the previous inverse `nontty`, which is consistent with the way other commands are written. --- pkg/cmd/gist/edit/edit_test.go | 223 ++++++++++++++++++++++----------- 1 file changed, 148 insertions(+), 75 deletions(-) diff --git a/pkg/cmd/gist/edit/edit_test.go b/pkg/cmd/gist/edit/edit_test.go index be610b6b4..a40695c39 100644 --- a/pkg/cmd/gist/edit/edit_test.go +++ b/pkg/cmd/gist/edit/edit_test.go @@ -3,11 +3,13 @@ package edit import ( "bytes" "encoding/json" + "fmt" "io" "net/http" "os" "path/filepath" "testing" + "time" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/gh" @@ -141,23 +143,31 @@ func Test_editRun(t *testing.T) { require.NoError(t, err) tests := []struct { - name string - opts *EditOptions - gist *shared.Gist - httpStubs func(*httpmock.Registry) - prompterStubs func(*prompter.MockPrompter) - nontty bool - stdin string - wantErr string - wantParams map[string]interface{} + name string + opts *EditOptions + mockGist *shared.Gist + mockGistList bool + httpStubs func(*httpmock.Registry) + prompterStubs func(*prompter.MockPrompter) + istty bool + stdin string + wantErr string + wantLastRequestParameters map[string]interface{} }{ { name: "no such gist", wantErr: "gist not found: 1234", + opts: &EditOptions{ + Selector: "1234", + }, }, { - name: "one file", - gist: &shared.Gist{ + name: "one file", + istty: false, + opts: &EditOptions{ + Selector: "1234", + }, + mockGist: &shared.Gist{ ID: "1234", Files: map[string]*shared.GistFile{ "cicada.txt": { @@ -172,7 +182,7 @@ func Test_editRun(t *testing.T) { reg.Register(httpmock.REST("POST", "gists/1234"), httpmock.StatusStringResponse(201, "{}")) }, - wantParams: map[string]interface{}{ + wantLastRequestParameters: map[string]interface{}{ "description": "", "files": map[string]interface{}{ "cicada.txt": map[string]interface{}{ @@ -183,7 +193,9 @@ func Test_editRun(t *testing.T) { }, }, { - name: "multiple files, submit", + name: "multiple files, submit, with TTY", + istty: true, + mockGistList: true, prompterStubs: func(pm *prompter.MockPrompter) { pm.RegisterSelect("Edit which file?", []string{"cicada.txt", "unix.md"}, @@ -196,7 +208,7 @@ func Test_editRun(t *testing.T) { return prompter.IndexFor(opts, "Submit") }) }, - gist: &shared.Gist{ + mockGist: &shared.Gist{ ID: "1234", Description: "catbug", Files: map[string]*shared.GistFile{ @@ -215,7 +227,7 @@ func Test_editRun(t *testing.T) { reg.Register(httpmock.REST("POST", "gists/1234"), httpmock.StatusStringResponse(201, "{}")) }, - wantParams: map[string]interface{}{ + wantLastRequestParameters: map[string]interface{}{ "description": "catbug", "files": map[string]interface{}{ "cicada.txt": map[string]interface{}{ @@ -230,7 +242,11 @@ func Test_editRun(t *testing.T) { }, }, { - name: "multiple files, cancel", + name: "multiple files, cancel", + istty: true, + opts: &EditOptions{ + Selector: "1234", + }, prompterStubs: func(pm *prompter.MockPrompter) { pm.RegisterSelect("Edit which file?", []string{"cicada.txt", "unix.md"}, @@ -244,7 +260,7 @@ func Test_editRun(t *testing.T) { }) }, wantErr: "CancelError", - gist: &shared.Gist{ + mockGist: &shared.Gist{ ID: "1234", Files: map[string]*shared.GistFile{ "cicada.txt": { @@ -263,7 +279,10 @@ func Test_editRun(t *testing.T) { }, { name: "not change", - gist: &shared.Gist{ + opts: &EditOptions{ + Selector: "1234", + }, + mockGist: &shared.Gist{ ID: "1234", Files: map[string]*shared.GistFile{ "cicada.txt": { @@ -277,7 +296,10 @@ func Test_editRun(t *testing.T) { }, { name: "another user's gist", - gist: &shared.Gist{ + opts: &EditOptions{ + Selector: "1234", + }, + mockGist: &shared.Gist{ ID: "1234", Files: map[string]*shared.GistFile{ "cicada.txt": { @@ -292,7 +314,11 @@ func Test_editRun(t *testing.T) { }, { name: "add file to existing gist", - gist: &shared.Gist{ + opts: &EditOptions{ + AddFilename: fileToAdd, + Selector: "1234", + }, + mockGist: &shared.Gist{ ID: "1234", Files: map[string]*shared.GistFile{ "sample.txt": { @@ -307,16 +333,14 @@ func Test_editRun(t *testing.T) { reg.Register(httpmock.REST("POST", "gists/1234"), httpmock.StatusStringResponse(201, "{}")) }, - opts: &EditOptions{ - AddFilename: fileToAdd, - }, }, { name: "change description", opts: &EditOptions{ Description: "my new description", + Selector: "1234", }, - gist: &shared.Gist{ + mockGist: &shared.Gist{ ID: "1234", Description: "my old description", Files: map[string]*shared.GistFile{ @@ -331,7 +355,7 @@ func Test_editRun(t *testing.T) { reg.Register(httpmock.REST("POST", "gists/1234"), httpmock.StatusStringResponse(201, "{}")) }, - wantParams: map[string]interface{}{ + wantLastRequestParameters: map[string]interface{}{ "description": "my new description", "files": map[string]interface{}{ "sample.txt": map[string]interface{}{ @@ -343,7 +367,12 @@ func Test_editRun(t *testing.T) { }, { name: "add file to existing gist from source parameter", - gist: &shared.Gist{ + opts: &EditOptions{ + AddFilename: "from_source.txt", + SourceFile: fileToAdd, + Selector: "1234", + }, + mockGist: &shared.Gist{ ID: "1234", Files: map[string]*shared.GistFile{ "sample.txt": { @@ -358,11 +387,7 @@ func Test_editRun(t *testing.T) { reg.Register(httpmock.REST("POST", "gists/1234"), httpmock.StatusStringResponse(201, "{}")) }, - opts: &EditOptions{ - AddFilename: "from_source.txt", - SourceFile: fileToAdd, - }, - wantParams: map[string]interface{}{ + wantLastRequestParameters: map[string]interface{}{ "description": "", "files": map[string]interface{}{ "from_source.txt": map[string]interface{}{ @@ -374,7 +399,12 @@ func Test_editRun(t *testing.T) { }, { name: "add file to existing gist from stdin", - gist: &shared.Gist{ + opts: &EditOptions{ + AddFilename: "from_source.txt", + SourceFile: "-", + Selector: "1234", + }, + mockGist: &shared.Gist{ ID: "1234", Files: map[string]*shared.GistFile{ "sample.txt": { @@ -389,12 +419,8 @@ func Test_editRun(t *testing.T) { reg.Register(httpmock.REST("POST", "gists/1234"), httpmock.StatusStringResponse(201, "{}")) }, - opts: &EditOptions{ - AddFilename: "from_source.txt", - SourceFile: "-", - }, stdin: "data from stdin", - wantParams: map[string]interface{}{ + wantLastRequestParameters: map[string]interface{}{ "description": "", "files": map[string]interface{}{ "from_source.txt": map[string]interface{}{ @@ -406,7 +432,11 @@ func Test_editRun(t *testing.T) { }, { name: "remove file, file does not exist", - gist: &shared.Gist{ + opts: &EditOptions{ + RemoveFilename: "sample2.txt", + Selector: "1234", + }, + mockGist: &shared.Gist{ ID: "1234", Files: map[string]*shared.GistFile{ "sample.txt": { @@ -417,14 +447,15 @@ func Test_editRun(t *testing.T) { }, Owner: &shared.GistOwner{Login: "octocat"}, }, - opts: &EditOptions{ - RemoveFilename: "sample2.txt", - }, wantErr: "gist has no file \"sample2.txt\"", }, { name: "remove file from existing gist", - gist: &shared.Gist{ + opts: &EditOptions{ + RemoveFilename: "sample2.txt", + Selector: "1234", + }, + mockGist: &shared.Gist{ ID: "1234", Files: map[string]*shared.GistFile{ "sample.txt": { @@ -444,10 +475,7 @@ func Test_editRun(t *testing.T) { reg.Register(httpmock.REST("POST", "gists/1234"), httpmock.StatusStringResponse(201, "{}")) }, - opts: &EditOptions{ - RemoveFilename: "sample2.txt", - }, - wantParams: map[string]interface{}{ + wantLastRequestParameters: map[string]interface{}{ "description": "", "files": map[string]interface{}{ "sample.txt": map[string]interface{}{ @@ -460,7 +488,11 @@ func Test_editRun(t *testing.T) { }, { name: "edit gist using file from source parameter", - gist: &shared.Gist{ + opts: &EditOptions{ + SourceFile: fileToAdd, + Selector: "1234", + }, + mockGist: &shared.Gist{ ID: "1234", Files: map[string]*shared.GistFile{ "sample.txt": { @@ -475,10 +507,7 @@ func Test_editRun(t *testing.T) { reg.Register(httpmock.REST("POST", "gists/1234"), httpmock.StatusStringResponse(201, "{}")) }, - opts: &EditOptions{ - SourceFile: fileToAdd, - }, - wantParams: map[string]interface{}{ + wantLastRequestParameters: map[string]interface{}{ "description": "", "files": map[string]interface{}{ "sample.txt": map[string]interface{}{ @@ -490,7 +519,11 @@ func Test_editRun(t *testing.T) { }, { name: "edit gist using stdin", - gist: &shared.Gist{ + opts: &EditOptions{ + SourceFile: "-", + Selector: "1234", + }, + mockGist: &shared.Gist{ ID: "1234", Files: map[string]*shared.GistFile{ "sample.txt": { @@ -505,11 +538,8 @@ func Test_editRun(t *testing.T) { reg.Register(httpmock.REST("POST", "gists/1234"), httpmock.StatusStringResponse(201, "{}")) }, - opts: &EditOptions{ - SourceFile: "-", - }, stdin: "data from stdin", - wantParams: map[string]interface{}{ + wantLastRequestParameters: map[string]interface{}{ "description": "", "files": map[string]interface{}{ "sample.txt": map[string]interface{}{ @@ -520,8 +550,8 @@ func Test_editRun(t *testing.T) { }, }, { - name: "no arguments notty", - nontty: true, + name: "no arguments notty", + istty: false, opts: &EditOptions{ Selector: "", }, @@ -531,24 +561,62 @@ func Test_editRun(t *testing.T) { for _, tt := range tests { reg := &httpmock.Registry{} - if tt.gist == nil { + pm := prompter.NewMockPrompter(t) + + if tt.opts == nil { + tt.opts = &EditOptions{} + } + + if tt.opts.Selector != "" { + // Only register the HTTP stubs for a direct gist lookup if a selector is provided. + if tt.mockGist == nil { + // If no gist is provided, we expect a 404. + reg.Register(httpmock.REST("GET", fmt.Sprintf("gists/%s", tt.opts.Selector)), + httpmock.StatusStringResponse(404, "Not Found")) + } else { + // If a gist is provided, we expect the gist to be fetched. + reg.Register(httpmock.REST("GET", fmt.Sprintf("gists/%s", tt.opts.Selector)), + httpmock.JSONResponse(tt.mockGist)) + reg.Register(httpmock.GraphQL(`query UserCurrent\b`), + httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`)) + } + } + + if tt.mockGistList { + sixHours, _ := time.ParseDuration("6h") + sixHoursAgo := time.Now().Add(-sixHours) + reg.Register(httpmock.GraphQL(`query GistList\b`), + httpmock.StringResponse( + fmt.Sprintf(`{ "data": { "viewer": { "gists": { "nodes": [ + { + "description": "whatever", + "files": [{ "name": "cicada.txt" }, { "name": "unix.md" }], + "isPublic": true, + "name": "1234", + "updatedAt": "%s" + } + ], + "pageInfo": { + "hasNextPage": false, + "endCursor": "somevaluedoesnotmatter" + } } } } }`, sixHoursAgo.Format(time.RFC3339)))) reg.Register(httpmock.REST("GET", "gists/1234"), - httpmock.StatusStringResponse(404, "Not Found")) - } else { - reg.Register(httpmock.REST("GET", "gists/1234"), - httpmock.JSONResponse(tt.gist)) + httpmock.JSONResponse(tt.mockGist)) reg.Register(httpmock.GraphQL(`query UserCurrent\b`), httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`)) + + gistList := "cicada.txt whatever about 6 hours ago" + pm.RegisterSelect("Select a gist", + []string{gistList}, + func(_, _ string, opts []string) (int, error) { + return prompter.IndexFor(opts, gistList) + }) } if tt.httpStubs != nil { tt.httpStubs(reg) } - if tt.opts == nil { - tt.opts = &EditOptions{} - } - tt.opts.Edit = func(_, _, _ string, _ *iostreams.IOStreams) (string, error) { return "new file content", nil } @@ -558,19 +626,17 @@ func Test_editRun(t *testing.T) { } ios, stdin, stdout, stderr := iostreams.Test() stdin.WriteString(tt.stdin) - ios.SetStdoutTTY(!tt.nontty) - ios.SetStdinTTY(!tt.nontty) - ios.SetStderrTTY(!tt.nontty) + ios.SetStdoutTTY(tt.istty) + ios.SetStdinTTY(tt.istty) + ios.SetStderrTTY(tt.istty) tt.opts.IO = ios - tt.opts.Selector = "1234" tt.opts.Config = func() (gh.Config, error) { return config.NewBlankConfig(), nil } t.Run(tt.name, func(t *testing.T) { - pm := prompter.NewMockPrompter(t) if tt.prompterStubs != nil { tt.prompterStubs(pm) } @@ -584,14 +650,21 @@ func Test_editRun(t *testing.T) { } assert.NoError(t, err) - if tt.wantParams != nil { - bodyBytes, _ := io.ReadAll(reg.Requests[2].Body) + if tt.wantLastRequestParameters != nil { + // Currently only checking that the last request has + // the expected request parameters. + // + // This might need to be changed, if a test were to be added + // that needed to check that a request other than the last + // has the desired parameters. + lastRequest := reg.Requests[len(reg.Requests)-1] + bodyBytes, _ := io.ReadAll(lastRequest.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.wantParams, reqBody) + assert.Equal(t, tt.wantLastRequestParameters, reqBody) } assert.Equal(t, "", stdout.String()) From dec46670bb356a214e0ae4ecc8fd689a55df5214 Mon Sep 17 00:00:00 2001 From: Kynan Ware <47394200+BagToad@users.noreply.github.com> Date: Thu, 16 Jan 2025 11:14:33 -0700 Subject: [PATCH 10/12] Fix: gist edit/view tests var name consistency --- pkg/cmd/gist/edit/edit_test.go | 16 ++++++++-------- pkg/cmd/gist/view/view_test.go | 28 ++++++++++++++-------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/pkg/cmd/gist/edit/edit_test.go b/pkg/cmd/gist/edit/edit_test.go index a40695c39..5a3f0a268 100644 --- a/pkg/cmd/gist/edit/edit_test.go +++ b/pkg/cmd/gist/edit/edit_test.go @@ -149,7 +149,7 @@ func Test_editRun(t *testing.T) { mockGistList bool httpStubs func(*httpmock.Registry) prompterStubs func(*prompter.MockPrompter) - istty bool + isTTY bool stdin string wantErr string wantLastRequestParameters map[string]interface{} @@ -163,7 +163,7 @@ func Test_editRun(t *testing.T) { }, { name: "one file", - istty: false, + isTTY: false, opts: &EditOptions{ Selector: "1234", }, @@ -194,7 +194,7 @@ func Test_editRun(t *testing.T) { }, { name: "multiple files, submit, with TTY", - istty: true, + isTTY: true, mockGistList: true, prompterStubs: func(pm *prompter.MockPrompter) { pm.RegisterSelect("Edit which file?", @@ -243,7 +243,7 @@ func Test_editRun(t *testing.T) { }, { name: "multiple files, cancel", - istty: true, + isTTY: true, opts: &EditOptions{ Selector: "1234", }, @@ -551,7 +551,7 @@ func Test_editRun(t *testing.T) { }, { name: "no arguments notty", - istty: false, + isTTY: false, opts: &EditOptions{ Selector: "", }, @@ -626,9 +626,9 @@ func Test_editRun(t *testing.T) { } ios, stdin, stdout, stderr := iostreams.Test() stdin.WriteString(tt.stdin) - ios.SetStdoutTTY(tt.istty) - ios.SetStdinTTY(tt.istty) - ios.SetStderrTTY(tt.istty) + ios.SetStdoutTTY(tt.isTTY) + ios.SetStdinTTY(tt.isTTY) + ios.SetStderrTTY(tt.isTTY) tt.opts.IO = ios diff --git a/pkg/cmd/gist/view/view_test.go b/pkg/cmd/gist/view/view_test.go index 820401f1e..706b85f10 100644 --- a/pkg/cmd/gist/view/view_test.go +++ b/pkg/cmd/gist/view/view_test.go @@ -116,10 +116,10 @@ func Test_viewRun(t *testing.T) { name string opts *ViewOptions wantOut string - gist *shared.Gist + mockGist *shared.Gist + mockGistList bool isTTY bool wantErr string - mockGistList bool }{ { name: "no such gist", @@ -137,7 +137,7 @@ func Test_viewRun(t *testing.T) { Selector: "1234", ListFiles: false, }, - gist: &shared.Gist{ + mockGist: &shared.Gist{ Files: map[string]*shared.GistFile{ "cicada.txt": { Content: "bwhiizzzbwhuiiizzzz", @@ -155,7 +155,7 @@ func Test_viewRun(t *testing.T) { ListFiles: false, }, mockGistList: true, - gist: &shared.Gist{ + mockGist: &shared.Gist{ Files: map[string]*shared.GistFile{ "cicada.txt": { Content: "test interactive mode", @@ -178,7 +178,7 @@ func Test_viewRun(t *testing.T) { Filename: "cicada.txt", ListFiles: false, }, - gist: &shared.Gist{ + mockGist: &shared.Gist{ Files: map[string]*shared.GistFile{ "cicada.txt": { Content: "bwhiizzzbwhuiiizzzz", @@ -201,7 +201,7 @@ func Test_viewRun(t *testing.T) { Raw: true, ListFiles: false, }, - gist: &shared.Gist{ + mockGist: &shared.Gist{ Files: map[string]*shared.GistFile{ "cicada.txt": { Content: "bwhiizzzbwhuiiizzzz", @@ -222,7 +222,7 @@ func Test_viewRun(t *testing.T) { Selector: "1234", ListFiles: false, }, - gist: &shared.Gist{ + mockGist: &shared.Gist{ Files: map[string]*shared.GistFile{ "cicada.txt": { Content: "bwhiizzzbwhuiiizzzz", @@ -243,7 +243,7 @@ func Test_viewRun(t *testing.T) { Selector: "1234", ListFiles: false, }, - gist: &shared.Gist{ + mockGist: &shared.Gist{ Files: map[string]*shared.GistFile{ "cicada.txt": { Content: "bwhiizzzbwhuiiizzzz\n", @@ -264,7 +264,7 @@ func Test_viewRun(t *testing.T) { Selector: "1234", ListFiles: false, }, - gist: &shared.Gist{ + mockGist: &shared.Gist{ Description: "some files", Files: map[string]*shared.GistFile{ "cicada.txt": { @@ -287,7 +287,7 @@ func Test_viewRun(t *testing.T) { Raw: true, ListFiles: false, }, - gist: &shared.Gist{ + mockGist: &shared.Gist{ Description: "some files", Files: map[string]*shared.GistFile{ "cicada.txt": { @@ -310,7 +310,7 @@ func Test_viewRun(t *testing.T) { Raw: false, ListFiles: true, }, - gist: &shared.Gist{ + mockGist: &shared.Gist{ Description: "some files", Files: map[string]*shared.GistFile{ "cicada.txt": { @@ -329,7 +329,7 @@ func Test_viewRun(t *testing.T) { Raw: false, ListFiles: true, }, - gist: &shared.Gist{ + mockGist: &shared.Gist{ Description: "some files", Files: map[string]*shared.GistFile{ "cicada.txt": { @@ -348,12 +348,12 @@ func Test_viewRun(t *testing.T) { for _, tt := range tests { reg := &httpmock.Registry{} - if tt.gist == nil { + if tt.mockGist == nil { reg.Register(httpmock.REST("GET", "gists/1234"), httpmock.StatusStringResponse(404, "Not Found")) } else { reg.Register(httpmock.REST("GET", "gists/1234"), - httpmock.JSONResponse(tt.gist)) + httpmock.JSONResponse(tt.mockGist)) } if tt.opts == nil { From 5b6fd53a88498caab47c0e24e06c427f53574814 Mon Sep 17 00:00:00 2001 From: Kynan Ware <47394200+BagToad@users.noreply.github.com> Date: Thu, 16 Jan 2025 11:36:48 -0700 Subject: [PATCH 11/12] Fix: gist edit test name --- pkg/cmd/gist/edit/edit_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/gist/edit/edit_test.go b/pkg/cmd/gist/edit/edit_test.go index 5a3f0a268..728f6e894 100644 --- a/pkg/cmd/gist/edit/edit_test.go +++ b/pkg/cmd/gist/edit/edit_test.go @@ -242,7 +242,7 @@ func Test_editRun(t *testing.T) { }, }, { - name: "multiple files, cancel", + name: "multiple files, cancel, with TTY", isTTY: true, opts: &EditOptions{ Selector: "1234", From facd0caa2971ad3620f451d1968e04aab2cce17c Mon Sep 17 00:00:00 2001 From: Kynan Ware <47394200+BagToad@users.noreply.github.com> Date: Thu, 16 Jan 2025 11:44:46 -0700 Subject: [PATCH 12/12] Fix: accidental whitespace in gist edit --- pkg/cmd/gist/edit/edit.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/cmd/gist/edit/edit.go b/pkg/cmd/gist/edit/edit.go index b85118937..e0d6e3b84 100644 --- a/pkg/cmd/gist/edit/edit.go +++ b/pkg/cmd/gist/edit/edit.go @@ -113,7 +113,6 @@ func editRun(opts *EditOptions) error { } gist, err := shared.PromptGists(opts.Prompter, client, host, cs) - if err != nil { return err }