diff --git a/command/issue_test.go b/command/issue_test.go index 0d839d5a2..2f5875653 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -316,18 +316,11 @@ func TestIssueView_preview(t *testing.T) { eq(t, output.Stderr(), "") - expectedLines := []*regexp.Regexp{ - regexp.MustCompile(`ix of coins`), - regexp.MustCompile(`opened by marseilles. 9 comments. \(tarot\)`), - regexp.MustCompile(`bold story`), - regexp.MustCompile(`View this issue on GitHub: https://github.com/OWNER/REPO/issues/123`), - } - for _, r := range expectedLines { - if !r.MatchString(output.String()) { - t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) - return - } - } + test.ExpectLines(t, output.String(), + "ix of coins", + `opened by marseilles. 9 comments. \(tarot\)`, + "bold story", + "View this issue on GitHub: https://github.com/OWNER/REPO/issues/123") } func TestIssueView_previewWithEmptyBody(t *testing.T) { @@ -362,17 +355,10 @@ func TestIssueView_previewWithEmptyBody(t *testing.T) { eq(t, output.Stderr(), "") - expectedLines := []*regexp.Regexp{ - regexp.MustCompile(`ix of coins`), - regexp.MustCompile(`opened by marseilles. 9 comments. \(tarot\)`), - regexp.MustCompile(`View this issue on GitHub: https://github.com/OWNER/REPO/issues/123`), - } - for _, r := range expectedLines { - if !r.MatchString(output.String()) { - t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) - return - } - } + test.ExpectLines(t, output.String(), + "ix of coins", + `opened by marseilles. 9 comments. \(tarot\)`, + "View this issue on GitHub: https://github.com/OWNER/REPO/issues/123") } func TestIssueView_notFound(t *testing.T) { diff --git a/command/pr_test.go b/command/pr_test.go index 4b9d0c6de..56dae177e 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -416,18 +416,11 @@ func TestPRView_preview(t *testing.T) { eq(t, output.Stderr(), "") - expectedLines := []*regexp.Regexp{ - regexp.MustCompile(`Blueberries are from a fork`), - regexp.MustCompile(`nobody wants to merge 12 commits into master from blueberries`), - regexp.MustCompile(`blueberries taste good`), - regexp.MustCompile(`View this pull request on GitHub: https://github.com/OWNER/REPO/pull/12`), - } - for _, r := range expectedLines { - if !r.MatchString(output.String()) { - t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) - return - } - } + test.ExpectLines(t, output.String(), + "Blueberries are from a fork", + "nobody wants to merge 12 commits into master from blueberries", + "blueberries taste good", + "View this pull request on GitHub: https://github.com/OWNER/REPO/pull/12") } func TestPRView_previewCurrentBranch(t *testing.T) { @@ -451,18 +444,11 @@ func TestPRView_previewCurrentBranch(t *testing.T) { eq(t, output.Stderr(), "") - expectedLines := []*regexp.Regexp{ - regexp.MustCompile(`Blueberries are a good fruit`), - regexp.MustCompile(`nobody wants to merge 8 commits into master from blueberries`), - regexp.MustCompile(`blueberries taste good`), - regexp.MustCompile(`View this pull request on GitHub: https://github.com/OWNER/REPO/pull/10`), - } - for _, r := range expectedLines { - if !r.MatchString(output.String()) { - t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) - return - } - } + test.ExpectLines(t, output.String(), + "Blueberries are a good fruit", + "nobody wants to merge 8 commits into master from blueberries", + "blueberries taste good", + "View this pull request on GitHub: https://github.com/OWNER/REPO/pull/10") } func TestPRView_previewCurrentBranchWithEmptyBody(t *testing.T) { @@ -486,17 +472,10 @@ func TestPRView_previewCurrentBranchWithEmptyBody(t *testing.T) { eq(t, output.Stderr(), "") - expectedLines := []*regexp.Regexp{ - regexp.MustCompile(`Blueberries are a good fruit`), - regexp.MustCompile(`nobody wants to merge 8 commits into master from blueberries`), - regexp.MustCompile(`View this pull request on GitHub: https://github.com/OWNER/REPO/pull/10`), - } - for _, r := range expectedLines { - if !r.MatchString(output.String()) { - t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) - return - } - } + test.ExpectLines(t, output.String(), + "Blueberries are a good fruit", + "nobody wants to merge 8 commits into master from blueberries", + "View this pull request on GitHub: https://github.com/OWNER/REPO/pull/10") } func TestPRView_currentBranch(t *testing.T) { diff --git a/command/repo_test.go b/command/repo_test.go index 6001be817..3b4d65812 100644 --- a/command/repo_test.go +++ b/command/repo_test.go @@ -137,16 +137,9 @@ func TestRepoFork_in_parent_yes(t *testing.T) { eq(t, output.Stderr(), "") - expectedLines := []*regexp.Regexp{ - regexp.MustCompile(`Created fork someone/REPO`), - regexp.MustCompile(`Remote added at fork`), - } - for _, r := range expectedLines { - if !r.MatchString(output.String()) { - t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) - return - } - } + test.ExpectLines(t, output.String(), + "Created fork someone/REPO", + "Remote added at fork") } func TestRepoFork_outside_yes(t *testing.T) { @@ -169,16 +162,9 @@ func TestRepoFork_outside_yes(t *testing.T) { eq(t, strings.Join(seenCmd.Args, " "), "git clone https://github.com/someone/repo.git") - expectedLines := []*regexp.Regexp{ - regexp.MustCompile(`Created fork someone/REPO`), - regexp.MustCompile(`Cloned fork`), - } - for _, r := range expectedLines { - if !r.MatchString(output.String()) { - t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) - return - } - } + test.ExpectLines(t, output.String(), + "Created fork someone/REPO", + "Cloned fork") } func TestRepoFork_outside_survey_yes(t *testing.T) { @@ -208,16 +194,9 @@ func TestRepoFork_outside_survey_yes(t *testing.T) { eq(t, strings.Join(seenCmd.Args, " "), "git clone https://github.com/someone/repo.git") - expectedLines := []*regexp.Regexp{ - regexp.MustCompile(`Created fork someone/REPO`), - regexp.MustCompile(`Cloned fork`), - } - for _, r := range expectedLines { - if !r.MatchString(output.String()) { - t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) - return - } - } + test.ExpectLines(t, output.String(), + "Created fork someone/REPO", + "Cloned fork") } func TestRepoFork_outside_survey_no(t *testing.T) { @@ -290,16 +269,9 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) { eq(t, output.Stderr(), "") - expectedLines := []*regexp.Regexp{ - regexp.MustCompile(`Created fork someone/REPO`), - regexp.MustCompile(`Remote added at fork`), - } - for _, r := range expectedLines { - if !r.MatchString(output.String()) { - t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) - return - } - } + test.ExpectLines(t, output.String(), + "Created fork someone/REPO", + "Remote added at fork") } func TestRepoFork_in_parent_survey_no(t *testing.T) { diff --git a/test/helpers.go b/test/helpers.go index 6bcfcadca..c4c4f976b 100644 --- a/test/helpers.go +++ b/test/helpers.go @@ -6,6 +6,8 @@ import ( "os" "os/exec" "path/filepath" + "regexp" + "testing" ) // OutputStub implements a simple utils.Runnable @@ -74,3 +76,14 @@ func UseTempGitRepo() *TempGitRepo { return &TempGitRepo{Remote: remotePath, TearDown: tearDown} } + +func ExpectLines(t *testing.T, output string, lines ...string) { + var r *regexp.Regexp + for _, l := range lines { + r = regexp.MustCompile(l) + if !r.MatchString(output) { + t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output) + return + } + } +}