WIP porting repo edit to opts.Prompter

This commit is contained in:
Nate Smith 2023-08-07 17:59:56 -07:00
parent 7d470c4df4
commit aed3a67749
2 changed files with 67 additions and 30 deletions

View file

@ -279,7 +279,7 @@ func editRun(ctx context.Context, opts *EditOptions) error {
return nil
}
func interactiveChoice(r *api.Repository) ([]string, error) {
func interactiveChoice(opts EditOptions, r *api.Repository) ([]string, error) {
options := []string{
optionDefaultBranchName,
optionDescription,
@ -298,11 +298,14 @@ func interactiveChoice(r *api.Repository) ([]string, error) {
options = append(options, optionAllowForking)
}
var answers []string
//nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter
err := prompt.SurveyAskOne(&survey.MultiSelect{
Message: "What do you want to edit?",
Options: options,
}, &answers, survey.WithPageSize(11))
selected, err := opts.Prompter.MultiSelect("What do you want to edit?", nil, options)
if err != nil {
return nil, err
}
for _, i := range selected {
answers = append(answers, options[i])
}
return answers, err
}
@ -310,7 +313,7 @@ func interactiveRepoEdit(opts *EditOptions, r *api.Repository) error {
for _, v := range r.RepositoryTopics.Nodes {
opts.topicsCache = append(opts.topicsCache, v.Topic.Name)
}
choices, err := interactiveChoice(r)
choices, err := interactiveChoice(*opts, r)
if err != nil {
return err
}
@ -318,14 +321,11 @@ func interactiveRepoEdit(opts *EditOptions, r *api.Repository) error {
switch c {
case optionDescription:
opts.Edits.Description = &r.Description
//nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter
err = prompt.SurveyAskOne(&survey.Input{
Message: "Description of the repository",
Default: r.Description,
}, opts.Edits.Description)
answer, err := opts.Prompter.Input("Description of the repository", r.Description)
if err != nil {
return err
}
opts.Edits.Description = &answer
case optionHomePageURL:
opts.Edits.Homepage = &r.HomepageURL
//nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter
@ -337,11 +337,7 @@ func interactiveRepoEdit(opts *EditOptions, r *api.Repository) error {
return err
}
case optionTopics:
var addTopics string
//nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter
err = prompt.SurveyAskOne(&survey.Input{
Message: "Add topics?(csv format)",
}, &addTopics)
addTopics, err := opts.Prompter.Input("Add topics?(csv format)", "")
if err != nil {
return err
}
@ -350,14 +346,13 @@ func interactiveRepoEdit(opts *EditOptions, r *api.Repository) error {
}
if len(opts.topicsCache) > 0 {
//nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter
err = prompt.SurveyAskOne(&survey.MultiSelect{
Message: "Remove Topics",
Options: opts.topicsCache,
}, &opts.RemoveTopics)
selected, err := opts.Prompter.MultiSelect("Remove Topics", nil, opts.topicsCache)
if err != nil {
return err
}
for _, i := range selected {
opts.RemoveTopics = append(opts.RemoveTopics, opts.topicsCache[i])
}
}
case optionDefaultBranchName:
opts.Edits.DefaultBranch = &r.DefaultBranchRef.Name

View file

@ -10,6 +10,7 @@ import (
"github.com/cli/cli/v2/pkg/prompt"
"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"
@ -174,11 +175,26 @@ func Test_editRun(t *testing.T) {
}
}
// TODO consider unit test for interactiveRepoEdit that exercises every prompt type
func Test_editRun_interactive(t *testing.T) {
editList := []string{
"Default Branch Name",
"Description",
"Home Page URL",
"Issues",
"Merge Options",
"Projects",
"Template Repository",
"Topics",
"Visibility",
"Wikis"}
tests := []struct {
name string
opts EditOptions
askStubs func(*prompt.AskStubber)
promptStubs func(*prompter.MockPrompter)
httpStubs func(*testing.T, *httpmock.Registry)
wantsStderr string
wantsErr string
@ -189,9 +205,16 @@ func Test_editRun_interactive(t *testing.T) {
Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"),
InteractiveMode: true,
},
askStubs: func(as *prompt.AskStubber) {
as.StubPrompt("What do you want to edit?").AnswerWith([]string{"Description"})
as.StubPrompt("Description of the repository").AnswerWith("awesome repo description")
askStubs: func(as *prompt.AskStubber) {},
promptStubs: func(pm *prompter.MockPrompter) {
pm.RegisterMultiSelect("What do you want to edit?", nil, editList,
func(_ string, _, opts []string) ([]int, error) {
return []int{1}, nil
})
pm.RegisterInput("Description of the repository",
func(_, _ string) (string, error) {
return "awesome repo description", nil
})
},
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
@ -229,11 +252,24 @@ func Test_editRun_interactive(t *testing.T) {
Repository: ghrepo.NewWithHost("OWNER", "REPO", "github.com"),
InteractiveMode: true,
},
askStubs: func(as *prompt.AskStubber) {
as.StubPrompt("What do you want to edit?").AnswerWith([]string{"Description", "Topics"})
as.StubPrompt("Description of the repository").AnswerWith("awesome repo description")
as.StubPrompt("Add topics?(csv format)").AnswerWith("a, b,c,d ")
as.StubPrompt("Remove Topics").AnswerWith([]string{"x"})
askStubs: func(as *prompt.AskStubber) {},
promptStubs: func(pm *prompter.MockPrompter) {
pm.RegisterMultiSelect("What do you want to edit?", nil, editList,
func(_ string, _, opts []string) ([]int, error) {
return []int{1, 7}, nil
})
pm.RegisterInput("Description of the repository",
func(_, _ string) (string, error) {
return "awesome repo description", nil
})
pm.RegisterInput("Add topics?(csv format)",
func(_, _ string) (string, error) {
return "a, b,c,d ", nil
})
pm.RegisterMultiSelect("Remove Topics", nil, []string{"x"},
func(_ string, _, opts []string) ([]int, error) {
return []int{0}, nil
})
},
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(
@ -333,6 +369,12 @@ func Test_editRun_interactive(t *testing.T) {
tt.httpStubs(t, httpReg)
}
pm := prompter.NewMockPrompter(t)
tt.opts.Prompter = pm
if tt.promptStubs != nil {
tt.promptStubs(pm)
}
opts := &tt.opts
opts.HTTPClient = &http.Client{Transport: httpReg}
opts.IO = ios