Skip to content

Commit e48c418

Browse files
authored
Merge pull request #498 from mattness/fix/dataset-preserve-trailing-whitespace
fix(data_set): preservation of trailing whitespace
2 parents eceec9a + 1ff33f1 commit e48c418

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- set_up_before_script
1414
- tear_down_after_script
1515
- Fix false negative from `assert_have_been_called_with` with pipes
16+
- Fix preservation of trailing whitespace in final argument to `data_set`
1617

1718
## [0.24.0](https://github.com/TypedDevs/bashunit/compare/0.23.0...0.24.0) - 2025-09-14
1819

src/globals.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,5 @@ function data_set() {
128128
printf ' %q' "$arg"
129129
fi
130130
done
131-
printf '\n'
131+
printf ' %q\n' ""
132132
}

src/runner.sh

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,29 @@ function runner::parse_data_provider_args() {
114114
local arg
115115
local encoded_arg
116116
local -a args=()
117-
# Parse args from the input string into an array, respecting quotes and escapes
117+
118+
# Check for shell metacharacters that would break eval or cause globbing
119+
local has_metachar=false
120+
if [[ "$input" =~ [^\\][\|\&\;\*] ]] || [[ "$input" =~ ^[\|\&\;\*] ]]; then
121+
has_metachar=true
122+
fi
123+
124+
# Try eval first (needed for $'...' from printf '%q'), unless metacharacters present
125+
if [[ "$has_metachar" == false ]] && eval "args=($input)" 2>/dev/null && [[ ${#args[@]} -gt 0 ]]; then
126+
# Successfully parsed - remove sentinel if present
127+
local last_idx=$((${#args[@]} - 1))
128+
if [[ -z "${args[$last_idx]}" ]]; then
129+
unset 'args[$last_idx]'
130+
fi
131+
# Print args and return early
132+
for arg in "${args[@]}"; do
133+
encoded_arg="$(helper::encode_base64 "${arg}")"
134+
printf '%s\n' "$encoded_arg"
135+
done
136+
return
137+
fi
138+
139+
# Fallback: parse args from the input string into an array, respecting quotes and escapes
118140
for ((i=0; i<${#input}; i++)); do
119141
local char="${input:$i:1}"
120142
if [ "$escaped" = true ]; then

tests/functional/provider_test.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ function provide_value_with_whitespace() {
103103
data_set "first value" "second value"
104104
}
105105

106+
# @data_provider provide_value_with_trailing_whitespace
107+
function test_trailing_whitespace_in_last_value_from_data_provider() {
108+
local expected="$1"
109+
local actual="$2"
110+
111+
assert_same "${expected}" "${actual}"
112+
}
113+
114+
function provide_value_with_trailing_whitespace() {
115+
# Each data_set is passed the same value twice (expected, actual) to verify preservation
116+
data_set "value " "value "
117+
data_set "value " "value "
118+
}
119+
106120
# @data_provider provide_eval_gotchas
107121
function test_eval_gotchas_from_data_provider() {
108122
input=$1

tests/unit/helpers_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function test_get_provider_data_array() {
150150
}
151151

152152
assert_same \
153-
"one two three" \
153+
"one two three ''" \
154154
"$(helper::get_provider_data "fake_function_get_provider_data_array" "${BASH_SOURCE[0]}")"
155155
}
156156

0 commit comments

Comments
 (0)