Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Feb 15, 2022
1 parent 6bc0b69 commit 7234007
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 96 deletions.
14 changes: 1 addition & 13 deletions pkg/cmd/release/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,6 @@ func createRun(opts *CreateOptions) error {
}
}

if opts.Draft && len(opts.DiscussionCategory) > 0 {
return fmt.Errorf(
"%s Discussions not supported with draft releases",
opts.IO.ColorScheme().FailureIcon(),
)
}

params := map[string]interface{}{
"tag_name": opts.TagName,
"draft": opts.Draft,
Expand Down Expand Up @@ -418,12 +411,7 @@ func createRun(opts *CreateOptions) error {
}

if !opts.Draft {
params := map[string]interface{}{}
params["draft"] = false
if opts.DiscussionCategory != "" {
params["discussion_category_name"] = opts.DiscussionCategory
}
rel, err := publishRelease(httpClient, newRelease.APIURL, params)
rel, err := publishRelease(httpClient, newRelease.APIURL, opts.DiscussionCategory)
if err != nil {
return err
}
Expand Down
245 changes: 163 additions & 82 deletions pkg/cmd/release/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func Test_createRun(t *testing.T) {
name string
isTTY bool
opts CreateOptions
wantParams interface{}
httpStubs func(t *testing.T, reg *httpmock.Registry)
wantErr string
wantStdout string
wantStderr string
Expand All @@ -301,12 +301,20 @@ func Test_createRun(t *testing.T) {
BodyProvided: true,
Target: "",
},
wantParams: map[string]interface{}{
"tag_name": "v1.2.3",
"name": "The Big 1.2",
"body": "* Fixed bugs",
"draft": false,
"prerelease": false,
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
"url": "https://api.github.com/releases/123",
"upload_url": "https://api.github.com/assets/upload",
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
}`, func(params map[string]interface{}) {
assert.Equal(t, map[string]interface{}{
"tag_name": "v1.2.3",
"name": "The Big 1.2",
"body": "* Fixed bugs",
"draft": false,
"prerelease": false,
}, params)
}))
},
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
wantStderr: ``,
Expand All @@ -322,13 +330,21 @@ func Test_createRun(t *testing.T) {
Target: "",
DiscussionCategory: "General",
},
wantParams: map[string]interface{}{
"tag_name": "v1.2.3",
"name": "The Big 1.2",
"body": "* Fixed bugs",
"draft": false,
"prerelease": false,
"discussion_category_name": "General",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
"url": "https://api.github.com/releases/123",
"upload_url": "https://api.github.com/assets/upload",
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
}`, func(params map[string]interface{}) {
assert.Equal(t, map[string]interface{}{
"tag_name": "v1.2.3",
"name": "The Big 1.2",
"body": "* Fixed bugs",
"draft": false,
"prerelease": false,
"discussion_category_name": "General",
}, params)
}))
},
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
wantStderr: ``,
Expand All @@ -343,11 +359,19 @@ func Test_createRun(t *testing.T) {
BodyProvided: true,
Target: "main",
},
wantParams: map[string]interface{}{
"tag_name": "v1.2.3",
"draft": false,
"prerelease": false,
"target_commitish": "main",
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
"url": "https://api.github.com/releases/123",
"upload_url": "https://api.github.com/assets/upload",
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
}`, func(params map[string]interface{}) {
assert.Equal(t, map[string]interface{}{
"tag_name": "v1.2.3",
"draft": false,
"prerelease": false,
"target_commitish": "main",
}, params)
}))
},
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
wantStderr: ``,
Expand All @@ -363,35 +387,22 @@ func Test_createRun(t *testing.T) {
Draft: true,
Target: "",
},
wantParams: map[string]interface{}{
"tag_name": "v1.2.3",
"draft": true,
"prerelease": false,
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
"url": "https://api.github.com/releases/123",
"upload_url": "https://api.github.com/assets/upload",
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
}`, func(params map[string]interface{}) {
assert.Equal(t, map[string]interface{}{
"tag_name": "v1.2.3",
"draft": true,
"prerelease": false,
}, params)
}))
},
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
wantStderr: ``,
},
{
name: "discussion category for draft release",
isTTY: true,
opts: CreateOptions{
TagName: "v1.2.3",
Name: "",
Body: "",
BodyProvided: true,
Draft: true,
Target: "",
DiscussionCategory: "general",
},
wantParams: map[string]interface{}{
"tag_name": "v1.2.3",
"draft": true,
"prerelease": false,
"discussion_category_name": "general",
},
wantErr: "X Discussions not supported with draft releases",
wantStdout: "",
},
{
name: "with generate notes",
isTTY: true,
Expand All @@ -403,11 +414,19 @@ func Test_createRun(t *testing.T) {
BodyProvided: true,
GenerateNotes: true,
},
wantParams: map[string]interface{}{
"tag_name": "v1.2.3",
"draft": false,
"prerelease": false,
"generate_release_notes": true,
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
"url": "https://api.github.com/releases/123",
"upload_url": "https://api.github.com/assets/upload",
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
}`, func(params map[string]interface{}) {
assert.Equal(t, map[string]interface{}{
"tag_name": "v1.2.3",
"draft": false,
"prerelease": false,
"generate_release_notes": true,
}, params)
}))
},
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
wantErr: "",
Expand All @@ -432,10 +451,97 @@ func Test_createRun(t *testing.T) {
},
Concurrency: 1,
},
wantParams: map[string]interface{}{
"tag_name": "v1.2.3",
"draft": true,
"prerelease": false,
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
"url": "https://api.github.com/releases/123",
"upload_url": "https://api.github.com/assets/upload",
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
}`, func(params map[string]interface{}) {
assert.Equal(t, map[string]interface{}{
"tag_name": "v1.2.3",
"draft": true,
"prerelease": false,
}, params)
}))
reg.Register(httpmock.REST("POST", "assets/upload"), func(req *http.Request) (*http.Response, error) {
q := req.URL.Query()
assert.Equal(t, "ball.tgz", q.Get("name"))
assert.Equal(t, "", q.Get("label"))
return &http.Response{
StatusCode: 201,
Request: req,
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`)),
Header: map[string][]string{
"Content-Type": {"application/json"},
},
}, nil
})
reg.Register(httpmock.REST("PATCH", "releases/123"), httpmock.RESTPayload(201, `{
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final"
}`, func(params map[string]interface{}) {
assert.Equal(t, map[string]interface{}{
"draft": false,
}, params)
}))
},
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final\n",
wantStderr: ``,
},
{
name: "upload files and create discussion",
isTTY: true,
opts: CreateOptions{
TagName: "v1.2.3",
Name: "",
Body: "",
BodyProvided: true,
Draft: false,
Target: "",
Assets: []*shared.AssetForUpload{
{
Name: "ball.tgz",
Open: func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewBufferString(`TARBALL`)), nil
},
},
},
DiscussionCategory: "general",
Concurrency: 1,
},
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(201, `{
"url": "https://api.github.com/releases/123",
"upload_url": "https://api.github.com/assets/upload",
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
}`, func(params map[string]interface{}) {
assert.Equal(t, map[string]interface{}{
"tag_name": "v1.2.3",
"draft": true,
"prerelease": false,
"discussion_category_name": "general",
}, params)
}))
reg.Register(httpmock.REST("POST", "assets/upload"), func(req *http.Request) (*http.Response, error) {
q := req.URL.Query()
assert.Equal(t, "ball.tgz", q.Get("name"))
assert.Equal(t, "", q.Get("label"))
return &http.Response{
StatusCode: 201,
Request: req,
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`)),
Header: map[string][]string{
"Content-Type": {"application/json"},
},
}, nil
})
reg.Register(httpmock.REST("PATCH", "releases/123"), httpmock.RESTPayload(201, `{
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final"
}`, func(params map[string]interface{}) {
assert.Equal(t, map[string]interface{}{
"draft": false,
"discussion_category_name": "general",
}, params)
}))
},
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final\n",
wantStderr: ``,
Expand All @@ -449,15 +555,10 @@ func Test_createRun(t *testing.T) {
io.SetStderrTTY(tt.isTTY)

fakeHTTP := &httpmock.Registry{}
fakeHTTP.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.StatusStringResponse(201, `{
"url": "https://api.github.com/releases/123",
"upload_url": "https://api.github.com/assets/upload",
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3"
}`))
fakeHTTP.Register(httpmock.REST("POST", "assets/upload"), httpmock.StatusStringResponse(201, `{}`))
fakeHTTP.Register(httpmock.REST("PATCH", "releases/123"), httpmock.StatusStringResponse(201, `{
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final"
}`))
if tt.httpStubs != nil {
tt.httpStubs(t, fakeHTTP)
}
defer fakeHTTP.Verify(t)

