From d7644b2defea7fcf63abc7fa74d5a7af6145bb2f Mon Sep 17 00:00:00 2001 From: Alan Potter Date: Mon, 15 Jan 2024 06:30:59 -0500 Subject: [PATCH] fix: run test functions in defined order Run the test functions in the order they're defined in the test file. --- CHANGELOG.md | 1 + src/runner.sh | 23 ++++++++++++++++--- ...en_a_test_fail_verbose_output_env.snapshot | 4 ++-- ...a_test_fail_verbose_output_option.snapshot | 4 ++-- ...est_all_tests_files_with_wildcard.snapshot | 4 ++-- ...ll_tests_files_within_a_directory.snapshot | 4 ++-- ...est_all_tests_files_within_a_file.snapshot | 2 +- ..._a_test_passes_verbose_output_env.snapshot | 4 ++-- ...test_passes_verbose_output_option.snapshot | 4 ++-- ....test_bashunit_with_argument_path.snapshot | 4 ++-- ...st_bashunit_with_env_default_path.snapshot | 4 ++-- 11 files changed, 38 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da4f63de..d85c7f42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Add `--upgrade` option to `./bashunit` - Remove support to deprecated `setUp`, `tearDown`, `setUpBeforeScript` and `tearDownAfterScript` functions - Optimize test execution time +- Test functions are now run in the order they're defined in a test file ## [0.10.1](https://github.com/TypedDevs/bashunit/compare/0.10.0...0.10.1) - 2023-11-13 diff --git a/src/runner.sh b/src/runner.sh index 780a85e5..a6dcc0d5 100755 --- a/src/runner.sh +++ b/src/runner.sh @@ -34,16 +34,33 @@ function runner::load_test_files() { done } +function runner::functions_for_script() { + local script="$1" + local all_function_names="$2" + + # Filter the names down to the ones defined in the script, sort them by line + # number + shopt -s extdebug + for f in $all_function_names; do + declare -F "$f" | grep "$script" + done | sort -k2 -n | awk '{print $1}' + shopt -u extdebug +} + function runner::call_test_functions() { local script="$1" local filter="$2" local prefix="test" # Use declare -F to list all function names - local function_names - function_names=$(declare -F | awk '{print $3}') + local all_function_names + all_function_names=$(declare -F | awk '{print $3}') + local filtered_functions + # shellcheck disable=SC2207 + filtered_functions=$(helper::get_functions_to_run "$prefix" "$filter" "$all_function_names") + local functions_to_run # shellcheck disable=SC2207 - functions_to_run=($(helper::get_functions_to_run "$prefix" "$filter" "$function_names")) + functions_to_run=($(runner::functions_for_script "$script" "$filtered_functions")) if [[ "${#functions_to_run[@]}" -gt 0 ]]; then if [[ "$SIMPLE_OUTPUT" == false ]]; then diff --git a/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_env.snapshot b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_env.snapshot index 5879ab4f..ad66557b 100644 --- a/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_env.snapshot +++ b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_env.snapshot @@ -1,11 +1,11 @@ Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh -✓ Passed: Assert contains -✓ Passed: Assert empty ✓ Passed: Assert equals +✓ Passed: Assert contains ✗ Failed: Assert failing Expected '1' but got '0' ✓ Passed: Assert greater and less than +✓ Passed: Assert empty Tests:  4 passed, 1 failed, 5 total Assertions: 6 passed, 1 failed, 7 total diff --git a/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_option.snapshot b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_option.snapshot index 5879ab4f..ad66557b 100644 --- a/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_option.snapshot +++ b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_option.snapshot @@ -1,11 +1,11 @@ Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh -✓ Passed: Assert contains -✓ Passed: Assert empty ✓ Passed: Assert equals +✓ Passed: Assert contains ✗ Failed: Assert failing Expected '1' but got '0' ✓ Passed: Assert greater and less than +✓ Passed: Assert empty Tests:  4 passed, 1 failed, 5 total Assertions: 6 passed, 1 failed, 7 total diff --git a/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_with_wildcard.snapshot b/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_with_wildcard.snapshot index 14ec8e24..0ee25e47 100644 --- a/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_with_wildcard.snapshot +++ b/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_with_wildcard.snapshot @@ -1,9 +1,9 @@ Running ./tests/acceptance/fixtures/tests_path/a_test.sh -✓ Passed: Assert empty ✓ Passed: Assert greater and less than +✓ Passed: Assert empty Running ./tests/acceptance/fixtures/tests_path/other_test.sh -✓ Passed: Assert contains ✓ Passed: Assert equals +✓ Passed: Assert contains Tests:  4 passed, 4 total Assertions: 6 passed, 6 total diff --git a/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_directory.snapshot b/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_directory.snapshot index 14ec8e24..0ee25e47 100644 --- a/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_directory.snapshot +++ b/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_directory.snapshot @@ -1,9 +1,9 @@ Running ./tests/acceptance/fixtures/tests_path/a_test.sh -✓ Passed: Assert empty ✓ Passed: Assert greater and less than +✓ Passed: Assert empty Running ./tests/acceptance/fixtures/tests_path/other_test.sh -✓ Passed: Assert contains ✓ Passed: Assert equals +✓ Passed: Assert contains Tests:  4 passed, 4 total Assertions: 6 passed, 6 total diff --git a/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_file.snapshot b/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_file.snapshot index ad2dca23..2f112e44 100644 --- a/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_file.snapshot +++ b/tests/acceptance/snapshots/bashunit_find_tests_command_line_test_sh.test_all_tests_files_within_a_file.snapshot @@ -1,6 +1,6 @@ Running ./tests/acceptance/fixtures/tests_path/a_test.sh -✓ Passed: Assert empty ✓ Passed: Assert greater and less than +✓ Passed: Assert empty Tests:  2 passed, 2 total Assertions: 3 passed, 3 total diff --git a/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_env.snapshot b/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_env.snapshot index 0b11164f..a442a102 100644 --- a/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_env.snapshot +++ b/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_env.snapshot @@ -1,8 +1,8 @@ Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_passes.sh -✓ Passed: Assert contains -✓ Passed: Assert empty ✓ Passed: Assert equals +✓ Passed: Assert contains ✓ Passed: Assert greater and less than +✓ Passed: Assert empty Tests:  4 passed, 4 total Assertions: 6 passed, 6 total diff --git a/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_option.snapshot b/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_option.snapshot index 0b11164f..a442a102 100644 --- a/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_option.snapshot +++ b/tests/acceptance/snapshots/bashunit_pass_test_sh.test_bashunit_when_a_test_passes_verbose_output_option.snapshot @@ -1,8 +1,8 @@ Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_passes.sh -✓ Passed: Assert contains -✓ Passed: Assert empty ✓ Passed: Assert equals +✓ Passed: Assert contains ✓ Passed: Assert greater and less than +✓ Passed: Assert empty Tests:  4 passed, 4 total Assertions: 6 passed, 6 total diff --git a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_argument_path.snapshot b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_argument_path.snapshot index f6e5f07a..02a1abb9 100644 --- a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_argument_path.snapshot +++ b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_argument_path.snapshot @@ -1,9 +1,9 @@ Running tests/acceptance/fixtures/tests_path/a_test.sh -✓ Passed: Assert empty ✓ Passed: Assert greater and less than +✓ Passed: Assert empty Running tests/acceptance/fixtures/tests_path/other_test.sh -✓ Passed: Assert contains ✓ Passed: Assert equals +✓ Passed: Assert contains Tests:  4 passed, 4 total Assertions: 6 passed, 6 total diff --git a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_env_default_path.snapshot b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_env_default_path.snapshot index f6e5f07a..02a1abb9 100644 --- a/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_env_default_path.snapshot +++ b/tests/acceptance/snapshots/bashunit_path_test_sh.test_bashunit_with_env_default_path.snapshot @@ -1,9 +1,9 @@ Running tests/acceptance/fixtures/tests_path/a_test.sh -✓ Passed: Assert empty ✓ Passed: Assert greater and less than +✓ Passed: Assert empty Running tests/acceptance/fixtures/tests_path/other_test.sh -✓ Passed: Assert contains ✓ Passed: Assert equals +✓ Passed: Assert contains Tests:  4 passed, 4 total Assertions: 6 passed, 6 total