Clean some things up

This commit is contained in:
Corey Johnson 2019-10-11 10:43:06 -07:00
parent 5b28291762
commit 1303204b80
2 changed files with 20 additions and 9 deletions

BIN
command.test Executable file

Binary file not shown.

View file

@ -5,21 +5,20 @@ import (
"io/ioutil"
"os"
"regexp"
"strings"
"testing"
"github.com/github/gh-cli/api"
)
func TestPRList(t *testing.T) {
teardown := mockGraphQL("pr.json")
teardown := mockGraphQLResponse("fixtures/pr.json")
defer teardown()
output := captureOutput(func() {
err := ExecutePr()
if err != nil {
t.Errorf("got error %v", err)
}
})
output, err := runCommand("pr list")
if err != nil {
t.Errorf("error running command `pr list`: %v", err)
}
expectedPrs := []*regexp.Regexp{
regexp.MustCompile(`#8.*\[strawberries\]`),
@ -35,9 +34,9 @@ func TestPRList(t *testing.T) {
}
}
func mockGraphQL(fixtureName string) (teardown func()) {
func mockGraphQLResponse(fixturePath string) (teardown func()) {
api.OverriddenQueryFunction = func(query string, variables map[string]string, v interface{}) error {
contents, err := ioutil.ReadFile("fixtures/" + fixtureName)
contents, err := ioutil.ReadFile(fixturePath)
if err != nil {
return err
}
@ -54,6 +53,18 @@ func mockGraphQL(fixtureName string) (teardown func()) {
}
}
func runCommand(s string) (string, error) {
var err error
output := captureOutput(func() {
RootCmd.SetArgs(strings.Split(s, " "))
_, err = RootCmd.ExecuteC()
})
if err != nil {
return "", err
}
return output, nil
}
func captureOutput(f func()) string {
originalStdout := os.Stdout
defer func() {