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"
"testing"
log "github.com/sirupsen/logrus"
"github.com/Masterminds/semver"
"github.com/Nightapes/go-semantic-release/internal/releaser/github"
@@ -15,12 +17,12 @@ import (
"github.com/stretchr/testify/assert"
)
type testDouble struct {
type testHelperMethodStruct struct {
config config.GitHubProvider
valid bool
}
type testFourth struct {
type testReleaseStruct struct {
config config.GitHubProvider
releaseVersion *shared.ReleaseVersion
generatedChangelog *shared.GeneratedChangelog
@@ -29,15 +31,15 @@ type testFourth struct {
valid bool
}
var doublesNew = []testDouble{
testDouble{config: config.GitHubProvider{
var testNewClient = []testHelperMethodStruct{
testHelperMethodStruct{config: config.GitHubProvider{
Repo: "foo",
User: "bar",
},
valid: true,
},
testDouble{config: config.GitHubProvider{
testHelperMethodStruct{config: config.GitHubProvider{
Repo: "foo",
User: "bar",
CustomURL: "https://test.com",
@@ -46,22 +48,22 @@ var doublesNew = []testDouble{
},
}
var doublesValidateConfig = []testDouble{
testDouble{config: config.GitHubProvider{
var testHelperMethod = []testHelperMethodStruct{
testHelperMethodStruct{config: config.GitHubProvider{
Repo: "foo",
User: "bar",
},
valid: true,
},
testDouble{config: config.GitHubProvider{
testHelperMethodStruct{config: config.GitHubProvider{
Repo: "",
User: "bar",
},
valid: false,
},
testDouble{config: config.GitHubProvider{
testHelperMethodStruct{config: config.GitHubProvider{
Repo: "foo",
User: "",
},
@@ -72,8 +74,8 @@ var doublesValidateConfig = []testDouble{
var lastVersion, _ = semver.NewVersion("1.0.0")
var newVersion, _ = semver.NewVersion("2.0.0")
var fourthsReleas = []testFourth{
testFourth{
var testReleases = []testReleaseStruct{
testReleaseStruct{
config: config.GitHubProvider{
Repo: "foo",
User: "bar",
@@ -94,11 +96,10 @@ var fourthsReleas = []testFourth{
Title: "title",
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: 500,
requestResponseCode: 200,
valid: true,
},
testFourth{
testReleaseStruct{
config: config.GitHubProvider{
Repo: "foo",
User: "bar",
@@ -119,8 +120,7 @@ var fourthsReleas = []testFourth{
Title: "title",
Content: "content",
},
requestResponseBody: "",
requestResponseCode: 422,
requestResponseCode: 400,
valid: false,
},
}
@@ -128,16 +128,17 @@ var fourthsReleas = []testFourth{
func initHTTPServer(respCode int, body string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(body))
rw.Header().Set("Content-Type", "application/json")
log.Infof("Got request with method: %s, URL: %s", req.Method, req.URL)
log.Infof("Will response with statuscode: %d, body: %s", respCode, body)
rw.WriteHeader(respCode)
rw.Header().Set("Content-Type", "application/json")
rw.Write([]byte(body))
}))
}
func TestNew(t *testing.T) {
for _, testOject := range doublesNew {
for _, testOject := range testNewClient {
if testOject.valid {
os.Setenv("GITHUB_ACCESS_TOKEN", "XXX")
}
@@ -152,7 +153,7 @@ func TestNew(t *testing.T) {
func TestGetCommitURL(t *testing.T) {
os.Setenv("GITHUB_ACCESS_TOKEN", "XX")
for _, testOject := range doublesNew {
for _, testOject := range testNewClient {
client, _ := github.New(&testOject.config)
actualUrl := client.GetCommitURL()
if testOject.config.CustomURL != "" {
@@ -170,7 +171,7 @@ func TestGetCommitURL(t *testing.T) {
func TestGetCompareURL(t *testing.T) {
os.Setenv("GITHUB_ACCESS_TOKEN", "XX")
for _, testOject := range doublesNew {
for _, testOject := range testNewClient {
client, _ := github.New(&testOject.config)
actualUrl := client.GetCompareURL("1", "2")
if testOject.config.CustomURL != "" {
@@ -188,7 +189,7 @@ func TestGetCompareURL(t *testing.T) {
func TestValidateConfig(t *testing.T) {
os.Setenv("GITHUB_ACCESS_TOKEN", "XX")
for _, testOject := range doublesValidateConfig {
for _, testOject := range testHelperMethod {
client, _ := github.New(&testOject.config)
err := client.ValidateConfig()
@@ -201,24 +202,25 @@ func TestValidateConfig(t *testing.T) {
func TestCreateRelease(t *testing.T) {
os.Setenv("GITHUB_ACCESS_TOKEN", "XX")
for _, testObejct := range fourthsReleas {
for _, testObejct := range testReleases {
if testObejct.valid {
server := initHTTPServer(testObejct.requestResponseCode, "")
testObejct.config.CustomURL = server.URL
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)
if err != nil {
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")