Skip to content

Commit 290355a

Browse files
cristiand391samcoe
authored andcommitted
Fix and add tests for merge commit's body
1 parent 7c9db69 commit 290355a

File tree

1 file changed

+156
-8
lines changed

1 file changed

+156
-8
lines changed

pkg/cmd/pr/merge/merge_test.go

Lines changed: 156 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,14 @@ func TestPRMerge_interactive(t *testing.T) {
626626
as, surveyTeardown := prompt.InitAskStubber()
627627
defer surveyTeardown()
628628

629-
as.StubOne(0) // Merge method survey
630-
as.StubOne(true) // Delete branch survey
631-
as.StubOne(true) // Confirm submit survey
629+
as.StubOne(0) // Merge method survey
630+
as.StubOne(true) // Delete branch survey
631+
as.Stub([]*prompt.QuestionStub{ // Confirm submit survey
632+
{
633+
Name: "confirmation",
634+
Value: 0,
635+
},
636+
})
632637

633638
output, err := runCommand(http, "blueberries", true, "")
634639
if err != nil {
@@ -682,8 +687,14 @@ func TestPRMerge_interactiveWithDeleteBranch(t *testing.T) {
682687
as, surveyTeardown := prompt.InitAskStubber()
683688
defer surveyTeardown()
684689

685-
as.StubOne(0) // Merge method survey
686-
as.StubOne(true) // Confirm submit survey
690+
as.StubOne(0) // Merge method survey
691+
as.StubOne(true) // Confirm submit survey
692+
as.Stub([]*prompt.QuestionStub{ // Confirm submit survey
693+
{
694+
Name: "confirmation",
695+
Value: 0,
696+
},
697+
})
687698

688699
output, err := runCommand(http, "blueberries", true, "-d")
689700
if err != nil {
@@ -694,6 +705,138 @@ func TestPRMerge_interactiveWithDeleteBranch(t *testing.T) {
694705
test.ExpectLines(t, output.Stderr(), "Merged pull request #3", "Deleted branch blueberries and switched to branch master")
695706
}
696707

708+
func TestPRMerge_interactiveSquashEditCommitMsg(t *testing.T) {
709+
http := initFakeHTTP()
710+
defer http.Verify(t)
711+
http.Register(
712+
httpmock.GraphQL(`query PullRequestForBranch\b`),
713+
httpmock.StringResponse(`
714+
{ "data": { "repository": { "pullRequests": { "nodes": [{
715+
"headRefName": "blueberries",
716+
"headRepositoryOwner": {"login": "OWNER"},
717+
"id": "THE-ID",
718+
"number": 3
719+
}] } } } }`))
720+
http.Register(
721+
httpmock.GraphQL(`query RepositoryInfo\b`),
722+
httpmock.StringResponse(`
723+
{ "data": { "repository": {
724+
"mergeCommitAllowed": true,
725+
"rebaseMergeAllowed": true,
726+
"squashMergeAllowed": true
727+
} } }`))
728+
http.Register(
729+
httpmock.GraphQL(`mutation PullRequestMerge\b`),
730+
httpmock.GraphQLMutation(`{}`, func(input map[string]interface{}) {
731+
assert.Equal(t, "THE-ID", input["pullRequestId"].(string))
732+
assert.Equal(t, "SQUASH", input["mergeMethod"].(string))
733+
assert.Equal(t, "cool story", input["commitBody"].(string))
734+
}))
735+
736+
cs, cmdTeardown := run.Stub()
737+
defer cmdTeardown(t)
738+
739+
cs.Register(`git config --get-regexp.+branch\\\.blueberries\\\.`, 0, "")
740+
741+
as, surveyTeardown := prompt.InitAskStubber()
742+
defer surveyTeardown()
743+
744+
as.StubOne(2) // Merge method survey
745+
as.StubOne(false) // Delete branch survey
746+
as.Stub([]*prompt.QuestionStub{ // Confirm submit survey
747+
{
748+
Name: "confirmation",
749+
Value: 1,
750+
},
751+
})
752+
as.Stub([]*prompt.QuestionStub{ // Edit Commit Msg editor survey
753+
{
754+
Name: "body",
755+
Value: "cool story",
756+
},
757+
})
758+
as.Stub([]*prompt.QuestionStub{ // Confirm submit survey
759+
{
760+
Name: "confirmation",
761+
Value: 0,
762+
},
763+
})
764+
765+
output, err := runCommand(http, "blueberries", true, "")
766+
if err != nil {
767+
t.Fatalf("Got unexpected error running `pr merge` %s", err)
768+
}
769+
770+
assert.Equal(t, "✔ Squashed and merged pull request #3 ()\n", output.Stderr())
771+
}
772+
773+
func TestPRMerge_interactiveSquashEditCommitMsgDefaultBody(t *testing.T) {
774+
http := initFakeHTTP()
775+
defer http.Verify(t)
776+
http.Register(
777+
httpmock.GraphQL(`query PullRequestForBranch\b`),
778+
httpmock.StringResponse(`
779+
{ "data": { "repository": { "pullRequests": { "nodes": [{
780+
"headRefName": "blueberries",
781+
"headRepositoryOwner": {"login": "OWNER"},
782+
"id": "THE-ID",
783+
"number": 3,
784+
"viewerMergeBodyText": "Co-authored by: Octocat <octocat@github.com>",
785+
"viewerMergeHeadlineText": "Add new feat (#33)"
786+
}] } } } }`))
787+
http.Register(
788+
httpmock.GraphQL(`query RepositoryInfo\b`),
789+
httpmock.StringResponse(`
790+
{ "data": { "repository": {
791+
"mergeCommitAllowed": true,
792+
"rebaseMergeAllowed": true,
793+
"squashMergeAllowed": true
794+
} } }`))
795+
http.Register(
796+
httpmock.GraphQL(`mutation PullRequestMerge\b`),
797+
httpmock.GraphQLMutation(`{}`, func(input map[string]interface{}) {
798+
assert.Equal(t, "THE-ID", input["pullRequestId"].(string))
799+
assert.Equal(t, "SQUASH", input["mergeMethod"].(string))
800+
assert.Equal(t, "Add new feat (#33)\n\nCo-authored by: Octocat <octocat@github.com>", input["commitBody"])
801+
}))
802+
803+
cs, cmdTeardown := run.Stub()
804+
defer cmdTeardown(t)
805+
806+
cs.Register(`git config --get-regexp.+branch\\\.blueberries\\\.`, 0, "")
807+
808+
as, surveyTeardown := prompt.InitAskStubber()
809+
defer surveyTeardown()
810+
811+
as.StubOne(2) // Merge method survey
812+
as.StubOne(false) // Delete branch survey
813+
as.Stub([]*prompt.QuestionStub{ // Confirm submit survey
814+
{
815+
Name: "confirmation",
816+
Value: 1,
817+
},
818+
})
819+
as.Stub([]*prompt.QuestionStub{ // Edit Commit Msg editor survey
820+
{
821+
Name: "body",
822+
Default: true,
823+
},
824+
})
825+
as.Stub([]*prompt.QuestionStub{ // Confirm submit survey
826+
{
827+
Name: "confirmation",
828+
Value: 0,
829+
},
830+
})
831+
832+
output, err := runCommand(http, "blueberries", true, "")
833+
if err != nil {
834+
t.Fatalf("Got unexpected error running `pr merge` %s", err)
835+
}
836+
837+
assert.Equal(t, "✔ Squashed and merged pull request #3 ()\n", output.Stderr())
838+
}
839+
697840
func TestPRMerge_interactiveCancelled(t *testing.T) {
698841
http := initFakeHTTP()
699842
defer http.Verify(t)
@@ -724,9 +867,14 @@ func TestPRMerge_interactiveCancelled(t *testing.T) {
724867
as, surveyTeardown := prompt.InitAskStubber()
725868
defer surveyTeardown()
726869

727-
as.StubOne(0) // Merge method survey
728-
as.StubOne(true) // Delete branch survey
729-
as.StubOne(false) // Confirm submit survey
870+
as.StubOne(0) // Merge method survey
871+
as.StubOne(true) // Delete branch survey
872+
as.Stub([]*prompt.QuestionStub{ // Confirm submit survey
873+
{
874+
Name: "confirmation",
875+
Value: 2,
876+
},
877+
})
730878

731879
output, err := runCommand(http, "blueberries", true, "")
732880
if !errors.Is(err, cmdutil.SilentError) {

0 commit comments

Comments
 (0)