diff --git a/internal/core/testing.go b/internal/core/testing.go index 5f7398b6af..ce58d70371 100644 --- a/internal/core/testing.go +++ b/internal/core/testing.go @@ -3,6 +3,7 @@ package core import ( "bytes" "context" + "flag" "fmt" "io" "io/ioutil" @@ -31,11 +32,17 @@ import ( "github.com/stretchr/testify/require" ) -// Environment variable are prefixed by "CLI_" in order to avoid magic behavior with SDK variables. -// E.g.: SDK_UPDATE_CASSETTES=false will disable retry on WaitFor* methods. +// Test flags +// You can create a binary of each test using "go test -c -o myBinary" var ( - UpdateGoldens = os.Getenv("CLI_UPDATE_GOLDENS") == "true" - UpdateCassettes = os.Getenv("CLI_UPDATE_CASSETTES") == "true" + // UpdateGoldens will update all the golden files of a given test + UpdateGoldens = flag.Bool("goldens", false, "Record goldens") + + // UpdateCassettes will update all cassettes of a given test + UpdateCassettes = flag.Bool("cassettes", false, "Record Cassettes") + + // Debug set the log level to LogLevelDebug + Debug = flag.Bool("debug", false, "Enable Debug Mode") ) // CheckFuncCtx contain the result of a command execution @@ -210,7 +217,7 @@ func createTestClient(t *testing.T, testConfig *TestConfig, httpClient *http.Cli if !testConfig.UseE2EClient { clientOpts = append(clientOpts, scw.WithHTTPClient(httpClient)) - if UpdateCassettes { + if *UpdateCassettes { clientOpts = append(clientOpts, scw.WithEnv()) config, err := scw.LoadConfig() if err == nil { @@ -260,7 +267,7 @@ func Test(config *TestConfig) func(t *testing.T) { writer: os.Stderr, level: logger.LogLevelInfo, } - if os.Getenv("SCW_DEBUG") == "true" { + if *Debug { log.level = logger.LogLevelDebug } @@ -270,7 +277,7 @@ func Test(config *TestConfig) func(t *testing.T) { return "few seconds ago", nil }) - if !UpdateCassettes { + if !*UpdateCassettes { tmp := 0 * time.Second DefaultRetryInterval = &tmp } @@ -283,7 +290,7 @@ func Test(config *TestConfig) func(t *testing.T) { ctx = interactive.InjectMockResponseToContext(ctx, config.PromptResponseMocks) } - httpClient, cleanup, err := getHTTPRecoder(t, UpdateCassettes) + httpClient, cleanup, err := getHTTPRecoder(t, *UpdateCassettes) require.NoError(t, err) defer cleanup() ctx = account.InjectHTTPClient(ctx, httpClient) @@ -454,7 +461,7 @@ func BeforeFuncCombine(beforeFuncs ...BeforeFunc) BeforeFunc { func BeforeFuncWhenUpdatingCassette(beforeFunc BeforeFunc) BeforeFunc { return func(ctx *BeforeFuncCtx) error { - if UpdateCassettes { + if *UpdateCassettes { return beforeFunc(ctx) } return nil @@ -536,7 +543,7 @@ func TestCheckGolden() TestCheck { goldenPath := getTestFilePath(t, ".golden") // In order to avoid diff in goldens we set all timestamp to the same date - if UpdateGoldens { + if *UpdateGoldens { require.NoError(t, os.MkdirAll(path.Dir(goldenPath), 0755)) require.NoError(t, ioutil.WriteFile(goldenPath, []byte(actual), 0644)) //nolint:gosec } diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh index 22ac99d355..e68231ef24 100755 --- a/scripts/run-tests.sh +++ b/scripts/run-tests.sh @@ -3,8 +3,6 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" -OPT_VERBOSE="" - ## # Colorize output ## @@ -50,15 +48,10 @@ function usage() { exit $1; } -SCW_DEBUG="false" -OPT_UPDATE_GOLDENS="false" -OPT_UPDATE_CASSETTES="false" -OPT_RUN_SCOPE="" - ## # Parse arguments ## -while [[ $# > 0 ]] +while [[ $# -gt 0 ]] do case "$1" in -r|-run|--run) # keeping -run as this is the standard Go flag for this @@ -66,17 +59,17 @@ do OPT_RUN_SCOPE="$1" ;; -u|--update) - OPT_UPDATE_GOLDENS="true" - OPT_UPDATE_CASSETTES="true" + OPT_UPDATE_GOLDENS="-goldens" + OPT_UPDATE_CASSETTES="-cassettes" ;; -g|--update-goldens) - OPT_UPDATE_GOLDENS="true" + OPT_UPDATE_GOLDENS="-goldens" ;; -c|--update-cassettes) - OPT_UPDATE_CASSETTES="true" + OPT_UPDATE_CASSETTES="-cassettes" ;; -D|--debug) - SCW_DEBUG="true" + SCW_DEBUG="-debug" ;; -h|--help) usage esac @@ -88,9 +81,9 @@ if [[ ${OPT_UPDATE_CASSETTES} ]] ; then go clean -testcache fi -# Remove golden if thery are being updated, and all tests are being run -if [[ ${OPT_UPDATE_GOLDENS} == "true" ]] && [[ -z ${OPT_RUN_SCOPE} ]]; then +# Remove golden if they are being updated, and all tests are being run +if [[ ${OPT_UPDATE_GOLDENS} ]] && [[ -z ${OPT_RUN_SCOPE} ]]; then find . -type f -name "*.golden" -exec rm -f {} \; fi -SCW_DEBUG=$SCW_DEBUG CLI_UPDATE_GOLDENS=$OPT_UPDATE_GOLDENS CLI_UPDATE_CASSETTES=$OPT_UPDATE_CASSETTES go test -v $ROOT_DIR/... -timeout 20m -run=$OPT_RUN_SCOPE +go test -v $ROOT_DIR/... $OPT_UPDATE_CASSETTES $OPT_UPDATE_GOLDENS $SCW_DEBUG -timeout 20m -run=$OPT_RUN_SCOPE