From 88cf6ce16e2f237c3e98f43c729398dab8647edb Mon Sep 17 00:00:00 2001 From: vilmibm Date: Mon, 23 Mar 2020 16:36:24 -0500 Subject: [PATCH] consolidate CmdStubber into test package --- command/pr_create_test.go | 23 ++++++++++++----------- command/testing.go | 36 ------------------------------------ test/helpers.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 47 deletions(-) diff --git a/command/pr_create_test.go b/command/pr_create_test.go index 688dfd9c8..3b8769ad2 100644 --- a/command/pr_create_test.go +++ b/command/pr_create_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/cli/cli/context" + "github.com/cli/cli/test" ) func TestPRCreate(t *testing.T) { @@ -24,7 +25,7 @@ func TestPRCreate(t *testing.T) { } } } } `)) - cs, cmdTeardown := initCmdStubber() + cs, cmdTeardown := test.InitCmdStubber() defer cmdTeardown() cs.Stub("") // git status @@ -68,7 +69,7 @@ func TestPRCreate_alreadyExists(t *testing.T) { ] } } } } `)) - cs, cmdTeardown := initCmdStubber() + cs, cmdTeardown := test.InitCmdStubber() defer cmdTeardown() cs.Stub("") // git status @@ -88,7 +89,7 @@ func TestPRCreate_web(t *testing.T) { http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") - cs, cmdTeardown := initCmdStubber() + cs, cmdTeardown := test.InitCmdStubber() defer cmdTeardown() cs.Stub("") // git status @@ -123,7 +124,7 @@ func TestPRCreate_ReportsUncommittedChanges(t *testing.T) { } } } } `)) - cs, cmdTeardown := initCmdStubber() + cs, cmdTeardown := test.InitCmdStubber() defer cmdTeardown() cs.Stub(" M git/git.go") // git status @@ -193,7 +194,7 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) { } } } } `)) - cs, cmdTeardown := initCmdStubber() + cs, cmdTeardown := test.InitCmdStubber() defer cmdTeardown() cs.Stub("") // git status @@ -242,7 +243,7 @@ func TestPRCreate_survey_defaults_multicommit(t *testing.T) { } } } } `)) - cs, cmdTeardown := initCmdStubber() + cs, cmdTeardown := test.InitCmdStubber() defer cmdTeardown() cs.Stub("") // git status @@ -312,7 +313,7 @@ func TestPRCreate_survey_defaults_monocommit(t *testing.T) { } } } } `)) - cs, cmdTeardown := initCmdStubber() + cs, cmdTeardown := test.InitCmdStubber() defer cmdTeardown() cs.Stub("") // git status @@ -383,7 +384,7 @@ func TestPRCreate_survey_autofill(t *testing.T) { } } } } `)) - cs, cmdTeardown := initCmdStubber() + cs, cmdTeardown := test.InitCmdStubber() defer cmdTeardown() cs.Stub("") // git status @@ -426,7 +427,7 @@ func TestPRCreate_defaults_error_autofill(t *testing.T) { http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") - cs, cmdTeardown := initCmdStubber() + cs, cmdTeardown := test.InitCmdStubber() defer cmdTeardown() cs.Stub("") // git status @@ -442,7 +443,7 @@ func TestPRCreate_defaults_error_web(t *testing.T) { http := initFakeHTTP() http.StubRepoResponse("OWNER", "REPO") - cs, cmdTeardown := initCmdStubber() + cs, cmdTeardown := test.InitCmdStubber() defer cmdTeardown() cs.Stub("") // git status @@ -463,7 +464,7 @@ func TestPRCreate_defaults_error_interactive(t *testing.T) { } } } } `)) - cs, cmdTeardown := initCmdStubber() + cs, cmdTeardown := test.InitCmdStubber() defer cmdTeardown() cs.Stub("") // git status diff --git a/command/testing.go b/command/testing.go index 6d1a2594e..8ab97fc81 100644 --- a/command/testing.go +++ b/command/testing.go @@ -3,7 +3,6 @@ package command import ( "errors" "fmt" - "os/exec" "reflect" "github.com/AlecAivazis/survey/v2" @@ -11,43 +10,8 @@ import ( "github.com/cli/cli/api" "github.com/cli/cli/context" - "github.com/cli/cli/internal/run" - "github.com/cli/cli/test" ) -// TODO this is split between here and test/helpers.go. I did that because otherwise our test -// package would have to import utils (for utils.Runnable) which felt wrong. while utils_test -// currently doesn't import test helpers, i don't see why that ought to be precluded. -// I'm wondering if this is a case for having a global package just for storing Interfaces. -type CmdStubber struct { - Stubs []*test.OutputStub - Count int - Calls []*exec.Cmd -} - -func initCmdStubber() (*CmdStubber, func()) { - cs := CmdStubber{} - teardown := run.SetPrepareCmd(createStubbedPrepareCmd(&cs)) - return &cs, teardown -} - -func (cs *CmdStubber) Stub(desiredOutput string) { - // TODO maybe have some kind of command mapping but going simple for now - cs.Stubs = append(cs.Stubs, &test.OutputStub{[]byte(desiredOutput)}) -} - -func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) run.Runnable { - return func(cmd *exec.Cmd) run.Runnable { - cs.Calls = append(cs.Calls, cmd) - call := cs.Count - cs.Count += 1 - if call >= len(cs.Stubs) { - panic(fmt.Sprintf("more execs than stubs. most recent call: %v", cmd)) - } - return cs.Stubs[call] - } -} - type askStubber struct { Asks [][]*survey.Question Count int diff --git a/test/helpers.go b/test/helpers.go index add7b90c3..d9c3c8f68 100644 --- a/test/helpers.go +++ b/test/helpers.go @@ -1,5 +1,12 @@ package test +import ( + "fmt" + "os/exec" + + "github.com/cli/cli/internal/run" +) + // OutputStub implements a simple utils.Runnable type OutputStub struct { Out []byte @@ -12,3 +19,32 @@ func (s OutputStub) Output() ([]byte, error) { func (s OutputStub) Run() error { return nil } + +type CmdStubber struct { + Stubs []*OutputStub + Count int + Calls []*exec.Cmd +} + +func InitCmdStubber() (*CmdStubber, func()) { + cs := CmdStubber{} + teardown := run.SetPrepareCmd(createStubbedPrepareCmd(&cs)) + return &cs, teardown +} + +func (cs *CmdStubber) Stub(desiredOutput string) { + // TODO maybe have some kind of command mapping but going simple for now + cs.Stubs = append(cs.Stubs, &OutputStub{[]byte(desiredOutput)}) +} + +func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) run.Runnable { + return func(cmd *exec.Cmd) run.Runnable { + cs.Calls = append(cs.Calls, cmd) + call := cs.Count + cs.Count += 1 + if call >= len(cs.Stubs) { + panic(fmt.Sprintf("more execs than stubs. most recent call: %v", cmd)) + } + return cs.Stubs[call] + } +}