From 214dc7d87a6676b492ac8336c10bb6240aa36db0 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Fri, 12 Jul 2024 15:37:11 +0200 Subject: [PATCH 1/4] fix: test_doubles to enable strictmode --- src/test_doubles.sh | 6 +++++- tests/acceptance/mock_test.sh | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/test_doubles.sh b/src/test_doubles.sh index 5a624983..a13fc7df 100644 --- a/src/test_doubles.sh +++ b/src/test_doubles.sh @@ -88,7 +88,11 @@ function assert_have_been_called_times() { actual="${variable}_times" local label="${3:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}" - if [[ ${!actual} -ne $expected ]]; then + if [[ -z "${!actual-}" && $expected -ne 0 ]]; then + state::add_assertions_failed + console_results::print_failed_test "${label}" "${command}" "to has been called" "${expected} times" + return + elif [[ ${!actual-0} -ne $expected ]]; then state::add_assertions_failed console_results::print_failed_test "${label}" "${command}" "to has been called" "${expected} times" return diff --git a/tests/acceptance/mock_test.sh b/tests/acceptance/mock_test.sh index fab44b83..b852ece4 100644 --- a/tests/acceptance/mock_test.sh +++ b/tests/acceptance/mock_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail # # Make sure that the `runner::clear_mocks()` is being called, From 5ad33ce59f4e06ace46bd2d270b3deef6955de5a Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Fri, 12 Jul 2024 15:40:21 +0200 Subject: [PATCH 2/4] refactor: assert_have_been_called_times --- src/test_doubles.sh | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/test_doubles.sh b/src/test_doubles.sh index a13fc7df..8cb608a0 100644 --- a/src/test_doubles.sh +++ b/src/test_doubles.sh @@ -88,11 +88,7 @@ function assert_have_been_called_times() { actual="${variable}_times" local label="${3:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}" - if [[ -z "${!actual-}" && $expected -ne 0 ]]; then - state::add_assertions_failed - console_results::print_failed_test "${label}" "${command}" "to has been called" "${expected} times" - return - elif [[ ${!actual-0} -ne $expected ]]; then + if [[ -z "${!actual-}" && $expected -ne 0 || ${!actual-0} -ne $expected ]]; then state::add_assertions_failed console_results::print_failed_test "${label}" "${command}" "to has been called" "${expected} times" return From bc8c66f059c663ad54910ccca7ed39ecc8cbb635 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Fri, 12 Jul 2024 16:51:15 +0200 Subject: [PATCH 3/4] chore: set -euo pipefail whenever possible in tests --- bashunit | 16 ++++++++++------ src/colors.sh | 2 +- src/console_results.sh | 4 ++-- tests/acceptance/bashunit_direct_fn_call_test.sh | 1 + .../acceptance/bashunit_execution_error_test.sh | 1 + tests/acceptance/bashunit_fail_test.sh | 1 + .../bashunit_find_tests_command_line_test.sh | 1 + tests/acceptance/bashunit_pass_test.sh | 1 + tests/acceptance/bashunit_path_test.sh | 1 + tests/acceptance/bashunit_report_html_test.sh | 1 + .../acceptance/bashunit_stop_on_failure_test.sh | 1 + tests/acceptance/bashunit_test.sh | 1 + tests/acceptance/bashunit_upgrade_test.sh | 1 + tests/acceptance/install_test.sh | 1 + tests/functional/custom_asserts_test.sh | 1 + tests/functional/logic_test.sh | 1 + tests/functional/multi_invoker_test.sh | 1 + tests/functional/provider_test.sh | 1 + tests/unit/redirect_error_test.sh | 1 + 19 files changed, 29 insertions(+), 9 deletions(-) diff --git a/bashunit b/bashunit index 78a4f78c..95757e02 100755 --- a/bashunit +++ b/bashunit @@ -1,9 +1,11 @@ #!/bin/bash +set -euo pipefail # shellcheck disable=SC2034 declare -r BASHUNIT_VERSION="0.13.0" -readonly BASHUNIT_ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")" +# shellcheck disable=SC2155 +declare -r BASHUNIT_ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")" export BASHUNIT_ROOT_DIR source "$BASHUNIT_ROOT_DIR/src/default_env_config.sh" @@ -40,15 +42,15 @@ while [[ $# -gt 0 ]]; do shift ;; -s|--simple) - SIMPLE_OUTPUT=true + export SIMPLE_OUTPUT=true shift ;; -v|--verbose) - SIMPLE_OUTPUT=false + export SIMPLE_OUTPUT=false shift ;; -S|--stop-on-failure) - STOP_ON_FAILURE=true + export STOP_ON_FAILURE=true shift ;; -e|--env) @@ -58,12 +60,12 @@ while [[ $# -gt 0 ]]; do shift ;; -l|--log-junit) - LOG_JUNIT="$2"; + export LOG_JUNIT="$2"; shift shift ;; -r|--report-html) - REPORT_HTML="$2"; + export REPORT_HTML="$2"; shift shift ;; @@ -88,6 +90,8 @@ while [[ $# -gt 0 ]]; do esac done +set +eu + if [[ -n "$_ASSERT_FN" ]]; then main::exec_assert "$_ASSERT_FN" "${_ARGS[@]}" else diff --git a/src/colors.sh b/src/colors.sh index 59b9f4d0..e98f60a9 100644 --- a/src/colors.sh +++ b/src/colors.sh @@ -17,7 +17,6 @@ sgr() { echo $'\e'"[${codes}m" } -_COLOR_DEFAULT="$(sgr 0)" _COLOR_BOLD="$(sgr 1)" _COLOR_FAINT="$(sgr 2)" _COLOR_BLACK="$(sgr 30)" @@ -31,3 +30,4 @@ _COLOR_RETURN_SUCCESS="$(sgr 42)$_COLOR_BLACK$_COLOR_BOLD" _COLOR_RETURN_SKIPPED="$(sgr 43)$_COLOR_BLACK$_COLOR_BOLD" _COLOR_RETURN_INCOMPLETE="$(sgr 46)$_COLOR_BLACK$_COLOR_BOLD" _COLOR_RETURN_SNAPSHOT="$(sgr 44)$_COLOR_BLACK$_COLOR_BOLD" +_COLOR_DEFAULT="$(sgr 0)" diff --git a/src/console_results.sh b/src/console_results.sh index 476fcd7a..40a6df96 100644 --- a/src/console_results.sh +++ b/src/console_results.sh @@ -143,8 +143,8 @@ function console_results::print_failed_test() { local expected=$2 local failure_condition_message=$3 local actual=$4 - local extra_key=$5 - local extra_value=$6 + local extra_key=${5-} + local extra_value=${6-} printf "\ ${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s diff --git a/tests/acceptance/bashunit_direct_fn_call_test.sh b/tests/acceptance/bashunit_direct_fn_call_test.sh index a775ae9a..d4b37e21 100644 --- a/tests/acceptance/bashunit_direct_fn_call_test.sh +++ b/tests/acceptance/bashunit_direct_fn_call_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail function set_up_before_script() { TEST_ENV_FILE="tests/acceptance/fixtures/.env.default" diff --git a/tests/acceptance/bashunit_execution_error_test.sh b/tests/acceptance/bashunit_execution_error_test.sh index 512c35ff..929bbedf 100644 --- a/tests/acceptance/bashunit_execution_error_test.sh +++ b/tests/acceptance/bashunit_execution_error_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail function set_up_before_script() { TEST_ENV_FILE="tests/acceptance/fixtures/.env.default" diff --git a/tests/acceptance/bashunit_fail_test.sh b/tests/acceptance/bashunit_fail_test.sh index 9fb137b8..d881aaf7 100644 --- a/tests/acceptance/bashunit_fail_test.sh +++ b/tests/acceptance/bashunit_fail_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail function set_up_before_script() { TEST_ENV_FILE="tests/acceptance/fixtures/.env.default" diff --git a/tests/acceptance/bashunit_find_tests_command_line_test.sh b/tests/acceptance/bashunit_find_tests_command_line_test.sh index d9887afe..fee87666 100644 --- a/tests/acceptance/bashunit_find_tests_command_line_test.sh +++ b/tests/acceptance/bashunit_find_tests_command_line_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail function set_up_before_script() { TEST_ENV_FILE="tests/acceptance/fixtures/.env.default" diff --git a/tests/acceptance/bashunit_pass_test.sh b/tests/acceptance/bashunit_pass_test.sh index bb560cd0..1234f861 100644 --- a/tests/acceptance/bashunit_pass_test.sh +++ b/tests/acceptance/bashunit_pass_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail function set_up_before_script() { TEST_ENV_FILE="tests/acceptance/fixtures/.env.default" diff --git a/tests/acceptance/bashunit_path_test.sh b/tests/acceptance/bashunit_path_test.sh index 07ab0e39..856a158d 100644 --- a/tests/acceptance/bashunit_path_test.sh +++ b/tests/acceptance/bashunit_path_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail function set_up_before_script() { TEST_ENV_FILE="tests/acceptance/fixtures/.env.default" diff --git a/tests/acceptance/bashunit_report_html_test.sh b/tests/acceptance/bashunit_report_html_test.sh index 4c63f662..5096bfb9 100644 --- a/tests/acceptance/bashunit_report_html_test.sh +++ b/tests/acceptance/bashunit_report_html_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail function set_up_before_script() { TEST_ENV_FILE="tests/acceptance/fixtures/.env.default" diff --git a/tests/acceptance/bashunit_stop_on_failure_test.sh b/tests/acceptance/bashunit_stop_on_failure_test.sh index 711d20aa..9a6a5923 100644 --- a/tests/acceptance/bashunit_stop_on_failure_test.sh +++ b/tests/acceptance/bashunit_stop_on_failure_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail function set_up_before_script() { TEST_ENV_FILE="tests/acceptance/fixtures/.env.default" diff --git a/tests/acceptance/bashunit_test.sh b/tests/acceptance/bashunit_test.sh index 348ad259..81b96dc8 100644 --- a/tests/acceptance/bashunit_test.sh +++ b/tests/acceptance/bashunit_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail function set_up_before_script() { TEST_ENV_FILE="tests/acceptance/fixtures/.env.default" diff --git a/tests/acceptance/bashunit_upgrade_test.sh b/tests/acceptance/bashunit_upgrade_test.sh index e1a2de9e..b63f9b7a 100644 --- a/tests/acceptance/bashunit_upgrade_test.sh +++ b/tests/acceptance/bashunit_upgrade_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail function set_up() { ./build.sh >/dev/null diff --git a/tests/acceptance/install_test.sh b/tests/acceptance/install_test.sh index bac359f3..cb692034 100644 --- a/tests/acceptance/install_test.sh +++ b/tests/acceptance/install_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail function set_up_before_script() { TEST_ENV_FILE="./tests/acceptance/fixtures/.env.default" diff --git a/tests/functional/custom_asserts_test.sh b/tests/functional/custom_asserts_test.sh index a008b2be..948f5c2c 100644 --- a/tests/functional/custom_asserts_test.sh +++ b/tests/functional/custom_asserts_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail function set_up() { _ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")" diff --git a/tests/functional/logic_test.sh b/tests/functional/logic_test.sh index e9e82bb0..52d106cc 100755 --- a/tests/functional/logic_test.sh +++ b/tests/functional/logic_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")" diff --git a/tests/functional/multi_invoker_test.sh b/tests/functional/multi_invoker_test.sh index 4c4d9f3d..1614b541 100644 --- a/tests/functional/multi_invoker_test.sh +++ b/tests/functional/multi_invoker_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail _TEST_MULTI_INVOKER_ITERATION_FILE="" diff --git a/tests/functional/provider_test.sh b/tests/functional/provider_test.sh index a5f7f9cc..ac61eb65 100644 --- a/tests/functional/provider_test.sh +++ b/tests/functional/provider_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail _TEST_GET_DATA_FROM_PROVIDER_ITERATION_FILE="" diff --git a/tests/unit/redirect_error_test.sh b/tests/unit/redirect_error_test.sh index 62a68085..e43475e1 100644 --- a/tests/unit/redirect_error_test.sh +++ b/tests/unit/redirect_error_test.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail _ERROR_LOG=temp_error.log From ed2711212030ee121ec7a1c2de1c5e0e7650282c Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Fri, 12 Jul 2024 16:53:50 +0200 Subject: [PATCH 4/4] docs: update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5539e57a..9014ba7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ - Support for displaying the clock without `perl` (for non-macOS) - Add `-l|--log-junit ` option - Add `-r|--report-html ` option -- Foundation to enable strictmode +- Enable strictmode ## [0.13.0](https://github.com/TypedDevs/bashunit/compare/0.12.0...0.13.0) - 2024-06-23