add tests for pr/issue previewing in terminal

This commit is contained in:
vilmibm 2020-01-10 15:13:02 -06:00
parent f911a234c2
commit bbdf30c8f8
4 changed files with 107 additions and 6 deletions

View file

@ -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()

View file

@ -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 {

View file

@ -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()

24
test/fixtures/prViewPreview.json vendored Normal file
View file

@ -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
}
}
}
}