Skip to content

Commit 43318fd

Browse files
author
Nate Smith
authored
Merge pull request cli#1436 from cli/move-ask-stubber
move survey helper and test helpers to prompt pkg
2 parents f486cc4 + 245c3c7 commit 43318fd

File tree

9 files changed

+102
-90
lines changed

9 files changed

+102
-90
lines changed

command/pr.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/cli/cli/git"
1717
"github.com/cli/cli/internal/ghrepo"
1818
"github.com/cli/cli/pkg/cmdutil"
19+
"github.com/cli/cli/pkg/prompt"
1920
"github.com/cli/cli/pkg/text"
2021
"github.com/cli/cli/utils"
2122
"github.com/spf13/cobra"
@@ -619,7 +620,7 @@ func prInteractiveMerge(deleteLocalBranch bool, crossRepoPR bool) (api.PullReque
619620
DeleteBranch bool
620621
}{}
621622

622-
err := SurveyAsk(qs, &answers)
623+
err := prompt.SurveyAsk(qs, &answers)
623624
if err != nil {
624625
return 0, false, fmt.Errorf("could not prompt: %w", err)
625626
}

command/pr_create_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/cli/cli/git"
1212
"github.com/cli/cli/internal/ghrepo"
1313
"github.com/cli/cli/pkg/httpmock"
14+
"github.com/cli/cli/pkg/prompt"
1415
"github.com/cli/cli/test"
1516
"github.com/stretchr/testify/assert"
1617
)
@@ -602,10 +603,10 @@ func TestPRCreate_survey_defaults_multicommit(t *testing.T) {
602603
cs.Stub("") // git rev-parse
603604
cs.Stub("") // git push
604605

605-
as, surveyTeardown := initAskStubber()
606+
as, surveyTeardown := prompt.InitAskStubber()
606607
defer surveyTeardown()
607608

608-
as.Stub([]*QuestionStub{
609+
as.Stub([]*prompt.QuestionStub{
609610
{
610611
Name: "title",
611612
Default: true,
@@ -615,7 +616,7 @@ func TestPRCreate_survey_defaults_multicommit(t *testing.T) {
615616
Default: true,
616617
},
617618
})
618-
as.Stub([]*QuestionStub{
619+
as.Stub([]*prompt.QuestionStub{
619620
{
620621
Name: "confirmation",
621622
Value: 0,
@@ -687,10 +688,10 @@ func TestPRCreate_survey_defaults_monocommit(t *testing.T) {
687688
cs.Stub("") // git rev-parse
688689
cs.Stub("") // git push
689690

690-
as, surveyTeardown := initAskStubber()
691+
as, surveyTeardown := prompt.InitAskStubber()
691692
defer surveyTeardown()
692693

693-
as.Stub([]*QuestionStub{
694+
as.Stub([]*prompt.QuestionStub{
694695
{
695696
Name: "title",
696697
Default: true,
@@ -700,7 +701,7 @@ func TestPRCreate_survey_defaults_monocommit(t *testing.T) {
700701
Default: true,
701702
},
702703
})
703-
as.Stub([]*QuestionStub{
704+
as.Stub([]*prompt.QuestionStub{
704705
{
705706
Name: "confirmation",
706707
Value: 0,
@@ -896,10 +897,10 @@ func TestPRCreate_defaults_error_interactive(t *testing.T) {
896897
cs.Stub("") // git push
897898
cs.Stub("") // browser open
898899

899-
as, surveyTeardown := initAskStubber()
900+
as, surveyTeardown := prompt.InitAskStubber()
900901
defer surveyTeardown()
901902

902-
as.Stub([]*QuestionStub{
903+
as.Stub([]*prompt.QuestionStub{
903904
{
904905
Name: "title",
905906
Default: true,
@@ -909,7 +910,7 @@ func TestPRCreate_defaults_error_interactive(t *testing.T) {
909910
Value: "social distancing",
910911
},
911912
})
912-
as.Stub([]*QuestionStub{
913+
as.Stub([]*prompt.QuestionStub{
913914
{
914915
Name: "confirmation",
915916
Value: 1,

command/pr_review.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/spf13/cobra"
1010

1111
"github.com/cli/cli/api"
12+
"github.com/cli/cli/pkg/prompt"
1213
"github.com/cli/cli/pkg/surveyext"
1314
"github.com/cli/cli/utils"
1415
)
@@ -166,7 +167,7 @@ func reviewSurvey(cmd *cobra.Command) (*api.PullRequestReviewInput, error) {
166167
},
167168
}
168169

169-
err = SurveyAsk(typeQs, &typeAnswers)
170+
err = prompt.SurveyAsk(typeQs, &typeAnswers)
170171
if err != nil {
171172
return nil, err
172173
}
@@ -207,7 +208,7 @@ func reviewSurvey(cmd *cobra.Command) (*api.PullRequestReviewInput, error) {
207208
},
208209
}
209210

210-
err = SurveyAsk(bodyQs, &bodyAnswers)
211+
err = prompt.SurveyAsk(bodyQs, &bodyAnswers)
211212
if err != nil {
212213
return nil, err
213214
}
@@ -237,7 +238,7 @@ func reviewSurvey(cmd *cobra.Command) (*api.PullRequestReviewInput, error) {
237238
},
238239
}
239240

240-
err = SurveyAsk(confirmQs, &confirm)
241+
err = prompt.SurveyAsk(confirmQs, &confirm)
241242
if err != nil {
242243
return nil, err
243244
}

command/pr_review_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"regexp"
88
"testing"
99

10+
"github.com/cli/cli/pkg/prompt"
1011
"github.com/cli/cli/test"
1112
"github.com/stretchr/testify/assert"
1213
)
@@ -337,22 +338,22 @@ func TestPRReview_interactive(t *testing.T) {
337338
] } } } }
338339
`))
339340
http.StubResponse(200, bytes.NewBufferString(`{"data": {} }`))
340-
as, teardown := initAskStubber()
341+
as, teardown := prompt.InitAskStubber()
341342
defer teardown()
342343

343-
as.Stub([]*QuestionStub{
344+
as.Stub([]*prompt.QuestionStub{
344345
{
345346
Name: "reviewType",
346347
Value: "Approve",
347348
},
348349
})
349-
as.Stub([]*QuestionStub{
350+
as.Stub([]*prompt.QuestionStub{
350351
{
351352
Name: "body",
352353
Value: "cool story",
353354
},
354355
})
355-
as.Stub([]*QuestionStub{
356+
as.Stub([]*prompt.QuestionStub{
356357
{
357358
Name: "confirm",
358359
Value: true,
@@ -399,22 +400,22 @@ func TestPRReview_interactive_no_body(t *testing.T) {
399400
] } } } }
400401
`))
401402
http.StubResponse(200, bytes.NewBufferString(`{"data": {} }`))
402-
as, teardown := initAskStubber()
403+
as, teardown := prompt.InitAskStubber()
403404
defer teardown()
404405

405-
as.Stub([]*QuestionStub{
406+
as.Stub([]*prompt.QuestionStub{
406407
{
407408
Name: "reviewType",
408409
Value: "Request changes",
409410
},
410411
})
411-
as.Stub([]*QuestionStub{
412+
as.Stub([]*prompt.QuestionStub{
412413
{
413414
Name: "body",
414415
Default: true,
415416
},
416417
})
417-
as.Stub([]*QuestionStub{
418+
as.Stub([]*prompt.QuestionStub{
418419
{
419420
Name: "confirm",
420421
Value: true,
@@ -443,22 +444,22 @@ func TestPRReview_interactive_blank_approve(t *testing.T) {
443444
] } } } }
444445
`))
445446
http.StubResponse(200, bytes.NewBufferString(`{"data": {} }`))
446-
as, teardown := initAskStubber()
447+
as, teardown := prompt.InitAskStubber()
447448
defer teardown()
448449

449-
as.Stub([]*QuestionStub{
450+
as.Stub([]*prompt.QuestionStub{
450451
{
451452
Name: "reviewType",
452453
Value: "Approve",
453454
},
454455
})
455-
as.Stub([]*QuestionStub{
456+
as.Stub([]*prompt.QuestionStub{
456457
{
457458
Name: "body",
458459
Default: true,
459460
},
460461
})
461-
as.Stub([]*QuestionStub{
462+
as.Stub([]*prompt.QuestionStub{
462463
{
463464
Name: "confirm",
464465
Value: true,

command/pr_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/cli/cli/api"
1212
"github.com/cli/cli/internal/run"
1313
"github.com/cli/cli/pkg/httpmock"
14+
"github.com/cli/cli/pkg/prompt"
1415
"github.com/cli/cli/test"
1516
"github.com/google/go-cmp/cmp"
1617
"github.com/stretchr/testify/assert"
@@ -1618,10 +1619,10 @@ func TestPRMerge_interactive(t *testing.T) {
16181619
cs.Stub("") // git push origin --delete blueberries
16191620
cs.Stub("") // git branch -d
16201621

1621-
as, surveyTeardown := initAskStubber()
1622+
as, surveyTeardown := prompt.InitAskStubber()
16221623
defer surveyTeardown()
16231624

1624-
as.Stub([]*QuestionStub{
1625+
as.Stub([]*prompt.QuestionStub{
16251626
{
16261627
Name: "mergeMethod",
16271628
Value: 0,

command/testing.go

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"reflect"
87

9-
"github.com/AlecAivazis/survey/v2"
10-
"github.com/AlecAivazis/survey/v2/core"
118
"github.com/cli/cli/api"
129
"github.com/cli/cli/context"
1310
"github.com/cli/cli/internal/config"
@@ -23,57 +20,6 @@ const defaultTestConfig = `hosts:
2320
oauth_token: "1234567890"
2421
`
2522

26-
type askStubber struct {
27-
Asks [][]*survey.Question
28-
Count int
29-
Stubs [][]*QuestionStub
30-
}
31-
32-
func initAskStubber() (*askStubber, func()) {
33-
origSurveyAsk := SurveyAsk
34-
as := askStubber{}
35-
SurveyAsk = func(qs []*survey.Question, response interface{}, opts ...survey.AskOpt) error {
36-
as.Asks = append(as.Asks, qs)
37-
count := as.Count
38-
as.Count += 1
39-
if count >= len(as.Stubs) {
40-
panic(fmt.Sprintf("more asks than stubs. most recent call: %v", qs))
41-
}
42-
43-
// actually set response
44-
stubbedQuestions := as.Stubs[count]
45-
for i, sq := range stubbedQuestions {
46-
q := qs[i]
47-
if q.Name != sq.Name {
48-
panic(fmt.Sprintf("stubbed question mismatch: %s != %s", q.Name, sq.Name))
49-
}
50-
if sq.Default {
51-
defaultValue := reflect.ValueOf(q.Prompt).Elem().FieldByName("Default")
52-
_ = core.WriteAnswer(response, q.Name, defaultValue)
53-
} else {
54-
_ = core.WriteAnswer(response, q.Name, sq.Value)
55-
}
56-
}
57-
58-
return nil
59-
}
60-
teardown := func() {
61-
SurveyAsk = origSurveyAsk
62-
}
63-
return &as, teardown
64-
}
65-
66-
type QuestionStub struct {
67-
Name string
68-
Value interface{}
69-
Default bool
70-
}
71-
72-
func (as *askStubber) Stub(stubbedQuestions []*QuestionStub) {
73-
// A call to .Ask takes a list of questions; a stub is then a list of questions in the same order.
74-
as.Stubs = append(as.Stubs, stubbedQuestions)
75-
}
76-
7723
func initBlankContext(cfg, repo, branch string) {
7824
initContext = func() context.Context {
7925
ctx := context.NewBlank()

command/title_body_survey.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/cli/cli/api"
88
"github.com/cli/cli/internal/ghrepo"
99
"github.com/cli/cli/pkg/githubtemplate"
10+
"github.com/cli/cli/pkg/prompt"
1011
"github.com/cli/cli/pkg/surveyext"
1112
"github.com/cli/cli/utils"
1213
"github.com/spf13/cobra"
@@ -54,10 +55,6 @@ const (
5455
noMilestone = "(none)"
5556
)
5657

57-
var SurveyAsk = func(qs []*survey.Question, response interface{}, opts ...survey.AskOpt) error {
58-
return survey.Ask(qs, response, opts...)
59-
}
60-
6158
func confirmSubmission(allowPreview bool, allowMetadata bool) (Action, error) {
6259
const (
6360
submitLabel = "Submit"
@@ -88,7 +85,7 @@ func confirmSubmission(allowPreview bool, allowMetadata bool) (Action, error) {
8885
},
8986
}
9087

91-
err := SurveyAsk(confirmQs, &confirmAnswers)
88+
err := prompt.SurveyAsk(confirmQs, &confirmAnswers)
9289
if err != nil {
9390
return -1, fmt.Errorf("could not prompt: %w", err)
9491
}
@@ -130,7 +127,7 @@ func selectTemplate(nonLegacyTemplatePaths []string, legacyTemplatePath *string,
130127
},
131128
},
132129
}
133-
if err := SurveyAsk(selectQs, &templateResponse); err != nil {
130+
if err := prompt.SurveyAsk(selectQs, &templateResponse); err != nil {
134131
return "", fmt.Errorf("could not prompt: %w", err)
135132
}
136133

@@ -201,7 +198,7 @@ func titleBodySurvey(cmd *cobra.Command, issueState *issueMetadataState, apiClie
201198
qs = append(qs, bodyQuestion)
202199
}
203200

204-
err = SurveyAsk(qs, issueState)
201+
err = prompt.SurveyAsk(qs, issueState)
205202
if err != nil {
206203
return fmt.Errorf("could not prompt: %w", err)
207204
}
@@ -232,7 +229,7 @@ func titleBodySurvey(cmd *cobra.Command, issueState *issueMetadataState, apiClie
232229
}
233230
extraFieldsOptions = append(extraFieldsOptions, "Assignees", "Labels", "Projects", "Milestone")
234231

235-
err = SurveyAsk([]*survey.Question{
232+
err = prompt.SurveyAsk([]*survey.Question{
236233
{
237234
Name: "metadata",
238235
Prompt: &survey.MultiSelect{
@@ -364,7 +361,7 @@ func titleBodySurvey(cmd *cobra.Command, issueState *issueMetadataState, apiClie
364361
}
365362
}
366363
values := metadataValues{}
367-
err = SurveyAsk(mqs, &values, survey.WithKeepFilter(true))
364+
err = prompt.SurveyAsk(mqs, &values, survey.WithKeepFilter(true))
368365
if err != nil {
369366
return fmt.Errorf("could not prompt: %w", err)
370367
}

pkg/prompt/prompt.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ var Confirm = func(prompt string, result *bool) error {
2020
}
2121
return survey.AskOne(p, result)
2222
}
23+
24+
var SurveyAsk = func(qs []*survey.Question, response interface{}, opts ...survey.AskOpt) error {
25+
return survey.Ask(qs, response, opts...)
26+
}

0 commit comments

Comments
 (0)