X Tutup
Skip to content

Commit a235490

Browse files
committed
cleanup
1 parent 1b78403 commit a235490

File tree

4 files changed

+62
-94
lines changed

4 files changed

+62
-94
lines changed

command/pr_create_test.go

Lines changed: 5 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@ package command
33
import (
44
"bytes"
55
"encoding/json"
6-
"fmt"
76
"io/ioutil"
8-
"reflect"
97
"strings"
108
"testing"
119

1210
"github.com/cli/cli/context"
13-
14-
"github.com/AlecAivazis/survey/v2"
15-
"github.com/AlecAivazis/survey/v2/core"
1611
)
1712

1813
func TestPRCreate(t *testing.T) {
@@ -25,7 +20,7 @@ func TestPRCreate(t *testing.T) {
2520
} } } }
2621
`))
2722

28-
cs, cmdTeardown := InitCmdStubber()
23+
cs, cmdTeardown := initCmdStubber()
2924
defer cmdTeardown()
3025

3126
cs.Stub("") // git status
@@ -63,7 +58,7 @@ func TestPRCreate_web(t *testing.T) {
6358
http := initFakeHTTP()
6459
http.StubRepoResponse("OWNER", "REPO")
6560

66-
cs, cmdTeardown := InitCmdStubber()
61+
cs, cmdTeardown := initCmdStubber()
6762
defer cmdTeardown()
6863

6964
cs.Stub("") // git status
@@ -94,7 +89,7 @@ func TestPRCreate_ReportsUncommittedChanges(t *testing.T) {
9489
} } } }
9590
`))
9691

97-
cs, cmdTeardown := InitCmdStubber()
92+
cs, cmdTeardown := initCmdStubber()
9893
defer cmdTeardown()
9994

10095
cs.Stub(" M git/git.go") // git status
@@ -160,7 +155,7 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) {
160155
} } } }
161156
`))
162157

163-
cs, cmdTeardown := InitCmdStubber()
158+
cs, cmdTeardown := initCmdStubber()
164159
defer cmdTeardown()
165160

166161
cs.Stub("") // git status
@@ -195,81 +190,6 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) {
195190
// goal: only care that gql is formatted properly
196191
}
197192

198-
/*
199-
We aren't testing the survey code paths /at all/.
200-
201-
so if we want to test those code paths, some cases:
202-
203-
- user supplies no -t/-b and wants to preview in browser
204-
- user supplies no -t/-b and wants to submit directly
205-
- user supplies no -t/-b and wants to edit the title
206-
- user supplies no -t/-b and wants to edit the body
207-
208-
for defaults:
209-
210-
- one commit
211-
- multiple commits
212-
213-
checking that defaults are generated appropriately each time.
214-
215-
it seems that each survey prompt needs to be an injectable hook.
216-
*/
217-
218-
type askStubber struct {
219-
Asks [][]*survey.Question
220-
Count int
221-
Stubs [][]*QuestionStub
222-
}
223-
224-
func initAskStubber() (*askStubber, func()) {
225-
origSurveyAsk := SurveyAsk
226-
as := askStubber{}
227-
SurveyAsk = func(qs []*survey.Question, response interface{}, opts ...survey.AskOpt) error {
228-
as.Asks = append(as.Asks, qs)
229-
count := as.Count
230-
as.Count += 1
231-
if count >= len(as.Stubs) {
232-
panic(fmt.Sprintf("more asks than stubs. most recent call: %v", qs))
233-
}
234-
235-
// actually set response
236-
stubbedQuestions := as.Stubs[count]
237-
for i, sq := range stubbedQuestions {
238-
q := qs[i]
239-
if q.Name != sq.Name {
240-
panic(fmt.Sprintf("stubbed question mismatch: %s != %s", q.Name, sq.Name))
241-
}
242-
if sq.Default {
243-
defaultValue := reflect.ValueOf(q.Prompt).Elem().FieldByName("Default")
244-
core.WriteAnswer(response, q.Name, defaultValue)
245-
} else {
246-
core.WriteAnswer(response, q.Name, sq.Value)
247-
}
248-
}
249-
250-
return nil
251-
}
252-
teardown := func() {
253-
SurveyAsk = origSurveyAsk
254-
}
255-
return &as, teardown
256-
}
257-
258-
type QuestionStub struct {
259-
Name string
260-
Value interface{}
261-
Default bool
262-
}
263-
264-
func (as *askStubber) Stub(stubbedQuestions []*QuestionStub) {
265-
// A call to .Ask takes a list of questions; a stub is then a list of questions in the same order.
266-
as.Stubs = append(as.Stubs, stubbedQuestions)
267-
}
268-
269-
func (as *askStubber) StubWithDefaults() {
270-
as.Stubs = append(as.Stubs, nil)
271-
}
272-
273193
func TestPRCreate_survey_defaults_multicommit(t *testing.T) {
274194
initBlankContext("OWNER/REPO", "feature")
275195
http := initFakeHTTP()
@@ -280,7 +200,7 @@ func TestPRCreate_survey_defaults_multicommit(t *testing.T) {
280200
} } } }
281201
`))
282202

