Skip to content

Commit 0e18bb6

Browse files
Merge pull request cli#14 from github/test
Add test for `gh pr list`
2 parents 1009ad5 + 13308dd commit 0e18bb6

15 files changed

+237
-4
lines changed

api/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type graphQLResponse struct {
2121
}
2222

2323
/*
24-
graphQL usage
24+
GraphQL: Declared as an external variable so it can be mocked in tests
2525
2626
type repoResponse struct {
2727
Repository struct {
@@ -45,7 +45,7 @@ if err != nil {
4545
4646
fmt.Printf("%+v\n", resp)
4747
*/
48-
func graphQL(query string, variables map[string]string, data interface{}) error {
48+
var GraphQL = func(query string, variables map[string]string, data interface{}) error {
4949
url := "https://api.github.com/graphql"
5050
reqBody, err := json.Marshal(map[string]interface{}{"query": query, "variables": variables})
5151
if err != nil {

api/queries.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func PullRequests() (PullRequestsPayload, error) {
9898
}
9999

100100
var resp response
101-
err := graphQL(query, variables, &resp)
101+
err := GraphQL(query, variables, &resp)
102102
if err != nil {
103103
return PullRequestsPayload{}, err
104104
}
@@ -133,7 +133,6 @@ func project() github.Project {
133133
if error != nil {
134134
panic(error)
135135
}
136-
137136
for _, remote := range remotes {
138137
if project, error := remote.Project(); error == nil {
139138
return *project

command/pr_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package command
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/github/gh-cli/test"
8+
)
9+
10+
func TestPRList(t *testing.T) {
11+
teardown := test.MockGraphQLResponse("test/fixtures/pr.json")
12+
defer teardown()
13+
14+
gitRepo := test.UseTempGitRepo()
15+
defer gitRepo.TearDown()
16+
17+
output, err := test.RunCommand(RootCmd, "pr list")
18+
if err != nil {
19+
t.Errorf("error running command `pr list`: %v", err)
20+
}
21+
22+
expectedPrs := []*regexp.Regexp{
23+
regexp.MustCompile(`#8.*\[strawberries\]`),
24+
regexp.MustCompile(`#9.*\[apples\]`),
25+
regexp.MustCompile(`#10.*\[blueberries\]`),
26+
regexp.MustCompile(`#11.*\[figs\]`),
27+
}
28+
29+
for _, r := range expectedPrs {
30+
if !r.MatchString(output) {
31+
t.Errorf("output did not match regexp /%s/", r)
32+
}
33+
}
34+
}

test/fixtures/pr.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"repository": {
3+
"pullRequests": {
4+
"edges": [
5+
{
6+
"node": {
7+
"number": 10,
8+
"title": "Blueberries are a good fruit",
9+
"url": "https://github.com/github/gh-cli/pull/10",
10+
"headRefName": "[blueberries]"
11+
}
12+
}
13+
]
14+
}
15+
},
16+
"viewerCreated": {
17+
"edges": [
18+
{
19+
"node": {
20+
"number": 8,
21+
"title": "Strawberries are not actually berries",
22+
"url": "https://github.com/github/gh-cli/pull/8",
23+
"headRefName": "[strawberries]"
24+
}
25+
}
26+
],
27+
"pageInfo": { "hasNextPage": false }
28+
},
29+
"reviewRequested": {
30+
"edges": [
31+
{
32+
"node": {
33+
"number": 9,
34+
"title": "Apples are tasty",
35+
"url": "https://github.com/github/gh-cli/pull/9",
36+
"headRefName": "[apples]"
37+
}
38+
},
39+
{
40+
"node": {
41+
"number": 11,
42+
"title": "Figs are my favorite",
43+
"url": "https://github.com/github/gh-cli/pull/1",
44+
"headRefName": "[figs]"
45+
}
46+
}
47+
],
48+
"pageInfo": { "hasNextPage": false }
49+
}
50+
}

test/fixtures/test.git/HEAD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master

test/fixtures/test.git/config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = true
5+
ignorecase = true
6+
precomposeunicode = false
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# git ls-files --others --exclude-from=.git/info/exclude
2+
# Lines that start with '#' are comments.
3+
# For a project mostly in C, the following would be a good set of
4+
# exclude patterns (uncomment them if you want to use them):
5+
# *.[oa]
6+
# *~
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x��M
2+
�0F]�seb~���n�x��cMi�)^ߠ7p�>x��b�9U��7ub{��bgٴ��ă#h8����>��4o����
3+
��'�yȜ�],�ڄ�Fk��-�j�U���W���*�z�I�)�;�
Binary file not shown.

test/fixtures/test.git/objects/9b/5a719a3d76ac9dc2fa635d9b1f34fd73994c06

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x��K
2+
1D]�}�3�$q�Bϐdq&#^��s7UPūX�qh�/ZM �w*�cbc�G�3����Y��I���k� g
3+
6-U�s4i�6��1�F��#Y�,���K��0��i��G��|��=�~��b7 ��$�bKdD1�3eK���n���Z�C��՟{Nm

0 commit comments

Comments
 (0)