From bbdf30c8f877c4c2f70f11878184c7296fa4c4f0 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Fri, 10 Jan 2020 15:13:02 -0600 Subject: [PATCH] add tests for pr/issue previewing in terminal --- command/issue_test.go | 45 ++++++++++++++++++++++++++++++++ command/pr.go | 15 ++++++----- command/pr_test.go | 29 ++++++++++++++++++++ test/fixtures/prViewPreview.json | 24 +++++++++++++++++ 4 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/prViewPreview.json diff --git a/command/issue_test.go b/command/issue_test.go index 78189e4c7..93229dac2 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -231,6 +231,51 @@ func TestIssueView(t *testing.T) { eq(t, url, "https://github.com/OWNER/REPO/issues/123") } +func TestIssueView_preview(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { "issue": { + "number": 123, + "body": "**bold story**", + "title": "ix of coins", + "author": { + "login": "marseilles" + }, + "labels": { + "nodes": [ + {"name": "tarot"} + ] + }, + "comments": { + "totalCount": 9 + }, + "url": "https://github.com/OWNER/REPO/issues/123" + } } } } + `)) + + output, err := RunCommand(issueViewCmd, "issue view -p 123") + if err != nil { + t.Errorf("error running command `issue view`: %v", err) + } + + 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 + } + } +} + func TestIssueView_notFound(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() diff --git a/command/pr.go b/command/pr.go index 82bbec7b2..85ac58fa1 100644 --- a/command/pr.go +++ b/command/pr.go @@ -298,17 +298,20 @@ func prView(cmd *cobra.Command, args []string) error { meta += "s " } meta += "into %s from %s" - fmt.Println(utils.Bold(pr.Title)) - fmt.Println(utils.Gray(fmt.Sprintf(meta, + + out := colorableOut(cmd) + + fmt.Fprintln(out, utils.Bold(pr.Title)) + fmt.Fprintln(out, utils.Gray(fmt.Sprintf(meta, pr.Author.Login, pr.Commits.TotalCount, pr.BaseRefName, pr.HeadRefName, ))) - fmt.Println() - fmt.Println(utils.RenderMarkdown(pr.Body)) - fmt.Println() - fmt.Println(utils.Gray(fmt.Sprintf("View this PR on GitHub: %s", openURL))) + fmt.Fprintln(out) + fmt.Fprintln(out, utils.RenderMarkdown(pr.Body)) + fmt.Fprintln(out) + fmt.Fprintf(out, utils.Gray("View this PR on GitHub: %s\n"), openURL) return nil } else { diff --git a/command/pr_test.go b/command/pr_test.go index 360b6d852..e901b38bf 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -238,6 +238,35 @@ func TestPRList_filteringAssigneeLabels(t *testing.T) { } } +func TestPRView_preview(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + + jsonFile, _ := os.Open("../test/fixtures/prViewPreview.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + output, err := RunCommand(prViewCmd, "pr view -p 12") + if err != nil { + t.Errorf("error running command `pr view`: %v", err) + } + + 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 PR 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 + } + } +} + func TestPRView_currentBranch(t *testing.T) { initBlankContext("OWNER/REPO", "blueberries") http := initFakeHTTP() diff --git a/test/fixtures/prViewPreview.json b/test/fixtures/prViewPreview.json new file mode 100644 index 000000000..e40946774 --- /dev/null +++ b/test/fixtures/prViewPreview.json @@ -0,0 +1,24 @@ +{ + "data": { + "repository": { + "pullRequest": { + "number": 12, + "title": "Blueberries are from a fork", + "body": "**blueberries taste good**", + "url": "https://github.com/OWNER/REPO/pull/12", + "author": { + "login": "nobody" + }, + "commits": { + "totalCount": 12 + }, + "baseRefName": "master", + "headRefName": "blueberries", + "headRepositoryOwner": { + "login": "hubot" + }, + "isCrossRepository": true + } + } + } +}