Fix tests

This commit is contained in:
Corey Johnson 2019-12-20 12:04:23 -08:00
parent 8f74b658b2
commit 58761a8dfc
3 changed files with 49 additions and 40 deletions

View file

@ -33,7 +33,7 @@ func TestIssueStatus(t *testing.T) {
}
for _, r := range expectedIssues {
if !r.MatchString(output) {
if !r.MatchString(output.String()) {
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
return
}
@ -68,7 +68,7 @@ Issues opened by you
There are no issues opened by you
`
if output != expectedOutput {
if output.String() != expectedOutput {
t.Errorf("expected %q, got %q", expectedOutput, output)
}
}
@ -109,7 +109,7 @@ func TestIssueList(t *testing.T) {
}
for _, r := range expectedIssues {
if !r.MatchString(output) {
if !r.MatchString(output.String()) {
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
return
}
@ -127,11 +127,14 @@ func TestIssueList_withFlags(t *testing.T) {
} } }
`))
_, err := RunCommand(issueListCmd, "issue list -a probablyCher -l web,bug -s open")
output, 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 {
@ -214,9 +217,8 @@ func TestIssueView(t *testing.T) {
t.Errorf("error running command `issue view`: %v", err)
}
if output == "" {
t.Errorf("command output expected got an empty string")
}
eq(t, output.String(), "")
eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO/issues/123 in your browser.\n")
if seenCmd == nil {
t.Fatal("expected a command to run")
@ -275,9 +277,7 @@ func TestIssueView_urlArg(t *testing.T) {
t.Errorf("error running command `issue view`: %v", err)
}
if output == "" {
t.Errorf("command output expected got an empty string")
}
eq(t, output.String(), "")
if seenCmd == nil {
t.Fatal("expected a command to run")
@ -323,7 +323,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, "https://github.com/OWNER/REPO/issues/12\n")
eq(t, output.String(), "https://github.com/OWNER/REPO/issues/12\n")
}
func TestIssueCreate_disabledIssues(t *testing.T) {
@ -364,5 +364,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, "Opening https://github.com/OWNER/REPO/issues/new in your browser.\n")
eq(t, output.String(), "Opening https://github.com/OWNER/REPO/issues/new in your browser.\n")
}

View file

@ -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, "https://github.com/OWNER/REPO/pull/12\n")
eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n")
}
func TestPRCreate_web(t *testing.T) {
@ -115,9 +115,8 @@ func TestPRCreate_web(t *testing.T) {
output, err := RunCommand(prCreateCmd, `pr create --web`)
eq(t, err, nil)
if output == "" {
t.Fatal("expected output")
}
eq(t, output.String(), "")
eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO/pull/feature in your browser.\n")
eq(t, len(ranCommands), 3)
eq(t, strings.Join(ranCommands[1], " "), "git push --set-upstream origin HEAD:feature")
@ -155,7 +154,6 @@ 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, `Warning: 1 uncommitted change
https://github.com/OWNER/REPO/pull/12
`)
eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n")
eq(t, output.Stderr(), "Warning: 1 uncommitted change\n")
}

View file

@ -24,16 +24,30 @@ func eq(t *testing.T, got interface{}, expected interface{}) {
}
}
func RunCommand(cmd *cobra.Command, args string) (string, error) {
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 "", err
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
@ -50,7 +64,10 @@ func RunCommand(cmd *cobra.Command, args string) (string, error) {
})
_, err = rootCmd.ExecuteC()
return outBuf.String(), err
cmd.SetOut(nil)
cmd.SetErr(nil)
return &cmdOut{&outBuf, &errBuf}, err
}
func TestPRStatus(t *testing.T) {
@ -74,7 +91,7 @@ func TestPRStatus(t *testing.T) {
}
for _, r := range expectedPrs {
if !r.MatchString(output) {
if !r.MatchString(output.String()) {
t.Errorf("output did not match regexp /%s/", r)
}
}
@ -147,7 +164,7 @@ func TestPRList(t *testing.T) {
t.Fatal(err)
}
eq(t, output, `32 New feature feature
eq(t, output.String(), `32 New feature feature
29 Fixed bad bug hubot:bug-fix
28 Improve documentation docs
`)
@ -160,11 +177,14 @@ func TestPRList_filtering(t *testing.T) {
respBody := bytes.NewBufferString(`{ "data": {} }`)
http.StubResponse(200, respBody)
_, err := RunCommand(prListCmd, `pr list -s all -l one,two -l three`)
output, 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 {
@ -239,9 +259,8 @@ func TestPRView_currentBranch(t *testing.T) {
t.Errorf("error running command `pr view`: %v", err)
}
if output == "" {
t.Errorf("command output expected got an empty string")
}
eq(t, output.String(), "")
eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO/pull/10 in your browser.\n")
if seenCmd == nil {
t.Fatal("expected a command to run")
@ -304,9 +323,7 @@ func TestPRView_numberArg(t *testing.T) {
t.Errorf("error running command `pr view`: %v", err)
}
if output == "" {
t.Errorf("command output expected got an empty string")
}
eq(t, output.String(), "")
if seenCmd == nil {
t.Fatal("expected a command to run")
@ -337,9 +354,7 @@ func TestPRView_urlArg(t *testing.T) {
t.Errorf("error running command `pr view`: %v", err)
}
if output == "" {
t.Errorf("command output expected got an empty string")
}
eq(t, output.String(), "")
if seenCmd == nil {
t.Fatal("expected a command to run")
@ -372,9 +387,7 @@ func TestPRView_branchArg(t *testing.T) {
t.Errorf("error running command `pr view`: %v", err)
}
if output == "" {
t.Errorf("command output expected got an empty string")
}
eq(t, output.String(), "")
if seenCmd == nil {
t.Fatal("expected a command to run")
@ -408,9 +421,7 @@ func TestPRView_branchWithOwnerArg(t *testing.T) {
t.Errorf("error running command `pr view`: %v", err)
}
if output == "" {
t.Errorf("command output expected got an empty string")
}
eq(t, output.String(), "")
if seenCmd == nil {
t.Fatal("expected a command to run")