tt.opts.IO = io
tt.opts.HttpClient = func() (*http.Client, error) {
Expand All @@ -475,26 +576,6 @@ func Test_createRun(t *testing.T) {
require.NoError(t, err)
}

bb, err := ioutil.ReadAll(fakeHTTP.Requests[0].Body)
require.NoError(t, err)
var params interface{}
err = json.Unmarshal(bb, &params)
require.NoError(t, err)
assert.Equal(t, tt.wantParams, params)

if len(tt.opts.Assets) > 0 {
q := fakeHTTP.Requests[1].URL.Query()
assert.Equal(t, tt.opts.Assets[0].Name, q.Get("name"))
assert.Equal(t, tt.opts.Assets[0].Label, q.Get("label"))

bb, err := ioutil.ReadAll(fakeHTTP.Requests[2].Body)
require.NoError(t, err)
var updateParams interface{}
err = json.Unmarshal(bb, &updateParams)
require.NoError(t, err)
assert.Equal(t, map[string]interface{}{"draft": false}, updateParams)
}

assert.Equal(t, tt.wantStdout, stdout.String())
assert.Equal(t, tt.wantStderr, stderr.String())
})
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/release/create/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ func createRelease(httpClient *http.Client, repo ghrepo.Interface, params map[st
return &newRelease, err
}

func publishRelease(httpClient *http.Client, releaseURL string, params map[string]interface{}) (*shared.Release, error) {
func publishRelease(httpClient *http.Client, releaseURL string, discussionCategory string) (*shared.Release, error) {
params := map[string]interface{}{"draft": false}
if discussionCategory != "" {
params["discussion_category_name"] = discussionCategory
}

bodyBytes, err := json.Marshal(params)
if err != nil {
return nil, err
Expand Down

0 comments on commit 7234007

Please sign in to comment.