Skip to content

Commit 09157f1

Browse files
author
Daniel Foad
committed
unit tests
1 parent c62dd55 commit 09157f1

File tree

3 files changed

+96
-4
lines changed

3 files changed

+96
-4
lines changed

api/fake_http.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ func (f *FakeHTTP) StubRepoResponseWithPermission(owner, repo, permission string
7070
f.responseStubs = append(f.responseStubs, resp)
7171
}
7272

73+
func (f *FakeHTTP) StubRepoResponseWithDefaultBranch(owner, repo, defaultBranch string) {
74+
body := bytes.NewBufferString(fmt.Sprintf(`
75+
{ "data": { "repo_000": {
76+
"id": "REPOID",
77+
"name": "%s",
78+
"owner": {"login": "%s"},
79+
"defaultBranchRef": {
80+
"name": "%s"
81+
},
82+
"viewerPermission": "READ"
83+
} } }
84+
`, repo, owner, defaultBranch))
85+
resp := &http.Response{
86+
StatusCode: 200,
87+
Body: ioutil.NopCloser(body),
88+
}
89+
f.responseStubs = append(f.responseStubs, resp)
90+
}
91+
7392
func (f *FakeHTTP) StubForkedRepoResponse(forkFullName, parentFullName string) {
7493
forkRepo := strings.SplitN(forkFullName, "/", 2)
7594
parentRepo := strings.SplitN(parentFullName, "/", 2)

command/pr.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ func prStatus(cmd *cobra.Command, args []string) error {
8383
return err
8484
}
8585

86-
baseRepoOverride, _ := cmd.Flags().GetString("repo")
87-
repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, baseRepoOverride)
86+
repoOverride, _ := cmd.Flags().GetString("repo")
87+
88+
remoteContext, err := context.ResolveRemotesToRepos(remotes, apiClient, repoOverride)
8889
if err != nil {
8990
return err
9091
}
@@ -94,7 +95,6 @@ func prStatus(cmd *cobra.Command, args []string) error {
9495
return err
9596
}
9697

