From a23549001e54dc461f4eb7cb1097ef0224e77c5b Mon Sep 17 00:00:00 2001 From: vilmibm Date: Fri, 13 Mar 2020 14:01:23 -0500 Subject: [PATCH] cleanup --- command/pr_create_test.go | 95 ++---------------------------------- command/testing.go | 57 +++++++++++++++++++++- command/title_body_survey.go | 2 - git/git_test.go | 2 +- 4 files changed, 62 insertions(+), 94 deletions(-) diff --git a/command/pr_create_test.go b/command/pr_create_test.go index 0f6c1b612..12469063a 100644 --- a/command/pr_create_test.go +++ b/command/pr_create_test.go @@ -3,16 +3,11 @@ package command import ( "bytes" "encoding/json" - "fmt" "io/ioutil" - "reflect" "strings" "testing" "github.com/cli/cli/context" - - "github.com/AlecAivazis/survey/v2" - "github.com/AlecAivazis/survey/v2/core" ) func TestPRCreate(t *testing.T) { @@ -25,7 +20,7 @@ func TestPRCreate(t *testing.T) { } } } } `)) - cs, cmdTeardown := InitCmdStubber() + cs, cmdTeardown := initCmdStubber() defer cmdTeardown() cs.Stub("") // git status @@ -63,7 +58,7 @@ func TestPRCreate_web(t *testing.T) { http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") - cs, cmdTeardown := InitCmdStubber() + cs, cmdTeardown := initCmdStubber() defer cmdTeardown() cs.Stub("") // git status @@ -94,7 +89,7 @@ func TestPRCreate_ReportsUncommittedChanges(t *testing.T) { } } } } `)) - cs, cmdTeardown := InitCmdStubber() + cs, cmdTeardown := initCmdStubber() defer cmdTeardown() cs.Stub(" M git/git.go") // git status @@ -160,7 +155,7 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) { } } } } `)) - cs, cmdTeardown := InitCmdStubber() + cs, cmdTeardown := initCmdStubber() defer cmdTeardown() cs.Stub("") // git status @@ -195,81 +190,6 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) { // goal: only care that gql is formatted properly } -/* - We aren't testing the survey code paths /at all/. - - so if we want to test those code paths, some cases: - - - user supplies no -t/-b and wants to preview in browser - - user supplies no -t/-b and wants to submit directly - - user supplies no -t/-b and wants to edit the title - - user supplies no -t/-b and wants to edit the body - - for defaults: - - - one commit - - multiple commits - - checking that defaults are generated appropriately each time. - - it seems that each survey prompt needs to be an injectable hook. -*/ - -type askStubber struct { - Asks [][]*survey.Question - Count int - Stubs [][]*QuestionStub -} - -func initAskStubber() (*askStubber, func()) { - origSurveyAsk := SurveyAsk - as := askStubber{} - SurveyAsk = func(qs []*survey.Question, response interface{}, opts ...survey.AskOpt) error { - as.Asks = append(as.Asks, qs) - count := as.Count - as.Count += 1 - if count >= len(as.Stubs) { - panic(fmt.Sprintf("more asks than stubs. most recent call: %v", qs)) - } - - // actually set response - stubbedQuestions := as.Stubs[count] - for i, sq := range stubbedQuestions { - q := qs[i] - if q.Name != sq.Name { - panic(fmt.Sprintf("stubbed question mismatch: %s != %s", q.Name, sq.Name)) - } - if sq.Default { - defaultValue := reflect.ValueOf(q.Prompt).Elem().FieldByName("Default") - core.WriteAnswer(response, q.Name, defaultValue) - } else { - core.WriteAnswer(response, q.Name, sq.Value) - } - } - - return nil - } - teardown := func() { - SurveyAsk = origSurveyAsk - } - return &as, teardown -} - -type QuestionStub struct { - Name string - Value interface{} - Default bool -} - -func (as *askStubber) Stub(stubbedQuestions []*QuestionStub) { - // A call to .Ask takes a list of questions; a stub is then a list of questions in the same order. - as.Stubs = append(as.Stubs, stubbedQuestions) -} - -func (as *askStubber) StubWithDefaults() { - as.Stubs = append(as.Stubs, nil) -} - func TestPRCreate_survey_defaults_multicommit(t *testing.T) { initBlankContext("OWNER/REPO", "feature") http := initFakeHTTP() @@ -280,7 +200,7 @@ func TestPRCreate_survey_defaults_multicommit(t *testing.T) { } } } } `)) - cs, cmdTeardown := InitCmdStubber() + cs, cmdTeardown := initCmdStubber() defer cmdTeardown() cs.Stub("") // git status @@ -291,11 +211,6 @@ func TestPRCreate_survey_defaults_multicommit(t *testing.T) { as, surveyTeardown := initAskStubber() defer surveyTeardown() - // so here is a problem: we lose survey's default detection. This works for specifying what a user - // has typed in, but not for simulating when a user inputs nothing. - // so; how to simulate when a user inputs nothing? can have a special method for that--even just - // "all defaults" would be ok -- but need to figure out if we can even /access/ what the default - // values would be. as.Stub([]*QuestionStub{ &QuestionStub{ Name: "title", diff --git a/command/testing.go b/command/testing.go index 83f96cc2a..0221ce8cf 100644 --- a/command/testing.go +++ b/command/testing.go @@ -4,6 +4,10 @@ import ( "errors" "fmt" "os/exec" + "reflect" + + "github.com/AlecAivazis/survey/v2" + "github.com/AlecAivazis/survey/v2/core" "github.com/cli/cli/api" "github.com/cli/cli/context" @@ -21,7 +25,7 @@ type CmdStubber struct { Calls []*exec.Cmd } -func InitCmdStubber() (*CmdStubber, func()) { +func initCmdStubber() (*CmdStubber, func()) { cs := CmdStubber{} teardown := utils.SetPrepareCmd(createStubbedPrepareCmd(&cs)) return &cs, teardown @@ -44,6 +48,57 @@ func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) utils.Runnable { } } +type askStubber struct { + Asks [][]*survey.Question + Count int + Stubs [][]*QuestionStub +} + +func initAskStubber() (*askStubber, func()) { + origSurveyAsk := SurveyAsk + as := askStubber{} + SurveyAsk = func(qs []*survey.Question, response interface{}, opts ...survey.AskOpt) error { + as.Asks = append(as.Asks, qs) + count := as.Count + as.Count += 1 + if count >= len(as.Stubs) { + panic(fmt.Sprintf("more asks than stubs. most recent call: %v", qs)) + } + + // actually set response + stubbedQuestions := as.Stubs[count] + for i, sq := range stubbedQuestions { + q := qs[i] + if q.Name != sq.Name { + panic(fmt.Sprintf("stubbed question mismatch: %s != %s", q.Name, sq.Name)) + } + if sq.Default { + defaultValue := reflect.ValueOf(q.Prompt).Elem().FieldByName("Default") + core.WriteAnswer(response, q.Name, defaultValue) + } else { + core.WriteAnswer(response, q.Name, sq.Value) + } + } + + return nil + } + teardown := func() { + SurveyAsk = origSurveyAsk + } + return &as, teardown +} + +type QuestionStub struct { + Name string + Value interface{} + Default bool +} + +func (as *askStubber) Stub(stubbedQuestions []*QuestionStub) { + // A call to .Ask takes a list of questions; a stub is then a list of questions in the same order. + as.Stubs = append(as.Stubs, stubbedQuestions) +} + func initBlankContext(repo, branch string) { initContext = func() context.Context { ctx := context.NewBlank() diff --git a/command/title_body_survey.go b/command/title_body_survey.go index ef534a398..465887c69 100644 --- a/command/title_body_survey.go +++ b/command/title_body_survey.go @@ -50,8 +50,6 @@ var ConfirmSubmission = func() (Action, error) { return -1, fmt.Errorf("could not prompt: %w", err) } - fmt.Printf("GOTTA NUMBER %d\n", confirmAnswers.Confirmation) - return Action(confirmAnswers.Confirmation), nil } diff --git a/git/git_test.go b/git/git_test.go index 9f226b62e..9e3f9a333 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -15,7 +15,7 @@ func Test_UncommittedChangeCount(t *testing.T) { Output string } cases := []c{ - c{Label: "no changes", Expected: 0, Output: ""}, // TODO will this fail and be seen as one newline? + c{Label: "no changes", Expected: 0, Output: ""}, c{Label: "one change", Expected: 1, Output: " M poem.txt"}, c{Label: "untracked file", Expected: 2, Output: " M poem.txt\n?? new.txt"}, }