test(releaser/github): tests for GetCommitURL(), GetCompareURL(), ValisateConfig()

This commit is contained in:
fwiedmann
2019-07-29 23:11:07 +02:00
parent e170ca9885
commit 682fae3239

View File

@@ -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")
}