From eb8bfcfe96d63fffa07528e576d3f1d46c48408f Mon Sep 17 00:00:00 2001 From: zhangzhengyuan Date: Mon, 20 Feb 2023 16:14:32 -0800 Subject: [PATCH] use prompter for pkg/cmd/repo/unarchive --- pkg/cmd/repo/unarchive/unarchive.go | 16 ++++----- pkg/cmd/repo/unarchive/unarchive_test.go | 45 ++++++++++++++---------- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/pkg/cmd/repo/unarchive/unarchive.go b/pkg/cmd/repo/unarchive/unarchive.go index 9fdb6c759..8cd1c1a03 100644 --- a/pkg/cmd/repo/unarchive/unarchive.go +++ b/pkg/cmd/repo/unarchive/unarchive.go @@ -5,14 +5,13 @@ import ( "net/http" "strings" - "github.com/AlecAivazis/survey/v2" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/spf13/cobra" ) @@ -23,6 +22,7 @@ type UnarchiveOptions struct { Confirmed bool IO *iostreams.IOStreams RepoArg string + Prompter prompter.Prompter } func NewCmdUnarchive(f *cmdutil.Factory, runF func(*UnarchiveOptions) error) *cobra.Command { @@ -31,6 +31,7 @@ func NewCmdUnarchive(f *cmdutil.Factory, runF func(*UnarchiveOptions) error) *co HttpClient: f.HttpClient, Config: f.Config, BaseRepo: f.BaseRepo, + Prompter: f.Prompter, } cmd := &cobra.Command{ @@ -114,16 +115,11 @@ func unarchiveRun(opts *UnarchiveOptions) error { } if !opts.Confirmed { - p := &survey.Confirm{ - Message: fmt.Sprintf("Unarchive %s?", fullName), - Default: false, - } - //nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter - err = prompt.SurveyAskOne(p, &opts.Confirmed) + confirmed, err := opts.Prompter.Confirm(fmt.Sprintf("Unarchive %s?", fullName), false) if err != nil { - return fmt.Errorf("failed to prompt: %w", err) + return err } - if !opts.Confirmed { + if !confirmed { return cmdutil.CancelError } } diff --git a/pkg/cmd/repo/unarchive/unarchive_test.go b/pkg/cmd/repo/unarchive/unarchive_test.go index a0b1df103..cae1dfb46 100644 --- a/pkg/cmd/repo/unarchive/unarchive_test.go +++ b/pkg/cmd/repo/unarchive/unarchive_test.go @@ -7,10 +7,10 @@ import ( "testing" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/prompter" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/cli/v2/pkg/prompt" "github.com/google/shlex" "github.com/stretchr/testify/assert" ) @@ -68,20 +68,24 @@ func TestNewCmdUnarchive(t *testing.T) { func Test_UnarchiveRun(t *testing.T) { queryResponse := `{ "data": { "repository": { "id": "THE-ID","isArchived": %s} } }` tests := []struct { - name string - opts UnarchiveOptions - httpStubs func(*httpmock.Registry) - askStubs func(*prompt.AskStubber) - isTTY bool - wantStdout string - wantStderr string + name string + opts UnarchiveOptions + httpStubs func(*httpmock.Registry) + prompterStubs func(*prompter.PrompterMock) + isTTY bool + wantStdout string + wantStderr string }{ { name: "archived repo tty", wantStdout: "✓ Unarchived repository OWNER/REPO\n", - askStubs: func(q *prompt.AskStubber) { - //nolint:staticcheck // SA1019: q.StubOne is deprecated: use StubPrompt - q.StubOne(true) + prompterStubs: func(pm *prompter.PrompterMock) { + pm.ConfirmFunc = func(p string, d bool) (bool, error) { + if p == "Unarchive OWNER/REPO?" { + return true, nil + } + return false, prompter.NoSuchPromptErr(p) + } }, isTTY: true, opts: UnarchiveOptions{RepoArg: "OWNER/REPO"}, @@ -98,9 +102,13 @@ func Test_UnarchiveRun(t *testing.T) { name: "infer base repo", wantStdout: "✓ Unarchived repository OWNER/REPO\n", opts: UnarchiveOptions{}, - askStubs: func(q *prompt.AskStubber) { - //nolint:staticcheck // SA1019: q.StubOne is deprecated: use StubPrompt - q.StubOne(true) + prompterStubs: func(pm *prompter.PrompterMock) { + pm.ConfirmFunc = func(p string, d bool) (bool, error) { + if p == "Unarchive OWNER/REPO?" { + return true, nil + } + return false, prompter.NoSuchPromptErr(p) + } }, isTTY: true, httpStubs: func(reg *httpmock.Registry) { @@ -140,12 +148,11 @@ func Test_UnarchiveRun(t *testing.T) { ios, _, stdout, stderr := iostreams.Test() tt.opts.IO = ios - //nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber - q, teardown := prompt.InitAskStubber() - defer teardown() - if tt.askStubs != nil { - tt.askStubs(q) + pm := &prompter.PrompterMock{} + if tt.prompterStubs != nil { + tt.prompterStubs(pm) } + tt.opts.Prompter = pm t.Run(tt.name, func(t *testing.T) { defer reg.Verify(t)