test(internal/releaser/github): add test for CreateRelease()

This commit is contained in:
fwiedmann
2019-08-08 20:07:22 +02:00
parent 3c500142aa
commit 0037abfaf7

View File

@@ -7,6 +7,8 @@ import (
"os" "os"
"testing" "testing"
log "github.com/sirupsen/logrus"
"github.com/Masterminds/semver" "github.com/Masterminds/semver"
"github.com/Nightapes/go-semantic-release/internal/releaser/github" "github.com/Nightapes/go-semantic-release/internal/releaser/github"
@@ -15,12 +17,12 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
type testDouble struct { type testHelperMethodStruct struct {
config config.GitHubProvider config config.GitHubProvider
valid bool valid bool
} }
type testFourth struct { type testReleaseStruct struct {
config config.GitHubProvider config config.GitHubProvider
releaseVersion *shared.ReleaseVersion releaseVersion *shared.ReleaseVersion
generatedChangelog *shared.GeneratedChangelog generatedChangelog *shared.GeneratedChangelog
@@ -29,15 +31,15 @@ type testFourth struct {
valid bool valid bool
} }
var doublesNew = []testDouble{ var testNewClient = []testHelperMethodStruct{
testDouble{config: config.GitHubProvider{ testHelperMethodStruct{config: config.GitHubProvider{
Repo: "foo", Repo: "foo",
User: "bar", User: "bar",
}, },
valid: true, valid: true,
}, },
testDouble{config: config.GitHubProvider{ testHelperMethodStruct{config: config.GitHubProvider{
Repo: "foo", Repo: "foo",
User: "bar", User: "bar",
CustomURL: "https://test.com", CustomURL: "https://test.com",
@@ -46,22 +48,22 @@ var doublesNew = []testDouble{
}, },
} }
var doublesValidateConfig = []testDouble{ var testHelperMethod = []testHelperMethodStruct{
testDouble{config: config.GitHubProvider{ testHelperMethodStruct{config: config.GitHubProvider{
Repo: "foo", Repo: "foo",
User: "bar", User: "bar",
}, },
valid: true, valid: true,
}, },
testDouble{config: config.GitHubProvider{ testHelperMethodStruct{config: config.GitHubProvider{
Repo: "", Repo: "",
User: "bar", User: "bar",
}, },
valid: false, valid: false,
}, },
testDouble{config: config.GitHubProvider{ testHelperMethodStruct{config: config.GitHubProvider{
Repo: "foo", Repo: "foo",
User: "", User: "",
}, },
@@ -72,8 +74,8 @@ var doublesValidateConfig = []testDouble{
var lastVersion, _ = semver.NewVersion("1.0.0") var lastVersion, _ = semver.NewVersion("1.0.0")
var newVersion, _ = semver.NewVersion("2.0.0") var newVersion, _ = semver.NewVersion("2.0.0")
var fourthsReleas = []testFourth{ var testReleases = []testReleaseStruct{
testFourth{ testReleaseStruct{
config: config.GitHubProvider{ config: config.GitHubProvider{
Repo: "foo", Repo: "foo",
User: "bar", User: "bar",
@@ -94,11 +96,10 @@ var fourthsReleas = []testFourth{
Title: "title", Title: "title",
Content: "content", Content: "content",
}, },
requestResponseBody: "{ \"url\": \"https://api.github.com/repos/octocat/Hello-World/releases/1\", \"html_url\": \"https://github.com/octocat/Hello-World/releases/v1.0.0\", \"assets_url\": \"https://api.github.com/repos/octocat/Hello-World/releases/1/assets\", \"upload_url\": \"https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}\", \"tarball_url\": \"https://api.github.com/repos/octocat/Hello-World/tarball/v1.0.0\", \"zipball_url\": \"https://api.github.com/repos/octocat/Hello-World/zipball/v1.0.0\", \"id\": 1, \"node_id\": \"MDc6UmVsZWFzZTE=\", \"tag_name\": \"v1.0.0\", \"target_commitish\": \"master\", \"name\": \"v1.0.0\", \"body\": \"Description of the release\", \"draft\": false, \"prerelease\": false, \"created_at\": \"2013-02-27T19:35:32Z\", \"published_at\": \"2013-02-27T19:35:32Z\", \"author\": { \"login\": \"octocat\", \"id\": 1, \"node_id\": \"MDQ6VXNlcjE=\", \"avatar_url\": \"https://github.com/images/error/octocat_happy.gif\", \"gravatar_id\": \"\", \"url\": \"https://api.github.com/users/octocat\", \"html_url\": \"https://github.com/octocat\", \"followers_url\": \"https://api.github.com/users/octocat/followers\", \"following_url\": \"https://api.github.com/users/octocat/following{/other_user}\", \"gists_url\": \"https://api.github.com/users/octocat/gists{/gist_id}\", \"starred_url\": \"https://api.github.com/users/octocat/starred{/owner}{/repo}\", \"subscriptions_url\": \"https://api.github.com/users/octocat/subscriptions\", \"organizations_url\": \"https://api.github.com/users/octocat/orgs\", \"repos_url\": \"https://api.github.com/users/octocat/repos\", \"events_url\": \"https://api.github.com/users/octocat/events{/privacy}\", \"received_events_url\": \"https://api.github.com/users/octocat/received_events\", \"type\": \"User\", \"site_admin\": false }, \"assets\": [ ]}", requestResponseCode: 200,
requestResponseCode: 500,
valid: true, valid: true,
}, },
testFourth{ testReleaseStruct{
config: config.GitHubProvider{ config: config.GitHubProvider{
Repo: "foo", Repo: "foo",
User: "bar", User: "bar",
@@ -119,8 +120,7 @@ var fourthsReleas = []testFourth{
Title: "title", Title: "title",
Content: "content", Content: "content",
}, },
requestResponseBody: "", requestResponseCode: 400,
requestResponseCode: 422,
valid: false, valid: false,
}, },
} }
@@ -128,16 +128,17 @@ var fourthsReleas = []testFourth{
func initHTTPServer(respCode int, body string) *httptest.Server { func initHTTPServer(respCode int, body string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
log.Infof("Got request with method: %s, URL: %s", req.Method, req.URL)
rw.Write([]byte(body)) log.Infof("Will response with statuscode: %d, body: %s", respCode, body)
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(respCode) rw.WriteHeader(respCode)
rw.Header().Set("Content-Type", "application/json")
rw.Write([]byte(body))
})) }))
} }
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
for _, testOject := range doublesNew { for _, testOject := range testNewClient {
if testOject.valid { if testOject.valid {
os.Setenv("GITHUB_ACCESS_TOKEN", "XXX") os.Setenv("GITHUB_ACCESS_TOKEN", "XXX")
} }
@@ -152,7 +153,7 @@ func TestNew(t *testing.T) {
func TestGetCommitURL(t *testing.T) { func TestGetCommitURL(t *testing.T) {
os.Setenv("GITHUB_ACCESS_TOKEN", "XX") os.Setenv("GITHUB_ACCESS_TOKEN", "XX")
for _, testOject := range doublesNew { for _, testOject := range testNewClient {
client, _ := github.New(&testOject.config) client, _ := github.New(&testOject.config)
actualUrl := client.GetCommitURL() actualUrl := client.GetCommitURL()
if testOject.config.CustomURL != "" { if testOject.config.CustomURL != "" {
@@ -170,7 +171,7 @@ func TestGetCommitURL(t *testing.T) {
func TestGetCompareURL(t *testing.T) { func TestGetCompareURL(t *testing.T) {
os.Setenv("GITHUB_ACCESS_TOKEN", "XX") os.Setenv("GITHUB_ACCESS_TOKEN", "XX")
for _, testOject := range doublesNew { for _, testOject := range testNewClient {
client, _ := github.New(&testOject.config) client, _ := github.New(&testOject.config)
actualUrl := client.GetCompareURL("1", "2") actualUrl := client.GetCompareURL("1", "2")
if testOject.config.CustomURL != "" { if testOject.config.CustomURL != "" {
@@ -188,7 +189,7 @@ func TestGetCompareURL(t *testing.T) {
func TestValidateConfig(t *testing.T) { func TestValidateConfig(t *testing.T) {
os.Setenv("GITHUB_ACCESS_TOKEN", "XX") os.Setenv("GITHUB_ACCESS_TOKEN", "XX")
for _, testOject := range doublesValidateConfig { for _, testOject := range testHelperMethod {
client, _ := github.New(&testOject.config) client, _ := github.New(&testOject.config)
err := client.ValidateConfig() err := client.ValidateConfig()
@@ -201,24 +202,25 @@ func TestValidateConfig(t *testing.T) {
func TestCreateRelease(t *testing.T) { func TestCreateRelease(t *testing.T) {
os.Setenv("GITHUB_ACCESS_TOKEN", "XX") os.Setenv("GITHUB_ACCESS_TOKEN", "XX")
for _, testObejct := range fourthsReleas { for _, testObejct := range testReleases {
if testObejct.valid { if testObejct.valid {
server := initHTTPServer(testObejct.requestResponseCode, "") server := initHTTPServer(testObejct.requestResponseCode, "")
testObejct.config.CustomURL = server.URL testObejct.config.CustomURL = server.URL
client, _ := github.New(&testObejct.config) client, _ := github.New(&testObejct.config)
err := client.CreateRelease(testObejct.releaseVersion, testObejct.generatedChangelog)
assert.Equal(t, testObejct.valid, err == nil)
} else {
testObejct.config.CustomURL = "foo"
client, _ := github.New(&testObejct.config)
err := client.CreateRelease(testObejct.releaseVersion, testObejct.generatedChangelog) err := client.CreateRelease(testObejct.releaseVersion, testObejct.generatedChangelog)
if err != nil { if err != nil {
t.Log(err) t.Log(err)
} }
assert.Equal(t, testObejct.valid, err == nil) assert.NoError(t, err)
server.Close()
} else {
testObejct.config.CustomURL = "http://foo"
client, _ := github.New(&testObejct.config)
err := client.CreateRelease(testObejct.releaseVersion, testObejct.generatedChangelog)
t.Log(err)
assert.Error(t, err)
} }
} }
os.Unsetenv("GITHUB_ACCESS_TOKEN") os.Unsetenv("GITHUB_ACCESS_TOKEN")