diff --git a/command.test b/command.test new file mode 100755 index 000000000..bcc0e9e7d Binary files /dev/null and b/command.test differ diff --git a/command/pr_test.go b/command/pr_test.go index 15ba29e50..078ec2e2d 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -5,21 +5,20 @@ import ( "io/ioutil" "os" "regexp" + "strings" "testing" "github.com/github/gh-cli/api" ) func TestPRList(t *testing.T) { - teardown := mockGraphQL("pr.json") + teardown := mockGraphQLResponse("fixtures/pr.json") defer teardown() - output := captureOutput(func() { - err := ExecutePr() - if err != nil { - t.Errorf("got error %v", err) - } - }) + output, err := runCommand("pr list") + if err != nil { + t.Errorf("error running command `pr list`: %v", err) + } expectedPrs := []*regexp.Regexp{ regexp.MustCompile(`#8.*\[strawberries\]`), @@ -35,9 +34,9 @@ func TestPRList(t *testing.T) { } } -func mockGraphQL(fixtureName string) (teardown func()) { +func mockGraphQLResponse(fixturePath string) (teardown func()) { api.OverriddenQueryFunction = func(query string, variables map[string]string, v interface{}) error { - contents, err := ioutil.ReadFile("fixtures/" + fixtureName) + contents, err := ioutil.ReadFile(fixturePath) if err != nil { return err } @@ -54,6 +53,18 @@ func mockGraphQL(fixtureName string) (teardown func()) { } } +func runCommand(s string) (string, error) { + var err error + output := captureOutput(func() { + RootCmd.SetArgs(strings.Split(s, " ")) + _, err = RootCmd.ExecuteC() + }) + if err != nil { + return "", err + } + return output, nil +} + func captureOutput(f func()) string { originalStdout := os.Stdout defer func() {