Skip to content

Commit cc786f7

Browse files
authored
Merge pull request #587 from TypedDevs/fix/586-data-providers-function-keyword-requirement
fix(helpers): make function keyword optional for data provider tests
2 parents c3270d6 + 3ea27cd commit cc786f7

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
- Can also be set via `BASHUNIT_NO_PROGRESS=true` environment variable
2121

2222
### Fixed
23+
- Data providers now work without the `function` keyword on test functions (Issue #586)
24+
- Previously, data provider detection required at least the first test using a provider to have the `function` keyword
25+
- Now all test functions work consistently regardless of whether they use the `function` keyword
26+
- Fixes regex in `bashunit::helper::get_provider_data()` to make the `function` keyword optional
2327
- Self-test `tests/acceptance/install_test.sh` now passes when no network tools are available (Issue #582)
2428
- Tests skip gracefully with `BASHUNIT_NO_NETWORK=true` or in sandboxed environments
2529

src/helpers.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ function bashunit::helper::get_provider_data() {
262262
local data_provider_function
263263
data_provider_function=$(
264264
# shellcheck disable=SC1087
265-
grep -B 2 -E "function[[:space:]]+$function_name[[:space:]]*\(\)" "$script" 2>/dev/null | \
265+
grep -B 2 -E "(function[[:space:]]+)?$function_name[[:space:]]*\(\)" "$script" 2>/dev/null | \
266266
sed -nE 's/^[[:space:]]*# *@?data_provider[[:space:]]+//p'
267267
)
268268

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Test case for issue #586: data providers should work without the function keyword
5+
# All test functions and providers defined WITHOUT the function keyword
6+
7+
# @data_provider provide_test_data_1
8+
test_should_work_without_function_keyword_1() {
9+
local input="$1"
10+
local expected="$2"
11+
12+
assert_equals "$expected" "$input"
13+
}
14+
15+
provide_test_data_1() {
16+
echo "value1" "value1"
17+
echo "value2" "value2"
18+
}
19+
20+
# @data_provider provide_test_data_2
21+
test_should_work_without_function_keyword_2() {
22+
local input="$1"
23+
24+
assert_not_equals "invalid" "$input"
25+
}
26+
27+
provide_test_data_2() {
28+
echo "first"
29+
echo "second"
30+
}
31+
32+
# @data_provider provide_test_data_3
33+
test_should_work_without_function_keyword_3() {
34+
local value="$1"
35+
36+
assert_matches "^test" "$value"
37+
}
38+
39+
provide_test_data_3() {
40+
bashunit::data_set "test1"
41+
bashunit::data_set "test2"
42+
bashunit::data_set "test3"
43+
}

0 commit comments

Comments
 (0)