From e170ca9885b07743ad26987693fdc5bccc312fb8 Mon Sep 17 00:00:00 2001 From: fwiedmann Date: Sun, 28 Jul 2019 23:00:08 +0200 Subject: [PATCH 1/4] test(releaser/github): test New func --- internal/releaser/github/github_test.go | 44 +++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 internal/releaser/github/github_test.go diff --git a/internal/releaser/github/github_test.go b/internal/releaser/github/github_test.go new file mode 100644 index 0000000..3929027 --- /dev/null +++ b/internal/releaser/github/github_test.go @@ -0,0 +1,44 @@ +package github_test + +import ( + "github.com/Nightapes/go-semantic-release/internal/releaser/github" + "github.com/Nightapes/go-semantic-release/pkg/config" + "github.com/stretchr/testify/assert" + "os" + "testing" +) + +type testDoubleNew struct { + config config.GitHubProvider + valid bool +} + +var doublesNew = []testDoubleNew{ + testDoubleNew{config: config.GitHubProvider{ + Repo: "foo", + User: "bar", + }, + valid: true, + }, + + testDoubleNew{config: config.GitHubProvider{ + Repo: "foo", + User: "bar", + }, + valid: false, + }, +} + +func TestNew(t *testing.T) { + for _, testOject := range doublesNew { + if testOject.valid { + os.Setenv("GITHUB_ACCESS_TOKEN", "XXX") + } + + _, err := github.New(&testOject.config) + assert.Equal(t, testOject.valid, err == nil) + + os.Unsetenv("GITHUB_ACCESS_TOKEN") + + } +} From 682fae323952ee8b8b4b7cd8801d079c13618cd4 Mon Sep 17 00:00:00 2001 From: fwiedmann Date: Mon, 29 Jul 2019 23:11:07 +0200 Subject: [PATCH 2/4] test(releaser/github): tests for GetCommitURL(), GetCompareURL(), ValisateConfig() --- internal/releaser/github/github_test.go | 86 +++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/internal/releaser/github/github_test.go b/internal/releaser/github/github_test.go index 3929027..c8bb1d3 100644 --- a/internal/releaser/github/github_test.go +++ b/internal/releaser/github/github_test.go @@ -1,29 +1,55 @@ package github_test import ( + "fmt" + "os" + "testing" + "github.com/Nightapes/go-semantic-release/internal/releaser/github" "github.com/Nightapes/go-semantic-release/pkg/config" "github.com/stretchr/testify/assert" - "os" - "testing" ) -type testDoubleNew struct { +type testDouble struct { config config.GitHubProvider valid bool } -var doublesNew = []testDoubleNew{ - testDoubleNew{config: config.GitHubProvider{ +var doublesNew = []testDouble{ + testDouble{config: config.GitHubProvider{ Repo: "foo", User: "bar", }, valid: true, }, - testDoubleNew{config: config.GitHubProvider{ + testDouble{config: config.GitHubProvider{ + Repo: "foo", + User: "bar", + CustomURL: "https://test.com", + }, + valid: false, + }, +} + +var doublesValidateConfig = []testDouble{ + testDouble{config: config.GitHubProvider{ Repo: "foo", User: "bar", + }, + valid: true, + }, + + testDouble{config: config.GitHubProvider{ + Repo: "", + User: "bar", + }, + valid: false, + }, + + testDouble{config: config.GitHubProvider{ + Repo: "foo", + User: "", }, valid: false, }, @@ -42,3 +68,51 @@ func TestNew(t *testing.T) { } } + +func TestGetCommitURL(t *testing.T) { + os.Setenv("GITHUB_ACCESS_TOKEN", "XX") + for _, testOject := range doublesNew { + client, _ := github.New(&testOject.config) + actualUrl := client.GetCommitURL() + if testOject.config.CustomURL != "" { + expectedUrl := fmt.Sprintf("%s/%s/%s/commit/{{hash}}", testOject.config.CustomURL, testOject.config.User, testOject.config.Repo) + assert.EqualValues(t, expectedUrl, actualUrl) + + } else { + expectedUrl := fmt.Sprintf("%s/%s/%s/commit/{{hash}}", "https://github.com", testOject.config.User, testOject.config.Repo) + assert.EqualValues(t, expectedUrl, actualUrl) + } + } + os.Unsetenv("GITHUB_ACCESS_TOKEN") + +} + +func TestGetCompareURL(t *testing.T) { + os.Setenv("GITHUB_ACCESS_TOKEN", "XX") + for _, testOject := range doublesNew { + client, _ := github.New(&testOject.config) + actualUrl := client.GetCompareURL("1", "2") + if testOject.config.CustomURL != "" { + expectedUrl := fmt.Sprintf("%s/%s/%s/compare/%s...%s", testOject.config.CustomURL, testOject.config.User, testOject.config.Repo, "1", "2") + assert.EqualValues(t, expectedUrl, actualUrl) + + } else { + expectedUrl := fmt.Sprintf("%s/%s/%s/compare/%s...%s", "https://github.com", testOject.config.User, testOject.config.Repo, "1", "2") + assert.EqualValues(t, expectedUrl, actualUrl) + } + } + os.Unsetenv("GITHUB_ACCESS_TOKEN") + +} + +func TestValidateConfig(t *testing.T) { + os.Setenv("GITHUB_ACCESS_TOKEN", "XX") + for _, testOject := range doublesValidateConfig { + client, _ := github.New(&testOject.config) + err := client.ValidateConfig() + + assert.Equal(t, testOject.valid, err == nil) + + } + os.Unsetenv("GITHUB_ACCESS_TOKEN") +} From bdc4fb1d747ac14dc6ec6e8780215e5a6c5f64b3 Mon Sep 17 00:00:00 2001 From: fwiedmann Date: Tue, 6 Aug 2019 23:21:25 +0200 Subject: [PATCH 3/4] tmp(releaser/github): add test for create release --- internal/releaser/github/github.go | 10 +-- internal/releaser/github/github_test.go | 106 ++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 5 deletions(-) diff --git a/internal/releaser/github/github.go b/internal/releaser/github/github.go index 3f73b95..0d5d099 100644 --- a/internal/releaser/github/github.go +++ b/internal/releaser/github/github.go @@ -89,7 +89,7 @@ func (g *Client) CreateRelease(releaseVersion *shared.ReleaseVersion, generatedC prerelease := releaseVersion.Next.Version.Prerelease() != "" - release, resp, err := g.client.Repositories.CreateRelease(g.context, g.config.User, g.config.Repo, &github.RepositoryRelease{ + release, _, err := g.client.Repositories.CreateRelease(g.context, g.config.User, g.config.Repo, &github.RepositoryRelease{ TagName: &tag, TargetCommitish: &releaseVersion.Branch, Name: &generatedChangelog.Title, @@ -97,19 +97,19 @@ func (g *Client) CreateRelease(releaseVersion *shared.ReleaseVersion, generatedC Draft: &releaseVersion.Draft, Prerelease: &prerelease, }) - if err != nil { - if !strings.Contains(err.Error(), "already_exists") && resp.StatusCode >= http.StatusUnprocessableEntity { + if !strings.Contains(err.Error(), "already_exists") { return fmt.Errorf("could not create release: %v", err) + } else { + log.Infof("A release with tag %s already exits, will not perform a release or update", tag) } - log.Infof("A release with tag %s already exits, will not perform a release or update", tag) } else { g.release = release log.Debugf("Release repsone: %+v", *release) log.Infof("Crated release") } - return nil + } // UploadAssets uploads specified assets diff --git a/internal/releaser/github/github_test.go b/internal/releaser/github/github_test.go index c8bb1d3..d9aba70 100644 --- a/internal/releaser/github/github_test.go +++ b/internal/releaser/github/github_test.go @@ -2,10 +2,15 @@ package github_test import ( "fmt" + "net/http" + "net/http/httptest" "os" "testing" + "github.com/Masterminds/semver" + "github.com/Nightapes/go-semantic-release/internal/releaser/github" + "github.com/Nightapes/go-semantic-release/internal/shared" "github.com/Nightapes/go-semantic-release/pkg/config" "github.com/stretchr/testify/assert" ) @@ -15,6 +20,15 @@ type testDouble struct { valid bool } +type testFourth struct { + config config.GitHubProvider + releaseVersion *shared.ReleaseVersion + generatedChangelog *shared.GeneratedChangelog + requestResponseBody string + requestResponseCode int + valid bool +} + var doublesNew = []testDouble{ testDouble{config: config.GitHubProvider{ Repo: "foo", @@ -55,6 +69,71 @@ var doublesValidateConfig = []testDouble{ }, } +var lastVersion, _ = semver.NewVersion("1.0.0") +var newVersion, _ = semver.NewVersion("2.0.0") + +var fourthsReleas = []testFourth{ + testFourth{ + config: config.GitHubProvider{ + Repo: "foo", + User: "bar", + }, + releaseVersion: &shared.ReleaseVersion{ + Last: shared.ReleaseVersionEntry{ + Version: lastVersion, + Commit: "foo", + }, + Next: shared.ReleaseVersionEntry{ + Version: newVersion, + Commit: "bar", + }, + Branch: "master", + Draft: false, + }, + generatedChangelog: &shared.GeneratedChangelog{ + 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, + valid: true, + }, + testFourth{ + config: config.GitHubProvider{ + Repo: "foo", + User: "bar", + }, + releaseVersion: &shared.ReleaseVersion{ + Last: shared.ReleaseVersionEntry{ + Version: lastVersion, + Commit: "foo", + }, + Next: shared.ReleaseVersionEntry{ + Version: newVersion, + Commit: "bar", + }, + Branch: "master", + Draft: false, + }, + generatedChangelog: &shared.GeneratedChangelog{ + Title: "title", + Content: "content", + }, + requestResponseBody: "", + requestResponseCode: 422, + valid: false, + }, +} + +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.WriteHeader(500) + })) +} + func TestNew(t *testing.T) { for _, testOject := range doublesNew { if testOject.valid { @@ -116,3 +195,30 @@ func TestValidateConfig(t *testing.T) { } os.Unsetenv("GITHUB_ACCESS_TOKEN") } + +func TestCreateRelease(t *testing.T) { + os.Setenv("GITHUB_ACCESS_TOKEN", "XX") + + for _, testObejct := range fourthsReleas { + 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) + } + } + os.Unsetenv("GITHUB_ACCESS_TOKEN") + +} From 3c500142aa701eaa40cd7b3b1a2018cf963ae5ec Mon Sep 17 00:00:00 2001 From: fwiedmann Date: Wed, 7 Aug 2019 23:11:04 +0200 Subject: [PATCH 4/4] temp(releaser/github): modify test --- internal/releaser/github/github_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/releaser/github/github_test.go b/internal/releaser/github/github_test.go index d9aba70..8afde31 100644 --- a/internal/releaser/github/github_test.go +++ b/internal/releaser/github/github_test.go @@ -130,7 +130,9 @@ 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.WriteHeader(500) + rw.Header().Set("Content-Type", "application/json") + + rw.WriteHeader(respCode) })) }