Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions internal/core/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"bytes"
"context"
"flag"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
25 changes: 9 additions & 16 deletions scripts/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"

OPT_VERBOSE=""

##
# Colorize output
##
Expand Down Expand Up @@ -50,33 +48,28 @@ 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
shift
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
Expand All @@ -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