Add PRView tests

This commit is contained in:
Corey Johnson 2019-10-16 14:47:47 -07:00
parent 9704bb55b1
commit a8b7e4c5f3
5 changed files with 93 additions and 16 deletions

View file

@ -4,7 +4,6 @@ import (
"fmt"
"net/url"
"os"
"os/exec"
"strconv"
"strings"
@ -88,7 +87,9 @@ func prView(cmd *cobra.Command, args []string) error {
}
} else {
prPayload, err := api.PullRequests()
if err != nil || prPayload.CurrentPR == nil {
if err != nil {
return err
} else if prPayload.CurrentPR == nil {
branch := currentBranch()
return fmt.Errorf("The [%s] branch has no open PRs", branch)
}
@ -96,7 +97,7 @@ func prView(cmd *cobra.Command, args []string) error {
}
fmt.Printf("Opening %s in your browser.\n", openURL)
return openInBrowser(openURL)
return utils.OpenInBrowser(openURL)
}
func printPrs(prs ...api.PullRequest) {
@ -122,15 +123,6 @@ func truncateTitle(title string) string {
return title
}
func openInBrowser(url string) error {
launcher, err := utils.BrowserLauncher()
if err != nil {
return err
}
endingArgs := append(launcher[1:], url)
return exec.Command(launcher[0], endingArgs...).Run()
}
// The functions below should be replaced at some point by the context package
// 🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨🧨
func currentBranch() string {

View file

@ -4,10 +4,12 @@ import (
"regexp"
"testing"
"github.com/github/gh-cli/utils"
"github.com/github/gh-cli/test"
)
func TestPRList(t *testing.T) {
func FTestPRList(t *testing.T) {
teardown := test.MockGraphQLResponse("test/fixtures/pr.json")
defer teardown()
@ -32,3 +34,30 @@ func TestPRList(t *testing.T) {
}
}
}
func TestPRView(t *testing.T) {
teardown := test.MockGraphQLResponse("test/fixtures/prView.json")
defer teardown()
gitRepo := test.UseTempGitRepo()
defer gitRepo.TearDown()
openInBrowserCalls := 0
utils.OpenInBrowser = func(_ string) error {
openInBrowserCalls++
return nil
}
output, err := test.RunCommand(RootCmd, "pr view")
if err != nil {
t.Errorf("error running command `pr view`: %v", err)
}
if output == "" {
t.Errorf("command output expected got an empty string")
}
if openInBrowserCalls != 1 {
t.Errorf("OpenInBrowser should be called 1 time but was called %d time(s)", openInBrowserCalls)
}
}

50
test/fixtures/prView.json vendored Normal file
View file

@ -0,0 +1,50 @@
{
"repository": {
"pullRequests": {
"edges": [
{
"node": {
"number": 10,
"title": "Blueberries are a good fruit",
"url": "https://github.com/github/gh-cli/pull/10",
"headRefName": "[blueberries]"
}
}
]
}
},
"viewerCreated": {
"edges": [
{
"node": {
"number": 8,
"title": "Strawberries are not actually berries",
"url": "https://github.com/github/gh-cli/pull/8",
"headRefName": "[strawberries]"
}
}
],
"pageInfo": { "hasNextPage": false }
},
"reviewRequested": {
"edges": [
{
"node": {
"number": 9,
"title": "Apples are tasty",
"url": "https://github.com/github/gh-cli/pull/9",
"headRefName": "[apples]"
}
},
{
"node": {
"number": 11,
"title": "Figs are my favorite",
"url": "https://github.com/github/gh-cli/pull/1",
"headRefName": "[figs]"
}
}
],
"pageInfo": { "hasNextPage": false }
}
}

View file

@ -27,7 +27,7 @@ func ConcatPaths(paths ...string) string {
return strings.Join(paths, "/")
}
func BrowserLauncher() ([]string, error) {
var OpenInBrowser = func(url string) error {
browser := os.Getenv("BROWSER")
if browser == "" {
browser = searchBrowserLauncher(runtime.GOOS)
@ -36,10 +36,16 @@ func BrowserLauncher() ([]string, error) {
}
if browser == "" {
return nil, errors.New("Please set $BROWSER to a web launcher")
return errors.New("Please set $BROWSER to a web launcher")
}
return shellquote.Split(browser)
browserArgs, err := shellquote.Split(browser)
if err != nil {
return err
}
endingArgs := append(browserArgs[1:], url)
return exec.Command(browserArgs[0], endingArgs...).Run()
}
func searchBrowserLauncher(goos string) (browser string) {