283-
cs, cmdTeardown := InitCmdStubber()
203+
cs, cmdTeardown := initCmdStubber()
284204
defer cmdTeardown()
285205

286206
cs.Stub("") // git status
@@ -291,11 +211,6 @@ func TestPRCreate_survey_defaults_multicommit(t *testing.T) {
291211
as, surveyTeardown := initAskStubber()
292212
defer surveyTeardown()
293213

294-
// so here is a problem: we lose survey's default detection. This works for specifying what a user
295-
// has typed in, but not for simulating when a user inputs nothing.
296-
// so; how to simulate when a user inputs nothing? can have a special method for that--even just
297-
// "all defaults" would be ok -- but need to figure out if we can even /access/ what the default
298-
// values would be.
299214
as.Stub([]*QuestionStub{
300215
&QuestionStub{
301216
Name: "title",

command/testing.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import (
44
"errors"
55
"fmt"
66
"os/exec"
7+
"reflect"
8+
9+
"github.com/AlecAivazis/survey/v2"
10+
"github.com/AlecAivazis/survey/v2/core"
711

812
"github.com/cli/cli/api"
913
"github.com/cli/cli/context"
@@ -21,7 +25,7 @@ type CmdStubber struct {
2125
Calls []*exec.Cmd
2226
}
2327

24-
func InitCmdStubber() (*CmdStubber, func()) {
28+
func initCmdStubber() (*CmdStubber, func()) {
2529
cs := CmdStubber{}
2630
teardown := utils.SetPrepareCmd(createStubbedPrepareCmd(&cs))
2731
return &cs, teardown
@@ -44,6 +48,57 @@ func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) utils.Runnable {
4448
}
4549
}
4650

51+
type askStubber struct {
52+
Asks [][]*survey.Question
53+
Count int
54+
Stubs [][]*QuestionStub
55+
}
56+
57+
func initAskStubber() (*askStubber, func()) {
58+
origSurveyAsk := SurveyAsk
59+
as := askStubber{}
60+
SurveyAsk = func(qs []*survey.Question, response interface{}, opts ...survey.AskOpt) error {
61+
as.Asks = append(as.Asks, qs)
62+
count := as.Count
63+
as.Count += 1
64+
if count >= len(as.Stubs) {
65+
panic(fmt.Sprintf("more asks than stubs. most recent call: %v", qs))
66+
}
67+
68+
// actually set response
69+
stubbedQuestions := as.Stubs[count]
70+
for i, sq := range stubbedQuestions {
71+
q := qs[i]
72+
if q.Name != sq.Name {
73+
panic(fmt.Sprintf("stubbed question mismatch: %s != %s", q.Name, sq.Name))
74+
}
75+
if sq.Default {
76+
defaultValue := reflect.ValueOf(q.Prompt).Elem().FieldByName("Default")
77+
core.WriteAnswer(response, q.Name, defaultValue)
78+
} else {
79+
core.WriteAnswer(response, q.Name, sq.Value)
80+
}
81+
}
82+
83+
return nil
84+
}
85+
teardown := func() {
86+
SurveyAsk = origSurveyAsk
87+
}
88+
return &as, teardown
89+
}
90+
91+
type QuestionStub struct {
92+
Name string
93+
Value interface{}
94+
Default bool
95+
}
96+
97+
func (as *askStubber) Stub(stubbedQuestions []*QuestionStub) {
98+
// A call to .Ask takes a list of questions; a stub is then a list of questions in the same order.
99+
as.Stubs = append(as.Stubs, stubbedQuestions)
100+
}
101+
47102
func initBlankContext(repo, branch string) {
48103
initContext = func() context.Context {
49104
ctx := context.NewBlank()

command/title_body_survey.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ var ConfirmSubmission = func() (Action, error) {
5050
return -1, fmt.Errorf("could not prompt: %w", err)
5151
}
5252

53-
fmt.Printf("GOTTA NUMBER %d\n", confirmAnswers.Confirmation)
54-
5553
return Action(confirmAnswers.Confirmation), nil
5654
}
5755

git/git_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func Test_UncommittedChangeCount(t *testing.T) {
1515
Output string
1616
}
1717
cases := []c{
18-
c{Label: "no changes", Expected: 0, Output: ""}, // TODO will this fail and be seen as one newline?
18+
c{Label: "no changes", Expected: 0, Output: ""},
1919
c{Label: "one change", Expected: 1, Output: " M poem.txt"},
2020
c{Label: "untracked file", Expected: 2, Output: " M poem.txt\n?? new.txt"},
2121
}

0 commit comments

Comments
 (0)
X Tutup