diff --git a/command/issue_test.go b/command/issue_test.go index 47924e865..45abfe04b 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -33,7 +33,7 @@ func TestIssueStatus(t *testing.T) { } for _, r := range expectedIssues { - if !r.MatchString(output.String()) { + if !r.MatchString(output) { t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) return } @@ -45,12 +45,11 @@ func TestIssueStatus_blankSlate(t *testing.T) { http := initFakeHTTP() http.StubResponse(200, bytes.NewBufferString(` - { "data": { "repository": { - "hasIssuesEnabled": true, - "assigned": { "nodes": [] }, - "mentioned": { "nodes": [] }, - "authored": { "nodes": [] } - } } } + { "data": { + "assigned": { "issues": { "nodes": [] } }, + "mentioned": { "issues": { "nodes": [] } }, + "authored": { "issues": { "nodes": [] } } + } } `)) output, err := RunCommand(issueStatusCmd, "issue status") @@ -68,27 +67,11 @@ Issues opened by you There are no issues opened by you ` - if output.String() != expectedOutput { + if output != expectedOutput { t.Errorf("expected %q, got %q", expectedOutput, output) } } -func TestIssueStatus_disabledIssues(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - - http.StubResponse(200, bytes.NewBufferString(` - { "data": { "repository": { - "hasIssuesEnabled": false - } } } - `)) - - _, err := RunCommand(issueStatusCmd, "issue status") - if err == nil || err.Error() != "the 'OWNER/REPO' repository has disabled issues" { - t.Errorf("error running command `issue status`: %v", err) - } -} - func TestIssueList(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() @@ -109,7 +92,7 @@ func TestIssueList(t *testing.T) { } for _, r := range expectedIssues { - if !r.MatchString(output.String()) { + if !r.MatchString(output) { t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) return } @@ -117,24 +100,15 @@ func TestIssueList(t *testing.T) { } func TestIssueList_withFlags(t *testing.T) { - initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() - http.StubResponse(200, bytes.NewBufferString(` - { "data": { "repository": { - "hasIssuesEnabled": true, - "issues": { "nodes": [] } - } } } - `)) + http.StubResponse(200, bytes.NewBufferString(`{"data": {}}`)) // Since we are testing that the flags are passed, we don't care about the response - output, err := RunCommand(issueListCmd, "issue list -a probablyCher -l web,bug -s open") + _, err := RunCommand(issueListCmd, "issue list -a probablyCher -l web,bug -s open") if err != nil { t.Errorf("error running command `issue list`: %v", err) } - eq(t, output.String(), "") - eq(t, output.Stderr(), "No issues match your search\n") - bodyBytes, _ := ioutil.ReadAll(http.Requests[0].Body) reqBody := struct { Variables struct { @@ -150,50 +124,6 @@ func TestIssueList_withFlags(t *testing.T) { eq(t, reqBody.Variables.States, []string{"OPEN"}) } -func TestIssueList_nullAssigneeLabels(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - - http.StubResponse(200, bytes.NewBufferString(` - { "data": { "repository": { - "hasIssuesEnabled": true, - "issues": { "nodes": [] } - } } } - `)) - - _, err := RunCommand(issueListCmd, "issue list") - if err != nil { - t.Errorf("error running command `issue list`: %v", err) - } - - bodyBytes, _ := ioutil.ReadAll(http.Requests[0].Body) - reqBody := struct { - Variables map[string]interface{} - }{} - json.Unmarshal(bodyBytes, &reqBody) - - _, assigneeDeclared := reqBody.Variables["assignee"] - _, labelsDeclared := reqBody.Variables["labels"] - eq(t, assigneeDeclared, false) - eq(t, labelsDeclared, false) -} - -func TestIssueList_disabledIssues(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - - http.StubResponse(200, bytes.NewBufferString(` - { "data": { "repository": { - "hasIssuesEnabled": false - } } } - `)) - - _, err := RunCommand(issueListCmd, "issue list") - if err == nil || err.Error() != "the 'OWNER/REPO' repository has disabled issues" { - t.Errorf("error running command `issue list`: %v", err) - } -} - func TestIssueView(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() @@ -217,8 +147,9 @@ func TestIssueView(t *testing.T) { t.Errorf("error running command `issue view`: %v", err) } - eq(t, output.String(), "") - eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO/issues/123 in your browser.\n") + if output == "" { + t.Errorf("command output expected got an empty string") + } if seenCmd == nil { t.Fatal("expected a command to run") @@ -277,7 +208,9 @@ func TestIssueView_urlArg(t *testing.T) { t.Errorf("error running command `issue view`: %v", err) } - eq(t, output.String(), "") + if output == "" { + t.Errorf("command output expected got an empty string") + } if seenCmd == nil { t.Fatal("expected a command to run") @@ -292,8 +225,7 @@ func TestIssueCreate(t *testing.T) { http.StubResponse(200, bytes.NewBufferString(` { "data": { "repository": { - "id": "REPOID", - "hasIssuesEnabled": true + "id": "REPOID" } } } `)) http.StubResponse(200, bytes.NewBufferString(` @@ -323,24 +255,7 @@ func TestIssueCreate(t *testing.T) { eq(t, reqBody.Variables.Input.Title, "hello") eq(t, reqBody.Variables.Input.Body, "cash rules everything around me") - eq(t, output.String(), "https://github.com/OWNER/REPO/issues/12\n") -} - -func TestIssueCreate_disabledIssues(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - - http.StubResponse(200, bytes.NewBufferString(` - { "data": { "repository": { - "id": "REPOID", - "hasIssuesEnabled": false - } } } - `)) - - _, err := RunCommand(issueCreateCmd, `issue create -t heres -b johnny`) - if err == nil || err.Error() != "the 'OWNER/REPO' repository has disabled issues" { - t.Errorf("error running command `issue create`: %v", err) - } + eq(t, output, "https://github.com/OWNER/REPO/issues/12\n") } func TestIssueCreate_web(t *testing.T) { @@ -364,5 +279,5 @@ func TestIssueCreate_web(t *testing.T) { } url := seenCmd.Args[len(seenCmd.Args)-1] eq(t, url, "https://github.com/OWNER/REPO/issues/new") - eq(t, output.String(), "Opening https://github.com/OWNER/REPO/issues/new in your browser.\n") + eq(t, output, "Opening https://github.com/OWNER/REPO/issues/new in your browser.\n") } diff --git a/command/pr_create_test.go b/command/pr_create_test.go index badfe7204..50e280d69 100644 --- a/command/pr_create_test.go +++ b/command/pr_create_test.go @@ -91,7 +91,7 @@ func TestPRCreate(t *testing.T) { eq(t, reqBody.Variables.Input.BaseRefName, "master") eq(t, reqBody.Variables.Input.HeadRefName, "feature") - eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n") + eq(t, output, "https://github.com/OWNER/REPO/pull/12\n") } func TestPRCreate_web(t *testing.T) { @@ -115,8 +115,9 @@ func TestPRCreate_web(t *testing.T) { output, err := RunCommand(prCreateCmd, `pr create --web`) eq(t, err, nil) - eq(t, output.String(), "") - eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO/pull/feature in your browser.\n") + if output == "" { + t.Fatal("expected output") + } eq(t, len(ranCommands), 3) eq(t, strings.Join(ranCommands[1], " "), "git push --set-upstream origin HEAD:feature") @@ -154,6 +155,7 @@ func TestPRCreate_ReportsUncommittedChanges(t *testing.T) { output, err := RunCommand(prCreateCmd, `pr create -t "my title" -b "my body"`) eq(t, err, nil) - eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n") - eq(t, output.Stderr(), "Warning: 1 uncommitted change\n") + eq(t, output, `Warning: 1 uncommitted change +https://github.com/OWNER/REPO/pull/12 +`) } diff --git a/command/pr_test.go b/command/pr_test.go index 91ec11a13..dadeeada0 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -24,50 +24,31 @@ 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) { +func RunCommand(cmd *cobra.Command, args string) (string, error) { rootCmd := cmd.Root() argv, err := shlex.Split(args) if err != nil { - return nil, err + return "", 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": + if v.Type() == "bool" { v.Set(f.DefValue) } } }) _, err = rootCmd.ExecuteC() - cmd.SetOut(nil) - cmd.SetErr(nil) - - return &cmdOut{&outBuf, &errBuf}, err + return outBuf.String(), err } func TestPRStatus(t *testing.T) { @@ -91,66 +72,12 @@ func TestPRStatus(t *testing.T) { } for _, r := range expectedPrs { - if !r.MatchString(output.String()) { + if !r.MatchString(output) { t.Errorf("output did not match regexp /%s/", r) } } } -func TestPRStatus_reviewsAndChecks(t *testing.T) { - initBlankContext("OWNER/REPO", "blueberries") - http := initFakeHTTP() - - jsonFile, _ := os.Open("../test/fixtures/prStatusChecks.json") - defer jsonFile.Close() - http.StubResponse(200, jsonFile) - - output, err := RunCommand(prStatusCmd, "pr status") - if err != nil { - t.Errorf("error running command `pr status`: %v", err) - } - - expected := []string{ - "- Checks passing - changes requested", - "- Checks pending - approved", - "- 1/3 checks failing - review required", - } - - for _, line := range expected { - if !strings.Contains(output.String(), line) { - t.Errorf("output did not contain %q: %q", line, output.String()) - } - } -} - -func TestPRStatus_blankSlate(t *testing.T) { - initBlankContext("OWNER/REPO", "blueberries") - http := initFakeHTTP() - - http.StubResponse(200, bytes.NewBufferString(` - { "data": {} } - `)) - - output, err := RunCommand(prStatusCmd, "pr status") - if err != nil { - t.Errorf("error running command `pr status`: %v", err) - } - - expected := `Current branch - There is no pull request associated with [blueberries] - -Created by you - You have no open pull requests - -Requesting a code review from you - You have no pull requests to review - -` - if output.String() != expected { - t.Errorf("expected %q, got %q", expected, output.String()) - } -} - func TestPRList(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() @@ -164,7 +91,7 @@ func TestPRList(t *testing.T) { t.Fatal(err) } - eq(t, output.String(), `32 New feature feature + eq(t, output, `32 New feature feature 29 Fixed bad bug hubot:bug-fix 28 Improve documentation docs `) @@ -177,14 +104,11 @@ func TestPRList_filtering(t *testing.T) { respBody := bytes.NewBufferString(`{ "data": {} }`) http.StubResponse(200, respBody) - output, err := RunCommand(prListCmd, `pr list -s all -l one,two -l three`) + _, err := RunCommand(prListCmd, `pr list -s all -l one,two -l three`) if err != nil { t.Fatal(err) } - eq(t, output.String(), "") - eq(t, output.Stderr(), "No pull requests match your search\n") - bodyBytes, _ := ioutil.ReadAll(http.Requests[0].Body) reqBody := struct { Variables struct { @@ -259,8 +183,9 @@ func TestPRView_currentBranch(t *testing.T) { t.Errorf("error running command `pr view`: %v", err) } - eq(t, output.String(), "") - eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO/pull/10 in your browser.\n") + if output == "" { + t.Errorf("command output expected got an empty string") + } if seenCmd == nil { t.Fatal("expected a command to run") @@ -323,7 +248,9 @@ func TestPRView_numberArg(t *testing.T) { t.Errorf("error running command `pr view`: %v", err) } - eq(t, output.String(), "") + if output == "" { + t.Errorf("command output expected got an empty string") + } if seenCmd == nil { t.Fatal("expected a command to run") @@ -354,7 +281,9 @@ func TestPRView_urlArg(t *testing.T) { t.Errorf("error running command `pr view`: %v", err) } - eq(t, output.String(), "") + if output == "" { + t.Errorf("command output expected got an empty string") + } if seenCmd == nil { t.Fatal("expected a command to run") @@ -387,7 +316,9 @@ func TestPRView_branchArg(t *testing.T) { t.Errorf("error running command `pr view`: %v", err) } - eq(t, output.String(), "") + if output == "" { + t.Errorf("command output expected got an empty string") + } if seenCmd == nil { t.Fatal("expected a command to run") @@ -421,7 +352,9 @@ func TestPRView_branchWithOwnerArg(t *testing.T) { t.Errorf("error running command `pr view`: %v", err) } - eq(t, output.String(), "") + if output == "" { + t.Errorf("command output expected got an empty string") + } if seenCmd == nil { t.Fatal("expected a command to run")