You've already forked go-semantic-release
test(ci services): add test for travis
This commit is contained in:
1
go.mod
1
go.mod
@@ -10,6 +10,7 @@ require (
|
||||
github.com/kevinburke/ssh_config v0.0.0-20190630040420-2e50c441276c // indirect
|
||||
github.com/sirupsen/logrus v1.4.2
|
||||
github.com/spf13/cobra v0.0.5
|
||||
github.com/stretchr/testify v1.3.0
|
||||
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
|
||||
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 // indirect
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
||||
|
||||
@@ -21,7 +21,7 @@ type ProviderConfig struct {
|
||||
|
||||
//Service interface
|
||||
type Service interface {
|
||||
Detect() (*ProviderConfig, error)
|
||||
detect() (*ProviderConfig, error)
|
||||
}
|
||||
|
||||
//GetCIProvider get provider
|
||||
@@ -33,12 +33,12 @@ func GetCIProvider(gitUtil *gitutil.GitUtil) (*ProviderConfig, error) {
|
||||
}
|
||||
|
||||
for _, service := range services {
|
||||
config, err := service.Detect()
|
||||
config, err := service.detect()
|
||||
if err == nil {
|
||||
log.Infof("Found CI: %s", config.Name)
|
||||
return config, nil
|
||||
}
|
||||
log.Debugf("%s", err.Error())
|
||||
log.Infof("%s", err.Error())
|
||||
}
|
||||
return nil, fmt.Errorf("could not find any CI, if running locally set env CI=true")
|
||||
}
|
||||
|
||||
86
internal/ci/ci_test.go
Normal file
86
internal/ci/ci_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package ci_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Nightapes/go-semantic-release/internal/ci"
|
||||
"github.com/Nightapes/go-semantic-release/internal/gitutil"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/src-d/go-git.v4"
|
||||
"gopkg.in/src-d/go-git.v4/storage/memory"
|
||||
"os"
|
||||
)
|
||||
|
||||
func TestSum(t *testing.T) {
|
||||
testConfigs := []struct {
|
||||
service string
|
||||
envs map[string]string
|
||||
result *ci.ProviderConfig
|
||||
hasError bool
|
||||
}{
|
||||
{
|
||||
service: "none",
|
||||
envs: map[string]string{},
|
||||
result: nil,
|
||||
hasError: true,
|
||||
},
|
||||
// {
|
||||
// service: "Git",
|
||||
// envs: map[string]string{
|
||||
// "CI": "true",
|
||||
// },
|
||||
// result: &ci.ProviderConfig{IsPR: true, PR: "10", PRBranch: "pr", Branch: "master", Tag: "TAG", Commit: "190bfd6aa60022afd0ef830342cfb07e33c45f37", BuildURL: "https://travis-ci.com/owner/repo/builds/1234", Service: "travis", Name: "Travis CI"},
|
||||
// hasError: false,
|
||||
// },
|
||||
{
|
||||
service: "Travis PR",
|
||||
envs: map[string]string{
|
||||
"TRAVIS": "true",
|
||||
"TRAVIS_PULL_REQUEST": "10",
|
||||
"TRAVIS_COMMIT": "190bfd6aa60022afd0ef830342cfb07e33c45f37",
|
||||
"TRAVIS_TAG": "TAG",
|
||||
"TRAVIS_BUILD_WEB_URL": "https://travis-ci.com/owner/repo/builds/1234",
|
||||
"TRAVIS_BRANCH": "master",
|
||||
"TRAVIS_PULL_REQUEST_BRANCH": "pr",
|
||||
},
|
||||
result: &ci.ProviderConfig{IsPR: true, PR: "10", PRBranch: "pr", Branch: "master", Tag: "TAG", Commit: "190bfd6aa60022afd0ef830342cfb07e33c45f37", BuildURL: "https://travis-ci.com/owner/repo/builds/1234", Service: "travis", Name: "Travis CI"},
|
||||
hasError: false,
|
||||
},
|
||||
{
|
||||
service: "Travis Push",
|
||||
envs: map[string]string{
|
||||
"TRAVIS": "true",
|
||||
"TRAVIS_PULL_REQUEST": "false",
|
||||
"TRAVIS_COMMIT": "190bfd6aa60022afd0ef830342cfb07e33c45f37",
|
||||
"TRAVIS_TAG": "TAG",
|
||||
"TRAVIS_BUILD_WEB_URL": "https://travis-ci.com/owner/repo/builds/1234",
|
||||
"TRAVIS_BRANCH": "master",
|
||||
},
|
||||
result: &ci.ProviderConfig{IsPR: false, PR: "", PRBranch: "", Branch: "master", Tag: "TAG", Commit: "190bfd6aa60022afd0ef830342cfb07e33c45f37", BuildURL: "https://travis-ci.com/owner/repo/builds/1234", Service: "travis", Name: "Travis CI"},
|
||||
hasError: false,
|
||||
},
|
||||
}
|
||||
|
||||
repository, err := git.Init(memory.NewStorage(), nil)
|
||||
assert.NoError(t, err, "should open git repository")
|
||||
|
||||
gitUtilInMemory := &gitutil.GitUtil{
|
||||
Repository: repository,
|
||||
}
|
||||
|
||||
for _, config := range testConfigs {
|
||||
|
||||
for key, value := range config.envs {
|
||||
os.Setenv(key, value)
|
||||
}
|
||||
|
||||
provider, err := ci.GetCIProvider(gitUtilInMemory)
|
||||
assert.Equalf(t, config.hasError, err != nil, "Service %s should have error: %t -> %s", config.service, config.hasError, err)
|
||||
assert.Equalf(t, config.result, provider, "Service %s should have provider", config.service)
|
||||
|
||||
for key, _ := range config.envs {
|
||||
os.Unsetenv(key)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,7 +12,7 @@ type Git struct {
|
||||
}
|
||||
|
||||
//Detect if on Git
|
||||
func (t Git) Detect() (*ProviderConfig, error) {
|
||||
func (t Git) detect() (*ProviderConfig, error) {
|
||||
|
||||
if _, exists := os.LookupEnv("CI"); !exists {
|
||||
return nil, fmt.Errorf("running not git only")
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
type Travis struct{}
|
||||
|
||||
//Detect if on travis
|
||||
func (t Travis) Detect() (*ProviderConfig, error) {
|
||||
func (t Travis) detect() (*ProviderConfig, error) {
|
||||
|
||||
if _, exists := os.LookupEnv("TRAVIS"); !exists {
|
||||
return nil, fmt.Errorf("not running on travis")
|
||||
@@ -19,11 +19,13 @@ func (t Travis) Detect() (*ProviderConfig, error) {
|
||||
isPR := false
|
||||
|
||||
value := os.Getenv("TRAVIS_PULL_REQUEST")
|
||||
pr := ""
|
||||
|
||||
if value == "false" {
|
||||
log.Debugf("TRAVIS_PULL_REQUEST=%s, not running on pr", value)
|
||||
} else {
|
||||
isPR = true
|
||||
pr = value
|
||||
}
|
||||
|
||||
return &ProviderConfig{
|
||||
@@ -34,6 +36,7 @@ func (t Travis) Detect() (*ProviderConfig, error) {
|
||||
BuildURL: os.Getenv("TRAVIS_BUILD_WEB_URL"),
|
||||
Branch: os.Getenv("TRAVIS_BRANCH"),
|
||||
IsPR: isPR,
|
||||
PR: pr,
|
||||
PRBranch: os.Getenv("TRAVIS_PULL_REQUEST_BRANCH"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user