You've already forked go-semantic-release
fix(tests): Updated tests for a pass ✅
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
@@ -17,12 +17,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type testHelperMethodStruct struct {
|
type testHelperMethodStruct struct {
|
||||||
config config.GitHubProvider
|
config config.GiteaProvider
|
||||||
valid bool
|
valid bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type testReleaseStruct struct {
|
type testReleaseStruct struct {
|
||||||
config config.GitHubProvider
|
config config.GiteaProvider
|
||||||
releaseVersion *shared.ReleaseVersion
|
releaseVersion *shared.ReleaseVersion
|
||||||
generatedChangelog *shared.GeneratedChangelog
|
generatedChangelog *shared.GeneratedChangelog
|
||||||
requestResponseBody string
|
requestResponseBody string
|
||||||
@@ -31,20 +31,13 @@ type testReleaseStruct struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var testNewClient = []testHelperMethodStruct{
|
var testNewClient = []testHelperMethodStruct{
|
||||||
{config: config.GitHubProvider{
|
{config: config.GiteaProvider{
|
||||||
Repo: "foo",
|
Repo: "foo",
|
||||||
User: "bar",
|
User: "bar",
|
||||||
|
URL: "https://hub.cybercinch.nz",
|
||||||
},
|
},
|
||||||
valid: true,
|
valid: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
{config: config.GitHubProvider{
|
|
||||||
Repo: "foo",
|
|
||||||
User: "bar",
|
|
||||||
CustomURL: "https://test.com",
|
|
||||||
},
|
|
||||||
valid: false,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var lastVersion, _ = semver.NewVersion("1.0.0")
|
var lastVersion, _ = semver.NewVersion("1.0.0")
|
||||||
@@ -52,7 +45,7 @@ var newVersion, _ = semver.NewVersion("2.0.0")
|
|||||||
|
|
||||||
var testReleases = []testReleaseStruct{
|
var testReleases = []testReleaseStruct{
|
||||||
{
|
{
|
||||||
config: config.GitHubProvider{
|
config: config.GiteaProvider{
|
||||||
Repo: "foo",
|
Repo: "foo",
|
||||||
User: "bar",
|
User: "bar",
|
||||||
},
|
},
|
||||||
@@ -76,7 +69,7 @@ var testReleases = []testReleaseStruct{
|
|||||||
valid: true,
|
valid: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
config: config.GitHubProvider{
|
config: config.GiteaProvider{
|
||||||
Repo: "foo",
|
Repo: "foo",
|
||||||
User: "bar",
|
User: "bar",
|
||||||
},
|
},
|
||||||
@@ -100,100 +93,147 @@ var testReleases = []testReleaseStruct{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func initHTTPServer(respCode int, body string) *httptest.Server {
|
func initHTTPServerMux() *httptest.Server {
|
||||||
|
// Take the request entries and return http mux
|
||||||
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/api/v1/version", func(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
log.Infof("Got call from %s %s", req.Method, req.URL.String())
|
||||||
|
rw.WriteHeader(200)
|
||||||
|
rw.Header().Set("Content-Type", "application/json")
|
||||||
|
body := `{
|
||||||
|
"version": "1.21.10"
|
||||||
|
}`
|
||||||
|
if _, err := rw.Write([]byte(body)); err != nil {
|
||||||
|
log.Info(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
mux.HandleFunc("/api/v1/repos/bar/foo/releases", func(rw http.ResponseWriter, req *http.Request) {
|
||||||
log.Infof("Got call from %s %s", req.Method, req.URL.String())
|
log.Infof("Got call from %s %s", req.Method, req.URL.String())
|
||||||
|
|
||||||
rw.WriteHeader(respCode)
|
rw.WriteHeader(200)
|
||||||
|
body := `{
|
||||||
|
"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": []
|
||||||
|
}`
|
||||||
rw.Header().Set("Content-Type", "application/json")
|
rw.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
if _, err := rw.Write([]byte(body)); err != nil {
|
if _, err := rw.Write([]byte(body)); err != nil {
|
||||||
log.Info(err)
|
log.Info(err)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}))
|
server := httptest.NewServer(mux)
|
||||||
|
return server
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNewGitea(t *testing.T) {
|
||||||
for _, testOject := range testNewClient {
|
for _, testOject := range testNewClient {
|
||||||
if testOject.valid {
|
if testOject.valid {
|
||||||
os.Setenv("GITHUB_TOKEN", "XXX")
|
os.Setenv("GITEA_TOKEN", "XXX")
|
||||||
}
|
}
|
||||||
|
server := initHTTPServerMux()
|
||||||
|
defer server.Close()
|
||||||
|
testOject.config.URL = server.URL
|
||||||
_, err := New(&testOject.config, true)
|
_, err := New(&testOject.config, true)
|
||||||
|
|
||||||
assert.Equal(t, testOject.valid, err == nil)
|
assert.Equal(t, testOject.valid, err == nil)
|
||||||
|
|
||||||
os.Unsetenv("GITHUB_TOKEN")
|
os.Unsetenv("GITEA_TOKEN")
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetCommitURL(t *testing.T) {
|
func TestGetCommitURLGitea(t *testing.T) {
|
||||||
os.Setenv("GITHUB_TOKEN", "XX")
|
os.Setenv("GITEA_TOKEN", "XX")
|
||||||
for _, testOject := range testNewClient {
|
for _, testOject := range testNewClient {
|
||||||
|
server := initHTTPServerMux()
|
||||||
|
defer server.Close()
|
||||||
|
testOject.config.URL = server.URL
|
||||||
client, _ := New(&testOject.config, false)
|
client, _ := New(&testOject.config, false)
|
||||||
actualURL := client.GetCommitURL()
|
actualURL := client.GetCommitURL()
|
||||||
if testOject.config.CustomURL != "" {
|
expectedURL := fmt.Sprintf("%s/%s/%s/commit/{{hash}}", testOject.config.URL, testOject.config.User, testOject.config.Repo)
|
||||||
expectedURL := fmt.Sprintf("%s/api/v3/%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)
|
assert.EqualValues(t, expectedURL, actualURL)
|
||||||
}
|
}
|
||||||
}
|
os.Unsetenv("GITEA_TOKEN")
|
||||||
os.Unsetenv("GITHUB_TOKEN")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetCompareURL(t *testing.T) {
|
func TestGetCompareURLGitea(t *testing.T) {
|
||||||
os.Setenv("GITHUB_TOKEN", "XX")
|
os.Setenv("GITEA_TOKEN", "XX")
|
||||||
for _, testOject := range testNewClient {
|
for _, testOject := range testNewClient {
|
||||||
|
server := initHTTPServerMux()
|
||||||
|
defer server.Close()
|
||||||
|
testOject.config.URL = server.URL
|
||||||
client, _ := New(&testOject.config, false)
|
client, _ := New(&testOject.config, false)
|
||||||
actualURL := client.GetCompareURL("1", "2")
|
actualURL := client.GetCompareURL("1", "2")
|
||||||
if testOject.config.CustomURL != "" {
|
expectedURL := fmt.Sprintf("%s/%s/%s/compare/%s...%s", testOject.config.URL, testOject.config.User, testOject.config.Repo, "1", "2")
|
||||||
expectedURL := fmt.Sprintf("%s/api/v3/%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)
|
assert.EqualValues(t, expectedURL, actualURL)
|
||||||
}
|
}
|
||||||
}
|
os.Unsetenv("GITEA_TOKEN")
|
||||||
os.Unsetenv("GITHUB_TOKEN")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateRelease(t *testing.T) {
|
func TestCreateReleaseGitea(t *testing.T) {
|
||||||
os.Setenv("GITHUB_TOKEN", "XX")
|
os.Setenv("GITEA_TOKEN", "XX")
|
||||||
|
|
||||||
for _, testObejct := range testReleases {
|
for _, testObject := range testReleases {
|
||||||
if testObejct.valid {
|
if testObject.valid {
|
||||||
server := initHTTPServer(testObejct.requestResponseCode, testObejct.requestResponseBody)
|
server := initHTTPServerMux()
|
||||||
testObejct.config.CustomURL = server.URL
|
defer server.Close()
|
||||||
client, _ := New(&testObejct.config, false)
|
testObject.config.URL = server.URL
|
||||||
|
client, _ := New(&testObject.config, false)
|
||||||
err := client.makeRelease(testObejct.releaseVersion, testObejct.generatedChangelog)
|
err := client.makeRelease(testObject.releaseVersion, testObject.generatedChangelog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, testObejct.valid, err == nil)
|
assert.Equal(t, testObject.valid, err == nil)
|
||||||
|
|
||||||
server.Close()
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
testObejct.config.CustomURL = "http://foo"
|
server := initHTTPServerMux()
|
||||||
client, _ := New(&testObejct.config, false)
|
defer server.Close()
|
||||||
|
testObject.config.URL = server.URL
|
||||||
err := client.makeRelease(testObejct.releaseVersion, testObejct.generatedChangelog)
|
client, _ := New(&testObject.config, false)
|
||||||
|
server.Close() // Simulate error response
|
||||||
|
err := client.makeRelease(testObject.releaseVersion, testObject.generatedChangelog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
}
|
}
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
os.Unsetenv("GITHUB_TOKEN")
|
os.Unsetenv("GITEA_TOKEN")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user