use new prompter in repo fork

This commit is contained in:
Nate Smith 2023-08-16 16:02:14 -05:00
parent e4cddb562e
commit 2ddfc3827d
2 changed files with 47 additions and 40 deletions

View file

@ -19,13 +19,16 @@ import (
"github.com/cli/cli/v2/pkg/cmd/repo/shared"
"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"
"github.com/spf13/pflag"
)
const defaultRemoteName = "origin"
type iprompter interface {
Confirm(string, bool) (bool, error)
}
type ForkOptions struct {
HttpClient func() (*http.Client, error)
GitClient *git.Client
@ -35,6 +38,7 @@ type ForkOptions struct {
Remotes func() (ghContext.Remotes, error)
Since func(time.Time) time.Duration
BackOff backoff.BackOff
Prompter iprompter
GitArgs []string
Repository string
@ -65,6 +69,7 @@ func NewCmdFork(f *cmdutil.Factory, runF func(*ForkOptions) error) *cobra.Comman
Config: f.Config,
BaseRepo: f.BaseRepo,
Remotes: f.Remotes,
Prompter: f.Prompter,
Since: time.Since,
}
@ -273,10 +278,9 @@ func forkRun(opts *ForkOptions) error {
remoteDesired := opts.Remote
if opts.PromptRemote {
//nolint:staticcheck // SA1019: prompt.Confirm is deprecated: use Prompter
err = prompt.Confirm("Would you like to add a remote for the fork?", &remoteDesired)
remoteDesired, err = opts.Prompter.Confirm("Would you like to add a remote for the fork?", false)
if err != nil {
return fmt.Errorf("failed to prompt: %w", err)
return err
}
}
@ -317,10 +321,9 @@ func forkRun(opts *ForkOptions) error {
} else {
cloneDesired := opts.Clone
if opts.PromptClone {
//nolint:staticcheck // SA1019: prompt.Confirm is deprecated: use Prompter
err = prompt.Confirm("Would you like to clone the fork?", &cloneDesired)
cloneDesired, err = opts.Prompter.Confirm("Would you like to clone the fork?", false)
if err != nil {
return fmt.Errorf("failed to prompt: %w", err)
return err
}
}
if cloneDesired {

View file

@ -14,11 +14,11 @@ import (
"github.com/cli/cli/v2/git"
"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/internal/run"
"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"
)
@ -205,18 +205,18 @@ func TestRepoFork(t *testing.T) {
}
tests := []struct {
name string
opts *ForkOptions
tty bool
httpStubs func(*httpmock.Registry)
execStubs func(*run.CommandStubber)
askStubs func(*prompt.AskStubber)
cfgStubs func(*config.ConfigMock)
remotes []*context.Remote
wantOut string
wantErrOut string
wantErr bool
errMsg string
name string
opts *ForkOptions
tty bool
httpStubs func(*httpmock.Registry)
execStubs func(*run.CommandStubber)
promptStubs func(*prompter.MockPrompter)
cfgStubs func(*config.ConfigMock)
remotes []*context.Remote
wantOut string
wantErrOut string
wantErr bool
errMsg string
}{
{
name: "implicit match, configured protocol overrides provided",
@ -272,9 +272,10 @@ func TestRepoFork(t *testing.T) {
RemoteName: defaultRemoteName,
},
httpStubs: forkPost,
askStubs: func(as *prompt.AskStubber) {
//nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt
as.StubOne(false)
promptStubs: func(pm *prompter.MockPrompter) {
pm.RegisterConfirm("Would you like to add a remote for the fork?", func(_ string, _ bool) (bool, error) {
return false, nil
})
},
wantErrOut: "✓ Created fork someone/REPO\n",
},
@ -291,9 +292,10 @@ func TestRepoFork(t *testing.T) {
cs.Register("git remote rename origin upstream", 0, "")
cs.Register(`git remote add origin https://github.com/someone/REPO.git`, 0, "")
},
askStubs: func(as *prompt.AskStubber) {
//nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt
as.StubOne(true)
promptStubs: func(pm *prompter.MockPrompter) {
pm.RegisterConfirm("Would you like to add a remote for the fork?", func(_ string, _ bool) (bool, error) {
return true, nil
})
},
wantErrOut: "✓ Created fork someone/REPO\n✓ Added remote origin\n",
},
@ -497,9 +499,10 @@ func TestRepoFork(t *testing.T) {
PromptClone: true,
},
httpStubs: forkPost,
askStubs: func(as *prompt.AskStubber) {
//nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt
as.StubOne(false)
promptStubs: func(pm *prompter.MockPrompter) {
pm.RegisterConfirm("Would you like to clone the fork?", func(_ string, _ bool) (bool, error) {
return false, nil
})
},
wantErrOut: "✓ Created fork someone/REPO\n",
},
@ -511,9 +514,10 @@ func TestRepoFork(t *testing.T) {
PromptClone: true,
},
httpStubs: forkPost,
askStubs: func(as *prompt.AskStubber) {
//nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt
as.StubOne(true)
promptStubs: func(pm *prompter.MockPrompter) {
pm.RegisterConfirm("Would you like to clone the fork?", func(_ string, _ bool) (bool, error) {
return true, nil
})
},
execStubs: func(cs *run.CommandStubber) {
cs.Register(`git clone https://github.com/someone/REPO\.git`, 0, "")
@ -533,9 +537,10 @@ func TestRepoFork(t *testing.T) {
},
},
httpStubs: forkPost,
askStubs: func(as *prompt.AskStubber) {
//nolint:staticcheck // SA1019: as.StubOne is deprecated: use StubPrompt
as.StubOne(true)
promptStubs: func(pm *prompter.MockPrompter) {
pm.RegisterConfirm("Would you like to clone the fork?", func(_ string, _ bool) (bool, error) {
return true, nil
})
},
execStubs: func(cs *run.CommandStubber) {
cs.Register(`git clone https://github.com/someone/REPO\.git`, 0, "")
@ -746,11 +751,10 @@ func TestRepoFork(t *testing.T) {
GitPath: "some/path/git",
}
//nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber
as, teardown := prompt.InitAskStubber()
defer teardown()
if tt.askStubs != nil {
tt.askStubs(as)
pm := prompter.NewMockPrompter(t)
tt.opts.Prompter = pm
if tt.promptStubs != nil {
tt.promptStubs(pm)
}
cs, restoreRun := run.Stub()