Skip to content

Commit df21025

Browse files
committed
Add tests for release create from commit log, annotated tag
1 parent 7090922 commit df21025

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

pkg/cmd/release/create/create_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/cli/cli/v2/internal/config"
1515
"github.com/cli/cli/v2/internal/ghrepo"
16+
"github.com/cli/cli/v2/internal/run"
1617
"github.com/cli/cli/v2/pkg/cmd/release/shared"
1718
"github.com/cli/cli/v2/pkg/cmdutil"
1819
"github.com/cli/cli/v2/pkg/httpmock"
@@ -506,6 +507,7 @@ func Test_createRun_interactive(t *testing.T) {
506507
name string
507508
httpStubs func(*httpmock.Registry)
508509
askStubs func(*prompt.AskStubber)
510+
runStubs func(*run.CommandStubber)
509511
opts *CreateOptions
510512
wantParams map[string]interface{}
511513
wantOut string
@@ -526,6 +528,9 @@ func Test_createRun_interactive(t *testing.T) {
526528
as.StubPrompt("Submit?").
527529
AssertOptions([]string{"Publish release", "Save as draft", "Cancel"}).AnswerWith("Publish release")
528530
},
531+
runStubs: func(rs *run.CommandStubber) {
532+
rs.Register(`git tag --list`, 1, "")
533+
},
529534
httpStubs: func(reg *httpmock.Registry) {
530535
reg.Register(httpmock.REST("GET", "repos/OWNER/REPO/tags"), httpmock.StatusStringResponse(200, `[
531536
{ "name": "v1.2.3" }, { "name": "v1.2.2" }, { "name": "v1.0.0" }, { "name": "v0.1.2" }
@@ -556,6 +561,9 @@ func Test_createRun_interactive(t *testing.T) {
556561
as.StubPrompt("Is this a prerelease?").AnswerWith(false)
557562
as.StubPrompt("Submit?").AnswerWith("Publish release")
558563
},
564+
runStubs: func(rs *run.CommandStubber) {
565+
rs.Register(`git tag --list`, 1, "")
566+
},
559567
httpStubs: func(reg *httpmock.Registry) {
560568
reg.Register(httpmock.REST("GET", "repos/OWNER/REPO/tags"), httpmock.StatusStringResponse(200, `[
561569
{ "name": "v1.2.2" }, { "name": "v1.0.0" }, { "name": "v0.1.2" }
@@ -586,6 +594,9 @@ func Test_createRun_interactive(t *testing.T) {
586594
as.StubPrompt("Is this a prerelease?").AnswerWith(false)
587595
as.StubPrompt("Submit?").AnswerWith("Publish release")
588596
},
597+
runStubs: func(rs *run.CommandStubber) {
598+
rs.Register(`git tag --list`, 1, "")
599+
},
589600
httpStubs: func(reg *httpmock.Registry) {
590601
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases/generate-notes"),
591602
httpmock.StatusStringResponse(200, `{
@@ -608,6 +619,77 @@ func Test_createRun_interactive(t *testing.T) {
608619
},
609620
wantOut: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
610621
},
622+
{
623+
name: "create a release using commit log as notes",
624+
opts: &CreateOptions{
625+
TagName: "v1.2.3",
626+
},
627+
askStubs: func(as *prompt.AskStubber) {
628+
as.StubPrompt("Title (optional)").AnswerDefault()
629+
as.StubPrompt("Release notes").
630+
AssertOptions([]string{"Write my own", "Write using commit log as template", "Leave blank"}).
631+
AnswerWith("Write using commit log as template")
632+
as.StubPrompt("Is this a prerelease?").AnswerWith(false)
633+
as.StubPrompt("Submit?").AnswerWith("Publish release")
634+
},
635+
runStubs: func(rs *run.CommandStubber) {
636+
rs.Register(`git tag --list`, 1, "")
637+
rs.Register(`git describe --tags --abbrev=0 HEAD\^`, 0, "v1.2.2\n")
638+
rs.Register(`git .+log .+v1\.2\.2\.\.HEAD$`, 0, "commit subject\n\ncommit body\n")
639+
},
640+
httpStubs: func(reg *httpmock.Registry) {
641+
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases/generate-notes"),
642+
httpmock.StatusStringResponse(404, `{}`))
643+
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"),
644+
httpmock.StatusStringResponse(201, `{
645+
"url": "https://api.github.com/releases/123",
646+
"upload_url": "https://api.github.com/assets/upload",
647+
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
648+
}`))
649+
},
650+
wantParams: map[string]interface{}{
651+
"body": "* commit subject\n\n commit body\n ",
652+
"draft": false,
653+
"prerelease": false,
654+
"tag_name": "v1.2.3",
655+
},
656+
wantOut: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
657+
},
658+
{
659+
name: "create using annotated tag as notes",
660+
opts: &CreateOptions{
661+
TagName: "v1.2.3",
662+
},
663+
askStubs: func(as *prompt.AskStubber) {
664+
as.StubPrompt("Title (optional)").AnswerDefault()
665+
as.StubPrompt("Release notes").
666+
AssertOptions([]string{"Write my own", "Write using git tag message as template", "Leave blank"}).
667+
AnswerWith("Write using git tag message as template")
668+
as.StubPrompt("Is this a prerelease?").AnswerWith(false)
669+
as.StubPrompt("Submit?").AnswerWith("Publish release")
670+
},
671+
runStubs: func(rs *run.CommandStubber) {
672+
rs.Register(`git tag --list`, 0, "hello from annotated tag")
673+
rs.Register(`git describe --tags --abbrev=0 v1\.2\.3\^`, 1, "")
674+
},
675+
httpStubs: func(reg *httpmock.Registry) {
676+
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases/generate-notes"),
677+
httpmock.StatusStringResponse(404, `{}`))
678+
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"),
679+
httpmock.StatusStringResponse(201, `{
680+
"url": "https://api.github.com/releases/123",
681+
"upload_url": "https://api.github.com/assets/upload",
682+
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
683+
}`))
684+
},
685+
wantParams: map[string]interface{}{
686+
"body": "hello from annotated tag",
687+
"draft": false,
688+
"prerelease": false,
689+
"tag_name": "v1.2.3",
690+
},
691+
wantOut: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
692+
},
611693
}
612694
for _, tt := range tests {
613695
ios, _, stdout, stderr := iostreams.Test()
@@ -641,6 +723,12 @@ func Test_createRun_interactive(t *testing.T) {
641723
tt.askStubs(as)
642724
}
643725

726+
rs, teardown := run.Stub()
727+
defer teardown(t)
728+
if tt.runStubs != nil {
729+
tt.runStubs(rs)
730+
}
731+
644732
err := createRun(tt.opts)
645733

646734
if tt.wantErr != "" {

0 commit comments

Comments
 (0)