From c536114de0fd6308b215ed3eaec0c8474ffbc0c2 Mon Sep 17 00:00:00 2001 From: nate smith Date: Mon, 10 Apr 2023 17:38:30 -0700 Subject: [PATCH] use new prompter in run download --- pkg/cmd/run/download/download.go | 25 ++++--------- pkg/cmd/run/download/download_test.go | 51 +++++++++------------------ 2 files changed, 23 insertions(+), 53 deletions(-) diff --git a/pkg/cmd/run/download/download.go b/pkg/cmd/run/download/download.go index 25eec5d62..4c5322806 100644 --- a/pkg/cmd/run/download/download.go +++ b/pkg/cmd/run/download/download.go @@ -5,12 +5,10 @@ import ( "fmt" "path/filepath" - "github.com/AlecAivazis/survey/v2" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/pkg/cmd/run/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/cli/cli/v2/pkg/set" "github.com/spf13/cobra" ) @@ -18,7 +16,7 @@ import ( type DownloadOptions struct { IO *iostreams.IOStreams Platform platform - Prompter prompter + Prompter iprompter DoPrompt bool RunID string @@ -31,13 +29,14 @@ type platform interface { List(runID string) ([]shared.Artifact, error) Download(url string, dir string) error } -type prompter interface { - Prompt(message string, options []string, result interface{}) error +type iprompter interface { + MultiSelect(string, []string, []string) ([]string, error) } func NewCmdDownload(f *cmdutil.Factory, runF func(*DownloadOptions) error) *cobra.Command { opts := &DownloadOptions{ - IO: f.IOStreams, + IO: f.IOStreams, + Prompter: f.Prompter, } cmd := &cobra.Command{ @@ -85,7 +84,6 @@ func NewCmdDownload(f *cmdutil.Factory, runF func(*DownloadOptions) error) *cobr client: httpClient, repo: baseRepo, } - opts.Prompter = &surveyPrompter{} if runF != nil { return runF(opts) @@ -133,8 +131,7 @@ func runDownload(opts *DownloadOptions) error { if len(options) > 10 { options = options[:10] } - err := opts.Prompter.Prompt("Select artifacts to download:", options, &wantNames) - if err != nil { + if wantNames, err = opts.Prompter.MultiSelect("Select artifacts to download:", nil, options); err != nil { return err } if len(wantNames) == 0 { @@ -194,13 +191,3 @@ func matchAnyPattern(patterns []string, name string) bool { } return false } - -type surveyPrompter struct{} - -func (sp *surveyPrompter) Prompt(message string, options []string, result interface{}) error { - //nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter - return prompt.SurveyAskOne(&survey.MultiSelect{ - Message: message, - Options: options, - }, result) -} diff --git a/pkg/cmd/run/download/download_test.go b/pkg/cmd/run/download/download_test.go index 10c7bbe3c..3640f93a0 100644 --- a/pkg/cmd/run/download/download_test.go +++ b/pkg/cmd/run/download/download_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmd/run/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" @@ -144,11 +145,11 @@ func Test_NewCmdDownload(t *testing.T) { func Test_runDownload(t *testing.T) { tests := []struct { - name string - opts DownloadOptions - mockAPI func(*mockPlatform) - mockPrompt func(*mockPrompter) - wantErr string + name string + opts DownloadOptions + mockAPI func(*mockPlatform) + promptStubs func(*prompter.MockPrompter) + wantErr string }{ { name: "download non-expired", @@ -281,13 +282,11 @@ func Test_runDownload(t *testing.T) { }, nil) p.On("Download", "http://download.com/artifact2.zip", ".").Return(nil) }, - mockPrompt: func(p *mockPrompter) { - p.On("Prompt", "Select artifacts to download:", []string{"artifact-1", "artifact-2"}, mock.AnythingOfType("*[]string")). - Run(func(args mock.Arguments) { - result := args.Get(2).(*[]string) - *result = []string{"artifact-2"} - }). - Return(nil) + promptStubs: func(pm *prompter.MockPrompter) { + pm.RegisterMultiSelect("Select artifacts to download:", nil, []string{"artifact-1", "artifact-2"}, + func(_ string, _, opts []string) ([]string, error) { + return []string{"artifact-2"}, nil + }) }, }, } @@ -297,7 +296,12 @@ func Test_runDownload(t *testing.T) { ios, _, stdout, stderr := iostreams.Test() opts.IO = ios opts.Platform = newMockPlatform(t, tt.mockAPI) - opts.Prompter = newMockPrompter(t, tt.mockPrompt) + + pm := prompter.NewMockPrompter(t) + opts.Prompter = pm + if tt.promptStubs != nil { + tt.promptStubs(pm) + } err := runDownload(opts) if tt.wantErr != "" { @@ -337,24 +341,3 @@ func (p *mockPlatform) Download(url string, dir string) error { args := p.Called(url, dir) return args.Error(0) } - -type mockPrompter struct { - mock.Mock -} - -func newMockPrompter(t *testing.T, config func(*mockPrompter)) *mockPrompter { - m := &mockPrompter{} - m.Test(t) - t.Cleanup(func() { - m.AssertExpectations(t) - }) - if config != nil { - config(m) - } - return m -} - -func (p *mockPrompter) Prompt(msg string, opts []string, res interface{}) error { - args := p.Called(msg, opts, res) - return args.Error(0) -}