Skip to content

Commit 95c421c

Browse files
authored
Merge pull request #421 from TypedDevs/fix/find-total-tests
Count data providers when counting total tests
2 parents 245e394 + 1389f20 commit 95c421c

File tree

6 files changed

+107
-8
lines changed

6 files changed

+107
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Fix typo "to has been called"
66
- Add weekly downloads to the docs
77
- Fix parallel runner
8+
- Count data providers when counting total tests
89

910
## [0.20.0](https://github.com/TypedDevs/bashunit/compare/0.19.1...0.20.0) - 2025-06-01
1011

src/console_header.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ EOF
3636
if [ "$total_tests" -eq 0 ]; then
3737
printf "%s\n" "$BASHUNIT_VERSION"
3838
else
39-
printf "%s | Tests: ~%s\n" "$BASHUNIT_VERSION" "$total_tests"
39+
printf "%s | Tests: %s\n" "$BASHUNIT_VERSION" "$total_tests"
4040
fi
4141
return
4242
fi
4343

4444
if [ "$total_tests" -eq 0 ]; then
4545
printf "${_COLOR_BOLD}${_COLOR_PASSED}bashunit${_COLOR_DEFAULT} - %s\n" "$BASHUNIT_VERSION"
4646
else
47-
printf "${_COLOR_BOLD}${_COLOR_PASSED}bashunit${_COLOR_DEFAULT} - %s | Tests: ~%s\n"\
47+
printf "${_COLOR_BOLD}${_COLOR_PASSED}bashunit${_COLOR_DEFAULT} - %s | Tests: %s\n"\
4848
"$BASHUNIT_VERSION"\
4949
"$total_tests"
5050
fi

src/helpers.sh

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,49 @@ function helpers::find_total_tests() {
196196
return
197197
fi
198198

199-
local pattern='^\s*function\s+test'
200-
if [[ -n "$filter" ]]; then
201-
pattern+=".*$filter"
202-
fi
203-
204-
grep -r -E "$pattern" --include="*[tT]est.sh" "${files[@]}" 2>/dev/null | wc -l | xargs
199+
local total_count=0
200+
local file
201+
202+
for file in "${files[@]}"; do
203+
if [[ ! -f "$file" ]]; then
204+
continue
205+
fi
206+
207+
local file_count
208+
file_count=$( (
209+
# shellcheck source=/dev/null
210+
source "$file"
211+
local all_fn_names
212+
all_fn_names=$(declare -F | awk '{print $3}')
213+
local filtered_functions
214+
filtered_functions=$(helper::get_functions_to_run "test" "$filter" "$all_fn_names") || true
215+
216+
local count=0
217+
if [[ -n "$filtered_functions" ]]; then
218+
# shellcheck disable=SC2206
219+
# shellcheck disable=SC2207
220+
local functions_to_run=($filtered_functions)
221+
for fn_name in "${functions_to_run[@]}"; do
222+
local provider_data=()
223+
while IFS=" " read -r line; do
224+
provider_data+=("$line")
225+
done <<< "$(helper::get_provider_data "$fn_name" "$file")"
226+
227+
if [[ "${#provider_data[@]}" -eq 0 ]]; then
228+
count=$((count + 1))
229+
else
230+
count=$((count + ${#provider_data[@]}))
231+
fi
232+
done
233+
fi
234+
235+
echo "$count"
236+
) )
237+
238+
total_count=$((total_count + file_count))
239+
done
240+
241+
echo "$total_count"
205242
}
206243

207244
function helper::load_test_files() {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
# @data_provider provider_lines
4+
function test_with_provider() {
5+
return 0
6+
}
7+
8+
function provider_lines() {
9+
echo "alpha beta"
10+
echo "gamma"
11+
echo "delta epsilon zeta"
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
function test_first() {
4+
return 0
5+
}
6+
7+
function test_second() {
8+
return 0
9+
}

tests/unit/helpers_test.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,43 @@ function test_normalize_test_function_name_with_interpolation() {
239239

240240
assert_same "Returns value '3' and '4' given" "$(helper::normalize_test_function_name "$fn" "$interpolated_fn")"
241241
}
242+
243+
function helpers_test::find_total_in_subshell() {
244+
bash -c 'source "$1"; shift; helpers::find_total_tests "$@"' bash "$BASHUNIT_ROOT_DIR/src/helpers.sh" "$@"
245+
}
246+
247+
function test_find_total_tests_no_files() {
248+
assert_same "0" "$(helpers_test::find_total_in_subshell)"
249+
}
250+
251+
function test_find_total_tests_simple_file() {
252+
local file
253+
file="$(current_dir)/fixtures/find_total_tests/simple_test.sh"
254+
255+
assert_same "2" "$(helpers_test::find_total_in_subshell "" "$file")"
256+
}
257+
258+
function test_find_total_tests_with_provider() {
259+
local file
260+
file="$(current_dir)/fixtures/find_total_tests/provider_test.sh"
261+
262+
assert_same "3" "$(helpers_test::find_total_in_subshell "" "$file")"
263+
}
264+
265+
function test_find_total_tests_multiple_files() {
266+
local file1
267+
local file2
268+
file1="$(current_dir)/fixtures/find_total_tests/simple_test.sh"
269+
file2="$(current_dir)/fixtures/find_total_tests/provider_test.sh"
270+
271+
assert_same "5" "$(helpers_test::find_total_in_subshell "" "$file1" "$file2")"
272+
}
273+
274+
function test_find_total_tests_with_filter() {
275+
local file1
276+
local file2
277+
file1="$(current_dir)/fixtures/find_total_tests/simple_test.sh"
278+
file2="$(current_dir)/fixtures/find_total_tests/provider_test.sh"
279+
280+
assert_same "3" "$(helpers_test::find_total_in_subshell "with_provider" "$file1" "$file2")"
281+
}

0 commit comments

Comments
 (0)