From 210c47572c21bfd099d4d40c17a2bce5cf914ea8 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Thu, 30 Apr 2020 12:14:40 -0700 Subject: [PATCH] Move RunCommand to testing file --- command/pr_test.go | 49 --------------------------------------------- command/testing.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 49 deletions(-) diff --git a/command/pr_test.go b/command/pr_test.go index ba0989645..cb4805f7b 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -15,9 +15,6 @@ import ( "github.com/cli/cli/internal/run" "github.com/cli/cli/test" "github.com/google/go-cmp/cmp" - "github.com/google/shlex" - "github.com/spf13/cobra" - "github.com/spf13/pflag" ) func eq(t *testing.T, got interface{}, expected interface{}) { @@ -27,52 +24,6 @@ func eq(t *testing.T, got interface{}, expected interface{}) { } } -type cmdOut struct { - outBuf, errBuf *bytes.Buffer -} - -func (c cmdOut) String() string { - return c.outBuf.String() -} - -func (c cmdOut) Stderr() string { - return c.errBuf.String() -} - -func RunCommand(cmd *cobra.Command, args string) (*cmdOut, error) { - rootCmd := cmd.Root() - argv, err := shlex.Split(args) - if err != nil { - return nil, err - } - rootCmd.SetArgs(argv) - - outBuf := bytes.Buffer{} - cmd.SetOut(&outBuf) - errBuf := bytes.Buffer{} - cmd.SetErr(&errBuf) - - // Reset flag values so they don't leak between tests - // FIXME: change how we initialize Cobra commands to render this hack unnecessary - cmd.Flags().VisitAll(func(f *pflag.Flag) { - switch v := f.Value.(type) { - case pflag.SliceValue: - _ = v.Replace([]string{}) - default: - switch v.Type() { - case "bool", "string", "int": - _ = v.Set(f.DefValue) - } - } - }) - - _, err = rootCmd.ExecuteC() - cmd.SetOut(nil) - cmd.SetErr(nil) - - return &cmdOut{&outBuf, &errBuf}, err -} - func TestPRStatus(t *testing.T) { initBlankContext("", "OWNER/REPO", "blueberries") http := initFakeHTTP() diff --git a/command/testing.go b/command/testing.go index b9b0ba04a..e99e8cbfb 100644 --- a/command/testing.go +++ b/command/testing.go @@ -1,12 +1,16 @@ package command import ( + "bytes" "errors" "fmt" "reflect" "github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2/core" + "github.com/google/shlex" + "github.com/spf13/cobra" + "github.com/spf13/pflag" "github.com/cli/cli/api" "github.com/cli/cli/context" @@ -99,6 +103,52 @@ func initFakeHTTP() *api.FakeHTTP { return http } +type cmdOut struct { + outBuf, errBuf *bytes.Buffer +} + +func (c cmdOut) String() string { + return c.outBuf.String() +} + +func (c cmdOut) Stderr() string { + return c.errBuf.String() +} + +func RunCommand(cmd *cobra.Command, args string) (*cmdOut, error) { + rootCmd := cmd.Root() + argv, err := shlex.Split(args) + if err != nil { + return nil, err + } + rootCmd.SetArgs(argv) + + outBuf := bytes.Buffer{} + cmd.SetOut(&outBuf) + errBuf := bytes.Buffer{} + cmd.SetErr(&errBuf) + + // Reset flag values so they don't leak between tests + // FIXME: change how we initialize Cobra commands to render this hack unnecessary + cmd.Flags().VisitAll(func(f *pflag.Flag) { + switch v := f.Value.(type) { + case pflag.SliceValue: + _ = v.Replace([]string{}) + default: + switch v.Type() { + case "bool", "string", "int": + _ = v.Set(f.DefValue) + } + } + }) + + _, err = rootCmd.ExecuteC() + cmd.SetOut(nil) + cmd.SetErr(nil) + + return &cmdOut{&outBuf, &errBuf}, err +} + type errorStub struct { message string }