@@ -3,16 +3,11 @@ package command
33import (
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
1813func 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-
273193func 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" ,
0 commit comments