From c86ad684c604337cbf28f6ae537071ced329dd34 Mon Sep 17 00:00:00 2001 From: Nightapes Date: Tue, 16 Jul 2019 20:26:57 +0200 Subject: [PATCH] test(ci services): add test for travis --- go.mod | 1 + internal/ci/ci.go | 6 +-- internal/ci/ci_test.go | 86 ++++++++++++++++++++++++++++++++++++++++++ internal/ci/git.go | 2 +- internal/ci/travis.go | 5 ++- 5 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 internal/ci/ci_test.go diff --git a/go.mod b/go.mod index 1696bb4..f966aa2 100644 --- a/go.mod +++ b/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 diff --git a/internal/ci/ci.go b/internal/ci/ci.go index 0476413..52173e5 100644 --- a/internal/ci/ci.go +++ b/internal/ci/ci.go @@ -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") } diff --git a/internal/ci/ci_test.go b/internal/ci/ci_test.go new file mode 100644 index 0000000..38f948f --- /dev/null +++ b/internal/ci/ci_test.go @@ -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) + } + } + +} diff --git a/internal/ci/git.go b/internal/ci/git.go index 1f13fed..a531e86 100644 --- a/internal/ci/git.go +++ b/internal/ci/git.go @@ -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") diff --git a/internal/ci/travis.go b/internal/ci/travis.go index dffd704..f069c59 100644 --- a/internal/ci/travis.go +++ b/internal/ci/travis.go @@ -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 }