Improve testing across the board.

This commit is contained in:
2021-05-15 19:37:11 +01:00
parent 39af019db4
commit e4007deecd
4 changed files with 103 additions and 52 deletions
+24 -21
View File
@@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"runtime" "runtime"
@@ -46,27 +47,29 @@ func updatePackage() bool {
fmt.Println("Unable to obtain download url for the binary", binaryName) fmt.Println("Unable to obtain download url for the binary", binaryName)
return false return false
} }
downloadedBinaryPath := fmt.Sprintf("/tmp/%s", binaryName) if flag.Lookup("test.v") == nil {
g := got.New() downloadedBinaryPath := fmt.Sprintf("/tmp/%s", binaryName)
err = g.Download(result, downloadedBinaryPath) g := got.New()
if err != nil { err = g.Download(result, downloadedBinaryPath)
fmt.Println("Unable to download binary", err.Error()) if err != nil {
return false fmt.Println("Unable to download binary", err.Error())
} return false
currentBinary, err := os.Executable() }
if err != nil { currentBinary, err := os.Executable()
fmt.Println("Unable to obtain current binary path", err.Error()) if err != nil {
return false fmt.Println("Unable to obtain current binary path", err.Error())
} return false
err = os.Rename(downloadedBinaryPath, currentBinary) }
if err != nil { err = os.Rename(downloadedBinaryPath, currentBinary)
fmt.Println("Unable to overwrite current binary", err.Error()) if err != nil {
return false fmt.Println("Unable to overwrite current binary", err.Error())
} return false
err = os.Chmod(currentBinary, 0777) }
if err != nil { err = os.Chmod(currentBinary, 0777)
fmt.Println("Unable to make binary executable", err.Error()) if err != nil {
return false fmt.Println("Unable to make binary executable", err.Error())
return false
}
} }
} }
return true return true
+11 -7
View File
@@ -17,6 +17,7 @@ limitations under the License.
package cmd package cmd
import ( import (
"flag"
"fmt" "fmt"
"net/url" "net/url"
"os" "os"
@@ -90,14 +91,14 @@ func checkMatches(content []string, targets []string) bool {
} }
func debugPrint(content string) { func debugPrint(content string) {
if varDebug { if params.varDebug && flag.Lookup("test.v") == nil {
fmt.Println("DEBUG:", content) fmt.Println("DEBUG:", content)
} }
} }
func (s *Setup) CalculateSemver() SemVer { func (s *Setup) CalculateSemver() SemVer {
for _, commit := range s.Commits { for _, commit := range s.Commits {
if !varStrict { if !params.varStrict {
s.Semver.Patch++ s.Semver.Patch++
debugPrint(fmt.Sprintln("Incrementing patch (DEFAULT) on ", strings.TrimSuffix(commit.Message, "\n"), "| Semver:", s.getSemver())) 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() { func main() {
if varShowVersion { if params.varShowVersion {
var outdatedMsg string var outdatedMsg string
latestRelease, latestRelaseOk := checkLatestRelease() latestRelease, latestRelaseOk := checkLatestRelease()
if PKG_VERSION != latestRelease && latestRelaseOk { if PKG_VERSION != latestRelease && latestRelaseOk {
@@ -235,17 +236,20 @@ func main() {
} }
return return
} }
if varUpdate { if params.varUpdate {
updatePackage() updatePackage()
return
} }
if repo.Generate { if repo.Generate || params.varGenerateInTest {
err := repo.ReadConfig(repo.LocalConfigFile) err := repo.ReadConfig(repo.LocalConfigFile)
if err != nil { if err != nil {
panic(err) fmt.Println("Unable to find config file", repo.LocalConfigFile)
os.Exit(1)
} }
err = repo.Prepare() err = repo.Prepare()
if err != nil { if err != nil {
panic(err) fmt.Println("Unable to prepare repository")
os.Exit(1)
} }
repo.ListCommits() repo.ListCommits()
repo.ForcedVersioning() repo.ForcedVersioning()
+47 -2
View File
@@ -38,7 +38,7 @@ var (
func (suite *Tests) SetupTest() { func (suite *Tests) SetupTest() {
os.Chdir(testCurrentPath) os.Chdir(testCurrentPath)
assert = assertions.New(suite.T()) assert = assertions.New(suite.T())
varDebug = true params.varDebug = true
} }
func TestSuite(t *testing.T) { func TestSuite(t *testing.T) {
@@ -404,7 +404,7 @@ func (suite *Tests) TestSetup_CalculateSemver() {
s.ForcedVersioning() s.ForcedVersioning()
s.Force = tt.fields.Force s.Force = tt.fields.Force
s.ListCommits() s.ListCommits()
varStrict = tt.strictMatching params.varStrict = tt.strictMatching
semver := s.CalculateSemver() semver := s.CalculateSemver()
assert.Equal(tt.wantSemver.Major, semver.Major, "Unexpected MAJOR semver result in "+tt.name) 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) 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()
})
}
}
+21 -22
View File
@@ -16,9 +16,6 @@ limitations under the License.
package cmd package cmd
import ( import (
"os"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -48,31 +45,33 @@ func (r *Setup) setupCobra() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
r.UseLocal = varUseLocal r.UseLocal = params.varUseLocal
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
var ( type myParams struct {
varRepoName, varLocalCfg string varRepoName string
varUseLocal bool varLocalCfg string
varShowVersion bool varUseLocal bool
varDebug bool varShowVersion bool
varUpdate bool varDebug bool
varStrict bool varUpdate bool
) varStrict bool
varGenerateInTest bool
}
var params myParams
func init() { func init() {
repo = &Setup{} repo = &Setup{}
if !strings.HasSuffix(os.Args[0], ".test") { cobra.OnInitialize(repo.setupCobra)
cobra.OnInitialize(repo.setupCobra) rootCmd.PersistentFlags().StringVarP(&params.varRepoName, "repository", "r", "https://github.com/lukaszraczylo/simple-gql-client", "Remote repository URL.")
rootCmd.PersistentFlags().StringVarP(&varRepoName, "repository", "r", "https://github.com/lukaszraczylo/simple-gql-client", "Remote repository URL.") rootCmd.PersistentFlags().StringVarP(&params.varLocalCfg, "config", "c", "semver.yaml", "Path to config file")
rootCmd.PersistentFlags().StringVarP(&varLocalCfg, "config", "c", "semver.yaml", "Path to config file") rootCmd.PersistentFlags().BoolVarP(&params.varUseLocal, "local", "l", false, "Use local repository")
rootCmd.PersistentFlags().BoolVarP(&varUseLocal, "local", "l", false, "Use local repository") rootCmd.PersistentFlags().BoolVarP(&params.varShowVersion, "version", "v", false, "Display version")
rootCmd.PersistentFlags().BoolVarP(&varShowVersion, "version", "v", false, "Display version") rootCmd.PersistentFlags().BoolVarP(&params.varDebug, "debug", "d", false, "Enable debug mode")
rootCmd.PersistentFlags().BoolVarP(&varDebug, "debug", "d", false, "Enable debug mode") rootCmd.PersistentFlags().BoolVarP(&params.varUpdate, "update", "u", false, "Update binary with latest")
rootCmd.PersistentFlags().BoolVarP(&varUpdate, "update", "u", false, "Update binary with latest") rootCmd.PersistentFlags().BoolVarP(&params.varStrict, "strict", "s", false, "Strict matching")
rootCmd.PersistentFlags().BoolVarP(&varStrict, "strict", "s", false, "Strict matching")
}
} }