Skip to content

fix(data_set): preservation of trailing whitespace#498

Merged
Chemaclass merged 2 commits into
TypedDevs:mainfrom
mattness:fix/dataset-preserve-trailing-whitespace
Oct 4, 2025
Merged

fix(data_set): preservation of trailing whitespace#498
Chemaclass merged 2 commits into
TypedDevs:mainfrom
mattness:fix/dataset-preserve-trailing-whitespace

Conversation

@mattness
Copy link
Copy Markdown
Contributor

@mattness mattness commented Oct 3, 2025

📚 Description

Fixes #497

Problem

data_set was not preserving trailing whitespace in its final argument. For example, data_set "value " or data_set "value " would lose lose one space by the time the test function received the values

Root Cause

The issue occurred in how data_set output was parsed. When using eval "args=($output)" to split the escaped arguments back into an array, bash's word splitting would trim trailing whitespace from the last element because there was nothing after it to delimit where it ended.

Solution

Added an empty string sentinel ('') at the end of data_set output. This ensures there's always something after the last real argument, preventing bash from trimming its trailing whitespace during word splitting.

🔖 Changes

  • Modified data_set to append an empty sentinel: printf '%q\n' ""
  • Updated runner::parse_data_provider_args to remove the sentinel after parsing
  • Added metacharacter detection to safely handle special characters (*, |, &, ;) that could break eval, falling back to character-by-character parsing when needed

✅ To-do list

  • I updated the CHANGELOG.md to reflect the new feature or fix

Prior to this change, data_set was not preserving trailing whitespace in its
final argument. For example, data_set "value " or data_set "value    " would
lose one space by the time the test function received the values.

Adding an empty string sentinel ('') at the end of data_set output ensures
there's always something after the last real argument, preventing bash from
trimming its trailing whitespace during word splitting.
Comment thread src/globals.sh
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 🧠

    Fixed "unbound variable" error when accessing array elements with
    negative indices under set -u (nounset mode). Changed from using
    args[-1] to calculating the last index explicitly and using a
    positive index, which is compatible with strict error checking
@Chemaclass Chemaclass added the bug Something isn't working label Oct 4, 2025
Copy link
Copy Markdown
Member

@Chemaclass Chemaclass left a comment

Choose a reason for hiding this comment

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

I applied a small fix for compatibility with Bash ^3.2. Thank you!

@mattness
Copy link
Copy Markdown
Contributor Author

mattness commented Oct 4, 2025

Bash 3.x got me! Thanks for the fix :)

@Chemaclass Chemaclass merged commit e48c418 into TypedDevs:main Oct 4, 2025
18 checks passed
@mattness mattness deleted the fix/dataset-preserve-trailing-whitespace branch October 4, 2025 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

data_set does not preserve trailing whitespace in final argument

2 participants