Skip to content

Commit

Permalink
Merge pull request #243 from apotterri/defined-order_20240115
Browse files Browse the repository at this point in the history
fix: run test functions in defined order
  • Loading branch information
antonio-gg-dev authored Feb 19, 2024
2 parents b83246a + d7644b2 commit f339462
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 20 additions & 3 deletions src/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit f339462

Please sign in to comment.