diff --git a/cmd/github.go b/cmd/github.go index e5e0ab6..6c454dd 100644 --- a/cmd/github.go +++ b/cmd/github.go @@ -1,6 +1,7 @@ package cmd import ( + "flag" "fmt" "os" "runtime" @@ -46,27 +47,29 @@ func updatePackage() bool { fmt.Println("Unable to obtain download url for the binary", binaryName) return false } - downloadedBinaryPath := fmt.Sprintf("/tmp/%s", binaryName) - g := got.New() - err = g.Download(result, downloadedBinaryPath) - if err != nil { - fmt.Println("Unable to download binary", err.Error()) - return false - } - currentBinary, err := os.Executable() - if err != nil { - fmt.Println("Unable to obtain current binary path", err.Error()) - return false - } - err = os.Rename(downloadedBinaryPath, currentBinary) - if err != nil { - fmt.Println("Unable to overwrite current binary", err.Error()) - return false - } - err = os.Chmod(currentBinary, 0777) - if err != nil { - fmt.Println("Unable to make binary executable", err.Error()) - return false + if flag.Lookup("test.v") == nil { + downloadedBinaryPath := fmt.Sprintf("/tmp/%s", binaryName) + g := got.New() + err = g.Download(result, downloadedBinaryPath) + if err != nil { + fmt.Println("Unable to download binary", err.Error()) + return false + } + currentBinary, err := os.Executable() + if err != nil { + fmt.Println("Unable to obtain current binary path", err.Error()) + return false + } + err = os.Rename(downloadedBinaryPath, currentBinary) + if err != nil { + fmt.Println("Unable to overwrite current binary", err.Error()) + return false + } + err = os.Chmod(currentBinary, 0777) + if err != nil { + fmt.Println("Unable to make binary executable", err.Error()) + return false + } } } return true diff --git a/cmd/main.go b/cmd/main.go index 45b2505..c923e55 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,6 +17,7 @@ limitations under the License. package cmd import ( + "flag" "fmt" "net/url" "os" @@ -90,14 +91,14 @@ func checkMatches(content []string, targets []string) bool { } func debugPrint(content string) { - if varDebug { + if params.varDebug && flag.Lookup("test.v") == nil { fmt.Println("DEBUG:", content) } } func (s *Setup) CalculateSemver() SemVer { for _, commit := range s.Commits { - if !varStrict { + if !params.varStrict { s.Semver.Patch++ debugPrint(fmt.Sprintln("Incrementing patch (DEFAULT) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())) } @@ -223,7 +224,7 @@ func (s *Setup) getSemver() string { } func main() { - if varShowVersion { + if params.varShowVersion { var outdatedMsg string latestRelease, latestRelaseOk := checkLatestRelease() if PKG_VERSION != latestRelease && latestRelaseOk { @@ -235,17 +236,20 @@ func main() { } return } - if varUpdate { + if params.varUpdate { updatePackage() + return } - if repo.Generate { + if repo.Generate || params.varGenerateInTest { err := repo.ReadConfig(repo.LocalConfigFile) if err != nil { - panic(err) + fmt.Println("Unable to find config file", repo.LocalConfigFile) + os.Exit(1) } err = repo.Prepare() if err != nil { - panic(err) + fmt.Println("Unable to prepare repository") + os.Exit(1) } repo.ListCommits() repo.ForcedVersioning() diff --git a/cmd/main_test.go b/cmd/main_test.go index 357f88b..2f346a2 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -38,7 +38,7 @@ var ( func (suite *Tests) SetupTest() { os.Chdir(testCurrentPath) assert = assertions.New(suite.T()) - varDebug = true + params.varDebug = true } func TestSuite(t *testing.T) { @@ -404,7 +404,7 @@ func (suite *Tests) TestSetup_CalculateSemver() { s.ForcedVersioning() s.Force = tt.fields.Force s.ListCommits() - varStrict = tt.strictMatching + params.varStrict = tt.strictMatching semver := s.CalculateSemver() assert.Equal(tt.wantSemver.Major, semver.Major, "Unexpected MAJOR semver result in "+tt.name) assert.Equal(tt.wantSemver.Minor, semver.Minor, "Unexpected MINOR semver result in "+tt.name) @@ -434,3 +434,48 @@ func (suite *Tests) Test_debugPrint() { }) } } + +func (suite *Tests) Test_main() { + type vars struct { + varRepoName string + varLocalCfg string + varUseLocal bool + varShowVersion bool + varDebug bool + varUpdate bool + varStrict bool + varGenerateInTest bool + } + tests := []struct { + name string + vars vars + }{ + { + name: "Test printing version", + vars: vars{ + varShowVersion: true, + }, + }, + { + name: "Test update switch", + vars: vars{ + varUpdate: true, + }, + }, + { + name: "Test main", + vars: vars{ + varGenerateInTest: false, + }, + }, + } + for _, tt := range tests { + suite.T().Run(tt.name, func(t *testing.T) { + params = myParams(tt.vars) + repo = &Setup{} + repo.LocalConfigFile = "../config.yaml" + repo.UseLocal = true + main() + }) + } +} diff --git a/cmd/root.go b/cmd/root.go index 66d1fe8..893d706 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,9 +16,6 @@ limitations under the License. package cmd import ( - "os" - "strings" - "github.com/spf13/cobra" ) @@ -48,31 +45,33 @@ func (r *Setup) setupCobra() { if err != nil { panic(err) } - r.UseLocal = varUseLocal + r.UseLocal = params.varUseLocal if err != nil { panic(err) } } -var ( - varRepoName, varLocalCfg string - varUseLocal bool - varShowVersion bool - varDebug bool - varUpdate bool - varStrict bool -) +type myParams struct { + varRepoName string + varLocalCfg string + varUseLocal bool + varShowVersion bool + varDebug bool + varUpdate bool + varStrict bool + varGenerateInTest bool +} + +var params myParams func init() { repo = &Setup{} - if !strings.HasSuffix(os.Args[0], ".test") { - cobra.OnInitialize(repo.setupCobra) - rootCmd.PersistentFlags().StringVarP(&varRepoName, "repository", "r", "https://github.com/lukaszraczylo/simple-gql-client", "Remote repository URL.") - rootCmd.PersistentFlags().StringVarP(&varLocalCfg, "config", "c", "semver.yaml", "Path to config file") - rootCmd.PersistentFlags().BoolVarP(&varUseLocal, "local", "l", false, "Use local repository") - rootCmd.PersistentFlags().BoolVarP(&varShowVersion, "version", "v", false, "Display version") - rootCmd.PersistentFlags().BoolVarP(&varDebug, "debug", "d", false, "Enable debug mode") - rootCmd.PersistentFlags().BoolVarP(&varUpdate, "update", "u", false, "Update binary with latest") - rootCmd.PersistentFlags().BoolVarP(&varStrict, "strict", "s", false, "Strict matching") - } + cobra.OnInitialize(repo.setupCobra) + rootCmd.PersistentFlags().StringVarP(¶ms.varRepoName, "repository", "r", "https://github.com/lukaszraczylo/simple-gql-client", "Remote repository URL.") + rootCmd.PersistentFlags().StringVarP(¶ms.varLocalCfg, "config", "c", "semver.yaml", "Path to config file") + rootCmd.PersistentFlags().BoolVarP(¶ms.varUseLocal, "local", "l", false, "Use local repository") + rootCmd.PersistentFlags().BoolVarP(¶ms.varShowVersion, "version", "v", false, "Display version") + rootCmd.PersistentFlags().BoolVarP(¶ms.varDebug, "debug", "d", false, "Enable debug mode") + rootCmd.PersistentFlags().BoolVarP(¶ms.varUpdate, "update", "u", false, "Update binary with latest") + rootCmd.PersistentFlags().BoolVarP(¶ms.varStrict, "strict", "s", false, "Strict matching") }