From 5a23113b5bad8fd37efdffe0b33005f6983e3b11 Mon Sep 17 00:00:00 2001 From: Toshiya Doi Date: Mon, 16 Mar 2020 04:21:35 +0900 Subject: [PATCH] Add tests for the Closed/Merged PR preview --- command/pr_test.go | 60 +++++++++++++++++++++ test/fixtures/prViewPreviewClosedState.json | 25 +++++++++ test/fixtures/prViewPreviewMergedState.json | 25 +++++++++ 3 files changed, 110 insertions(+) create mode 100644 test/fixtures/prViewPreviewClosedState.json create mode 100644 test/fixtures/prViewPreviewMergedState.json diff --git a/command/pr_test.go b/command/pr_test.go index f3884b46a..b3336b640 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -411,6 +411,66 @@ func TestPRView_preview(t *testing.T) { } } +func TestPRView_previewClosedState(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/prViewPreviewClosedState.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(`CLOSED • 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 + } + } +} + +func TestPRView_previewMergedState(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/prViewPreviewMergedState.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(`MERGED • 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 + } + } +} + func TestPRView_previewCurrentBranch(t *testing.T) { initBlankContext("OWNER/REPO", "blueberries") http := initFakeHTTP() diff --git a/test/fixtures/prViewPreviewClosedState.json b/test/fixtures/prViewPreviewClosedState.json new file mode 100644 index 000000000..9df2c1038 --- /dev/null +++ b/test/fixtures/prViewPreviewClosedState.json @@ -0,0 +1,25 @@ +{ + "data": { + "repository": { + "pullRequest": { + "number": 12, + "title": "Blueberries are from a fork", + "state": "CLOSED", + "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 + } + } + } +} diff --git a/test/fixtures/prViewPreviewMergedState.json b/test/fixtures/prViewPreviewMergedState.json new file mode 100644 index 000000000..d62449092 --- /dev/null +++ b/test/fixtures/prViewPreviewMergedState.json @@ -0,0 +1,25 @@ +{ + "data": { + "repository": { + "pullRequest": { + "number": 12, + "title": "Blueberries are from a fork", + "state": "MERGED", + "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 + } + } + } +}