X Tutup
Skip to content

Commit 1b78403

Browse files
committed
actually add tests for computing defaults
1 parent fba4f4d commit 1b78403

File tree

1 file changed

+91
-18
lines changed

1 file changed

+91
-18
lines changed

command/pr_create_test.go

Lines changed: 91 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -270,22 +270,7 @@ func (as *askStubber) StubWithDefaults() {
270270
as.Stubs = append(as.Stubs, nil)
271271
}
272272

273-
/*
274-
there are going to be calls to:
275-
- git status
276-
- git push
277-
- git rev-parse
278-
- git log
279-
280-
I can handle all that with the new CmdStubber.
281-
282-
For survey, there is going to be:
283-
- potentially template select Ask
284-
- title, body Ask
285-
- Confirm Action Ask
286-
287-
*/
288-
func TestPRCreate_survey_preview_defaults(t *testing.T) {
273+
func TestPRCreate_survey_defaults_multicommit(t *testing.T) {
289274
initBlankContext("OWNER/REPO", "feature")
290275
http := initFakeHTTP()
291276
http.StubRepoResponse("OWNER", "REPO")
@@ -345,11 +330,99 @@ func TestPRCreate_survey_preview_defaults(t *testing.T) {
345330
}{}
346331
json.Unmarshal(bodyBytes, &reqBody)
347332

333+
expectedBody := "---\n2 commits:\n\n- 12345 commit 0\n- 23456 commit 1\n"
334+
348335
eq(t, reqBody.Variables.Input.RepositoryID, "REPOID")
349-
eq(t, reqBody.Variables.Input.Title, "my title")
350-
eq(t, reqBody.Variables.Input.Body, "my body lies")
336+
eq(t, reqBody.Variables.Input.Title, "feature")
337+
eq(t, reqBody.Variables.Input.Body, expectedBody)
351338
eq(t, reqBody.Variables.Input.BaseRefName, "master")
352339
eq(t, reqBody.Variables.Input.HeadRefName, "feature")
353340

354341
eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n")
355342
}
343+
344+
func TestPRCreate_survey_defaults_monocommit(t *testing.T) {
345+
initBlankContext("OWNER/REPO", "feature")
346+
http := initFakeHTTP()
347+
http.StubRepoResponse("OWNER", "REPO")
348+
http.StubResponse(200, bytes.NewBufferString(`
349+
{ "data": { "createPullRequest": { "pullRequest": {
350+
"URL": "https://github.com/OWNER/REPO/pull/12"
351+
} } } }
352+
`))
353+
354+
cs, cmdTeardown := initCmdStubber()
355+
defer cmdTeardown()
356+
357+
cs.Stub("") // git status
358+
cs.Stub("1234567890,the sky above the port") // git log
359+
cs.Stub("was the color of a television, turned to a dead channel") // git show
360+
cs.Stub("") // git rev-parse
361+
cs.Stub("") // git push
362+
363+
as, surveyTeardown := initAskStubber()
364+
defer surveyTeardown()
365+
366+
as.Stub([]*QuestionStub{
367+
&QuestionStub{
368+
Name: "title",
369+
Default: true,
370+
},
371+
&QuestionStub{
372+
Name: "body",
373+
Default: true,
374+
},
375+
})
376+
as.Stub([]*QuestionStub{
377+
&QuestionStub{
378+
Name: "confirmation",
379+
Value: 1,
380+
},
381+
})
382+
383+
output, err := RunCommand(prCreateCmd, `pr create`)
384+
eq(t, err, nil)
385+
386+
bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body)
387+
reqBody := struct {
388+
Variables struct {
389+
Input struct {
390+
RepositoryID string
391+
Title string
392+
Body string
393+
BaseRefName string
394+
HeadRefName string
395+
}
396+
}
397+
}{}
398+
json.Unmarshal(bodyBytes, &reqBody)
399+
400+
expectedBody := "was the color of a television, turned to a dead channel"
401+
402+
eq(t, reqBody.Variables.Input.RepositoryID, "REPOID")
403+
eq(t, reqBody.Variables.Input.Title, "the sky above the port")
404+
eq(t, reqBody.Variables.Input.Body, expectedBody)
405+
eq(t, reqBody.Variables.Input.BaseRefName, "master")
406+
eq(t, reqBody.Variables.Input.HeadRefName, "feature")
407+
408+
eq(t, output.String(), "https://github.com/OWNER/REPO/pull/12\n")
409+
}
410+
411+
func TestPRCreate_survey_defaults_no_changes(t *testing.T) {
412+
initBlankContext("OWNER/REPO", "feature")
413+
414+
http := initFakeHTTP()
415+
http.StubRepoResponse("OWNER", "REPO")
416+
417+
cs, cmdTeardown := initCmdStubber()
418+
defer cmdTeardown()
419+
420+
cs.Stub("") // git status
421+
cs.Stub("") // git log
422+
423+
_, err := RunCommand(prCreateCmd, `pr create`)
424+
if err == nil {
425+
t.Error("expected error")
426+
}
427+
eq(t, err.Error(), "could not compute title or body defaults: could not find any commits between master and feature")
428+
}

0 commit comments

Comments
 (0)
X Tutup