diff --git a/run_tests.sh b/run_tests.sh index 0bdc75a69bb3537cfb7b6cab36c8710782719421..a17ed14519c1a59dae15250771346e6869d36003 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -16,6 +16,7 @@ Options: -t <test file path> Execute specific test files only (all tests are executed by default). This option can be specified multiple times. -v Add the verbose switch to the terraform test command. + -h Display this help message. EOF } @@ -46,22 +47,42 @@ cleanup() { trap 'cleanup' EXIT INT TERM -while getopts 'cht:v' option; do - case $option in - c) ci_run=1;; - h) usage; exit;; - t) tests+=("$OPTARG");; - v) verbose=1;; - *) all_tests=1;; - esac +ci_run=false +verbose=false +tests=() +all_tests=true # Default to true if no -t options are provided + +while getopts ":cht:v" opt; do + case $opt in + c) + ci_run=true + ;; + h) + usage + exit + ;; + t) + if [ -z "$OPTARG" ] || [[ "$OPTARG" == -* ]]; then + echo "Error: Option -t requires a non-empty argument." >&2 + exit 1 + fi + tests+=("$OPTARG") + all_tests=false + ;; + v) + verbose=true + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + :) + echo "Option -$OPTARG requires an argument." >&2 + exit 1 + ;; + esac done -shift $((OPTIND-1)) - -if [[ ${#tests[@]} -gt 0 && -n $all_tests ]]; then - echo -e "Error: Cannot specify both -a and -t together.\n" - usage - exit 1 -fi +shift "$((OPTIND - 1))" test_args=("--var-file=tests/tests.tfvars") @@ -71,8 +92,10 @@ if [[ "${#tests[@]}" -gt 0 ]]; then done fi -if [[ -n $verbose ]]; then +if [ $verbose = true ]; then test_args+=(--verbose) + echo "CI mode: $ci_run" + echo "Verbose: $verbose" fi # Comment out the prevent_destroy lifecycle argument otherwise the tests will fail as they cannot tear down the @@ -87,7 +110,7 @@ if [[ -n $GOOGLE_PROVIDER_VERSION_CONSTRAINT ]]; then > versions.tf.json fi -if [[ -n $ci_run ]]; then +if [ "$ci_run" = true ]; then terraform init terraform test "${test_args[@]}" else