Skip to content

Commit

Permalink
Check that IFS changes don't leak
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-schulze-vireso committed Mar 1, 2024
1 parent 9ce68e1 commit 8f58a52
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
26 changes: 15 additions & 11 deletions libexec/bats-core/bats-gather-tests
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,27 @@ abort() {
exit 1
} >&2

read_tags() {
local IFS=,
read -ra tags <<<"$1" || true
if ((${#tags[@]} > 0)); then
for ((i = 0; i < ${#tags[@]}; ++i)); do
bats_trim "tags[$i]" "${tags[$i]}"
done
bats_sort sorted_tags "${tags[@]}"
filter_tags_list+=("${sorted_tags[*]}")
else
filter_tags_list+=("")
fi
}

while [[ "$#" -ne 0 ]]; do
case "$1" in
--dummy-flag)
;;
--filter-tags)
shift
IFS=, read -ra tags <<<"$1" || true
if ((${#tags[@]} > 0)); then
for ((i = 0; i < ${#tags[@]}; ++i)); do
bats_trim "tags[$i]" "${tags[$i]}"
done
bats_sort sorted_tags "${tags[@]}"
IFS=, filter_tags_list+=("${sorted_tags[*]}")
else
filter_tags_list+=("")
fi
read_tags "$1"
;;
--)
shift 1
Expand Down Expand Up @@ -250,7 +255,6 @@ fi
# shellcheck source=lib/bats-core/tracing.bash
source "$BATS_ROOT/lib/bats-core/tracing.bash"


bats_gather_tests_exit_trap() {
local bats_gather_tests_exit_status=$?
trap - ERR EXIT DEBUG
Expand Down
10 changes: 1 addition & 9 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1499,13 +1499,5 @@ END_OF_ERR_MSG
@test "IFS is preserved in all contexts" {
bats_require_minimum_version 1.5.0

REENTRANT_RUN_PRESERVE+=(IFS EXPECTED_IFS)
export EXPECTED_IFS="$IFS"
export EXPECTED_IFS_MISMATCH_FILE="$BATS_TEST_TMPDIR/IFS_mismatches"

reentrant_run -0 bats "$FIXTURE_ROOT/preserve_IFS"

if [[ -e "$EXPECTED_IFS_MISMATCH_FILE" ]]; then
cat "$EXPECTED_IFS_MISMATCH_FILE"
fi
reentrant_run -0 bats --print-output-on-failure "$FIXTURE_ROOT/preserve_IFS" --filter-tags ''
}
8 changes: 5 additions & 3 deletions test/fixtures/bats/preserve_IFS/helper.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
check_ifs() {
if [[ ${EXPECTED_IFS?} != ${IFS} ]]; then
diff <(hexdump -c <<<"$EXPECTED_IFS") <(hexdump -c <<<"$IFS") >"${EXPECTED_IFS_MISMATCH_FILE?}"
if [[ "$IFS" != $' \t\n' ]]; then
echo "${FUNCNAME[*]}"
echo "${BASH_SOURCE[*]}"
hexdump -c <<<"$IFS"
exit 1
fi
fi >&2
}

0 comments on commit 8f58a52

Please sign in to comment.