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
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
+11 -7
View File
@@ -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()
+47 -2
View File
@@ -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()
})
}
}
+21 -22
View File
@@ -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(&params.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().BoolVarP(&params.varUseLocal, "local", "l", false, "Use local repository")
rootCmd.PersistentFlags().BoolVarP(&params.varShowVersion, "version", "v", false, "Display version")
rootCmd.PersistentFlags().BoolVarP(&params.varDebug, "debug", "d", false, "Enable debug mode")
rootCmd.PersistentFlags().BoolVarP(&params.varUpdate, "update", "u", false, "Update binary with latest")
rootCmd.PersistentFlags().BoolVarP(&params.varStrict, "strict", "s", false, "Strict matching")
}