97-
repoOverride, _ := cmd.Flags().GetString("repo")
9898
currentPRNumber, currentPRHeadRef, err := prSelectorForCurrentBranch(ctx, baseRepo)
9999
if err != nil && repoOverride == "" && err.Error() != "git: not on any branch" {
100100
return fmt.Errorf("could not query for pull request for current branch: %w", err)
@@ -114,7 +114,7 @@ func prStatus(cmd *cobra.Command, args []string) error {
114114
printHeader(out, "Current branch")
115115
currentPR := prPayload.CurrentPR
116116
currentBranch, _ := ctx.Branch()
117-
remoteRepo, _ := repoContext.BaseRepo()
117+
remoteRepo, _ := remoteContext.BaseRepo()
118118
noPRMessage := fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentPRHeadRef+"]"))
119119
if currentPR != nil {
120120
if remoteRepo.DefaultBranchRef.Name == currentBranch && currentPR.State != "OPEN" {

command/pr_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func TestPRStatus(t *testing.T) {
7777
initBlankContext("", "OWNER/REPO", "blueberries")
7878
http := initFakeHTTP()
7979
http.StubRepoResponse("OWNER", "REPO")
80+
http.StubRepoResponse("OWNER", "REPO")
8081

8182
jsonFile, _ := os.Open("../test/fixtures/prStatus.json")
8283
defer jsonFile.Close()
@@ -105,6 +106,7 @@ func TestPRStatus_fork(t *testing.T) {
105106
initBlankContext("", "OWNER/REPO", "blueberries")
106107
http := initFakeHTTP()
107108
http.StubForkedRepoResponse("OWNER/REPO", "PARENT/REPO")
109+
http.StubForkedRepoResponse("OWNER/REPO", "PARENT/REPO")
108110

109111
jsonFile, _ := os.Open("../test/fixtures/prStatusFork.json")
110112
defer jsonFile.Close()
@@ -135,6 +137,7 @@ func TestPRStatus_reviewsAndChecks(t *testing.T) {
135137
initBlankContext("", "OWNER/REPO", "blueberries")
136138
http := initFakeHTTP()
137139
http.StubRepoResponse("OWNER", "REPO")
140+
http.StubRepoResponse("OWNER", "REPO")
138141

139142
jsonFile, _ := os.Open("../test/fixtures/prStatusChecks.json")
140143
defer jsonFile.Close()
@@ -162,6 +165,7 @@ func TestPRStatus_currentBranch_showTheMostRecentPR(t *testing.T) {
162165
initBlankContext("", "OWNER/REPO", "blueberries")
163166
http := initFakeHTTP()
164167
http.StubRepoResponse("OWNER", "REPO")
168+
http.StubRepoResponse("OWNER", "REPO")
165169

166170
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranch.json")
167171
defer jsonFile.Close()
@@ -190,10 +194,33 @@ func TestPRStatus_currentBranch_showTheMostRecentPR(t *testing.T) {
190194
}
191195
}
192196

197+
func TestPRStatus_currentBranch_defaultBranch(t *testing.T) {
198+
initBlankContext("", "OWNER/REPO", "blueberries")
199+
http := initFakeHTTP()
200+
http.StubRepoResponseWithDefaultBranch("OWNER", "REPO", "blueberries")
201+
http.StubRepoResponse("OWNER", "REPO")
202+
203+
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranch.json")
204+
defer jsonFile.Close()
205+
http.StubResponse(200, jsonFile)
206+
207+
output, err := RunCommand(prStatusCmd, "pr status")
208+
if err != nil {
209+
t.Errorf("error running command `pr status`: %v", err)
210+
}
211+
212+
expectedLine := regexp.MustCompile(`#10 Blueberries are certainly a good fruit \[blueberries\]`)
213+
if !expectedLine.MatchString(output.String()) {
214+
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", expectedLine, output)
215+
return
216+
}
217+
}
218+
193219
func TestPRStatus_currentBranch_Closed(t *testing.T) {
194220
initBlankContext("", "OWNER/REPO", "blueberries")
195221
http := initFakeHTTP()
196222
http.StubRepoResponse("OWNER", "REPO")
223+
http.StubRepoResponse("OWNER", "REPO")
197224

198225
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranchClosed.json")
199226
defer jsonFile.Close()
@@ -211,10 +238,33 @@ func TestPRStatus_currentBranch_Closed(t *testing.T) {
211238
}
212239
}
213240

241+
func TestPRStatus_currentBranch_Closed_defaultBranch(t *testing.T) {
242+
initBlankContext("", "OWNER/REPO", "blueberries")
243+
http := initFakeHTTP()
244+
http.StubRepoResponseWithDefaultBranch("OWNER", "REPO", "blueberries")
245+
http.StubRepoResponse("OWNER", "REPO")
246+
247+
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranchClosed.json")
248+
defer jsonFile.Close()
249+
http.StubResponse(200, jsonFile)
250+
251+
output, err := RunCommand(prStatusCmd, "pr status")
252+
if err != nil {
253+
t.Errorf("error running command `pr status`: %v", err)
254+
}
255+
256+
expectedLine := regexp.MustCompile(`There is no pull request associated with \[blueberries\]`)
257+
if !expectedLine.MatchString(output.String()) {
258+
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", expectedLine, output)
259+
return
260+
}
261+
}
262+
214263
func TestPRStatus_currentBranch_Merged(t *testing.T) {
215264
initBlankContext("", "OWNER/REPO", "blueberries")
216265
http := initFakeHTTP()
217266
http.StubRepoResponse("OWNER", "REPO")
267+
http.StubRepoResponse("OWNER", "REPO")
218268

219269
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranchMerged.json")
220270
defer jsonFile.Close()
@@ -232,10 +282,33 @@ func TestPRStatus_currentBranch_Merged(t *testing.T) {
232282
}
233283
}
234284

285+
func TestPRStatus_currentBranch_Merged_defaultBranch(t *testing.T) {
286+
initBlankContext("", "OWNER/REPO", "blueberries")
287+
http := initFakeHTTP()
288+
http.StubRepoResponseWithDefaultBranch("OWNER", "REPO", "blueberries")
289+
http.StubRepoResponse("OWNER", "REPO")
290+
291+
jsonFile, _ := os.Open("../test/fixtures/prStatusCurrentBranchMerged.json")
292+
defer jsonFile.Close()
293+
http.StubResponse(200, jsonFile)
294+
295+
output, err := RunCommand(prStatusCmd, "pr status")
296+
if err != nil {
297+
t.Errorf("error running command `pr status`: %v", err)
298+
}
299+
300+
expectedLine := regexp.MustCompile(`There is no pull request associated with \[blueberries\]`)
301+
if !expectedLine.MatchString(output.String()) {
302+
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", expectedLine, output)
303+
return
304+
}
305+
}
306+
235307
func TestPRStatus_blankSlate(t *testing.T) {
236308
initBlankContext("", "OWNER/REPO", "blueberries")
237309
http := initFakeHTTP()
238310
http.StubRepoResponse("OWNER", "REPO")
311+
http.StubRepoResponse("OWNER", "REPO")
239312

240313
http.StubResponse(200, bytes.NewBufferString(`
241314
{ "data": {} }

0 commit comments

Comments
 (0)