Skip to content

Commit a2d3923

Browse files
committed
Add unit tests for Closed and Merged states of the current branch
1 parent 74a2a24 commit a2d3923

File tree

3 files changed

+84
-42
lines changed

3 files changed

+84
-42
lines changed

command/pr_test.go

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ func TestPRStatus_reviewsAndChecks(t *testing.T) {
156156
}
157157
}
158158

159-
func TestPRStatus_closedMerged(t *testing.T) {
159+
func TestPRStatus_currentBranch_showTheMostRecentPR(t *testing.T) {
160160
initBlankContext("OWNER/REPO", "blueberries")
161161
http := initFakeHTTP()
162162
http.StubRepoResponse("OWNER", "REPO")
163163

164-
jsonFile, _ := os.Open("../test/fixtures/prStatusClosedMerged.json")
164+
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranch.json")
165165
defer jsonFile.Close()
166166
http.StubResponse(200, jsonFile)
167167

@@ -170,25 +170,30 @@ func TestPRStatus_closedMerged(t *testing.T) {
170170
t.Errorf("error running command `pr status`: %v", err)
171171
}
172172

173-
expected := []string{
174-
"- Checks passing - Changes requested",
175-
"- Closed",
176-
"- Merged",
173+
expectedLine := regexp.MustCompile(`#10 Blueberries are certainly a good fruit \[blueberries\]`)
174+
if !expectedLine.MatchString(output.String()) {
175+
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", expectedLine, output)
176+
return
177177
}
178178

179-
for _, line := range expected {
180-
if !strings.Contains(output.String(), line) {
181-
t.Errorf("output did not contain %q: %q", line, output.String())
179+
unexpectedLines := []*regexp.Regexp{
180+
regexp.MustCompile(`#9 Blueberries are a good fruit \[blueberries\] - Merged`),
181+
regexp.MustCompile(`#8 Blueberries are probably a good fruit \[blueberries\] - Closed`),
182+
}
183+
for _, r := range unexpectedLines {
184+
if r.MatchString(output.String()) {
185+
t.Errorf("output unexpectedly match regexp /%s/\n> output\n%s\n", r, output)
186+
return
182187
}
183188
}
184189
}
185190

186-
func TestPRStatus_currentBranch_showTheMostRecentPR(t *testing.T) {
191+
func TestPRStatus_currentBranch_Closed(t *testing.T) {
187192
initBlankContext("OWNER/REPO", "blueberries")
188193
http := initFakeHTTP()
189194
http.StubRepoResponse("OWNER", "REPO")
190195

191-
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranch.json")
196+
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranchClosed.json")
192197
defer jsonFile.Close()
193198
http.StubResponse(200, jsonFile)
194199

@@ -197,21 +202,31 @@ func TestPRStatus_currentBranch_showTheMostRecentPR(t *testing.T) {
197202
t.Errorf("error running command `pr status`: %v", err)
198203
}
199204

200-
expectedLine := regexp.MustCompile(`#10 Blueberries are certainly a good fruit \[blueberries\]`)
205+
expectedLine := regexp.MustCompile(`#8 Blueberries are a good fruit \[blueberries\] - Closed`)
201206
if !expectedLine.MatchString(output.String()) {
202207
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", expectedLine, output)
203208
return
204209
}
210+
}
205211

206-
unexpectedLines := []*regexp.Regexp{
207-
regexp.MustCompile(`#9 Blueberries are a good fruit \[blueberries\] - Merged`),
208-
regexp.MustCompile(`#8 Blueberries are probably a good fruit \[blueberries\] - Closed`),
212+
func TestPRStatus_currentBranch_Merged(t *testing.T) {
213+
initBlankContext("OWNER/REPO", "blueberries")
214+
http := initFakeHTTP()
215+
http.StubRepoResponse("OWNER", "REPO")
216+
217+
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranchMerged.json")
218+
defer jsonFile.Close()
219+
http.StubResponse(200, jsonFile)
220+
221+
output, err := RunCommand(prStatusCmd, "pr status")
222+
if err != nil {
223+
t.Errorf("error running command `pr status`: %v", err)
209224
}
210-
for _, r := range unexpectedLines {
211-
if r.MatchString(output.String()) {
212-
t.Errorf("output unexpectedly match regexp /%s/\n> output\n%s\n", r, output)
213-
return
214-
}
225+
226+
expectedLine := regexp.MustCompile(`#8 Blueberries are a good fruit \[blueberries\] - Merged`)
227+
if !expectedLine.MatchString(output.String()) {
228+
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", expectedLine, output)
229+
return
215230
}
216231
}
217232

test/fixtures/prStatusClosedMerged.json renamed to test/fixtures/prStatusCurrentBranchClosed.json

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"node": {
99
"number": 8,
1010
"title": "Blueberries are a good fruit",
11-
"state": "OPEN",
11+
"state": "CLOSED",
1212
"url": "https://github.com/cli/cli/pull/8",
1313
"headRefName": "blueberries",
1414
"reviewDecision": "CHANGES_REQUESTED",
@@ -35,27 +35,8 @@
3535
}
3636
},
3737
"viewerCreated": {
38-
"totalCount": 1,
39-
"edges": [
40-
{
41-
"node": {
42-
"number": 10,
43-
"state": "CLOSED",
44-
"title": "Strawberries are not actually berries",
45-
"url": "https://github.com/cli/cli/pull/10",
46-
"headRefName": "strawberries"
47-
}
48-
},
49-
{
50-
"node": {
51-
"number": 9,
52-
"state": "MERGED",
53-
"title": "Bananas are berries",
54-
"url": "https://github.com/cli/cli/pull/9",
55-
"headRefName": "banananana"
56-
}
57-
}
58-
]
38+
"totalCount": 0,
39+
"edges": []
5940
},
6041
"reviewRequested": {
6142
"totalCount": 0,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"data": {
3+
"repository": {
4+
"pullRequests": {
5+
"totalCount": 1,
6+
"edges": [
7+
{
8+
"node": {
9+
"number": 8,
10+
"title": "Blueberries are a good fruit",
11+
"state": "MERGED",
12+
"url": "https://github.com/cli/cli/pull/8",
13+
"headRefName": "blueberries",
14+
"reviewDecision": "CHANGES_REQUESTED",
15+
"commits": {
16+
"nodes": [
17+
{
18+
"commit": {
19+
"statusCheckRollup": {
20+
"contexts": {
21+
"nodes": [
22+
{
23+
"state": "SUCCESS"
24+
}
25+
]
26+
}
27+
}
28+
}
29+
}
30+
]
31+
}
32+
}
33+
}
34+
]
35+
}
36+
},
37+
"viewerCreated": {
38+
"totalCount": 0,
39+
"edges": []
40+
},
41+
"reviewRequested": {
42+
"totalCount": 0,
43+
"edges": []
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)