Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- set_up_before_script
- tear_down_after_script
- Fix false negative from `assert_have_been_called_with` with pipes
- Fix preservation of trailing whitespace in final argument to `data_set`

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

Expand Down
2 changes: 1 addition & 1 deletion src/globals.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,5 @@ function data_set() {
printf ' %q' "$arg"
fi
done
printf '\n'
printf ' %q\n' ""
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be made more succinct with the following, but I chose not to do so in order to minimize what changed.

function data_set() {
  for arg in "${@}"; do
    printf '%q ' "${arg}"
  done
  printf '%q\n' ""  # Sentinel
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to work on this refactoring in a follow up PR if you want 🧠

23 changes: 22 additions & 1 deletion src/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,28 @@ function runner::parse_data_provider_args() {
local arg
local encoded_arg
local -a args=()
# Parse args from the input string into an array, respecting quotes and escapes

# Check for shell metacharacters that would break eval or cause globbing
local has_metachar=false
if [[ "$input" =~ [^\\][\|\&\;\*] ]] || [[ "$input" =~ ^[\|\&\;\*] ]]; then
has_metachar=true
fi

# Try eval first (needed for $'...' from printf '%q'), unless metacharacters present
if [[ "$has_metachar" == false ]] && eval "args=($input)" 2>/dev/null; then
# Successfully parsed - remove sentinel if present
if [[ ${#args[@]} -gt 0 && -z "${args[-1]}" ]]; then
unset 'args[-1]'
fi
# Print args and return early
for arg in "${args[@]}"; do
encoded_arg="$(helper::encode_base64 "${arg}")"
printf '%s\n' "$encoded_arg"
done
return
fi

# Fallback: parse args from the input string into an array, respecting quotes and escapes
for ((i=0; i<${#input}; i++)); do
local char="${input:$i:1}"
if [ "$escaped" = true ]; then
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/provider_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ function provide_value_with_whitespace() {
data_set "first value" "second value"
}

# @data_provider provide_value_with_trailing_whitespace
function test_trailing_whitespace_in_last_value_from_data_provider() {
local expected="$1"
local actual="$2"

assert_same "${expected}" "${actual}"
}

function provide_value_with_trailing_whitespace() {
# Each data_set is passed the same value twice (expected, actual) to verify preservation
data_set "value " "value "
data_set "value " "value "
}

# @data_provider provide_eval_gotchas
function test_eval_gotchas_from_data_provider() {
input=$1
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/helpers_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function test_get_provider_data_array() {
}

assert_same \
"one two three" \
"one two three ''" \
"$(helper::get_provider_data "fake_function_get_provider_data_array" "${BASH_SOURCE[0]}")"
}

Expand Down
Loading