Skip to content

Commit

Permalink
Merge pull request #879 from bats-core/fix/check_ifs
Browse files Browse the repository at this point in the history
Ensure IFS is not changed in user contexts
  • Loading branch information
martin-schulze-vireso committed Mar 5, 2024
2 parents ab2a86e + 5142f45 commit 36451c9
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Expand Up @@ -26,6 +26,7 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
`set -u`/`set -o nounset` works as expected (#827)
* fix `--gather-test-outputs-in` fails on tests with multiple `/` (#789)
* install does not create unused `/usr/share/bats` anymore (#857)
* ensure IFS is unchanged in `{setup,teardown}{_suite,_file,}`, `@test` and free code (#879)

### Changed

Expand Down
26 changes: 15 additions & 11 deletions libexec/bats-core/bats-gather-tests
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
6 changes: 6 additions & 0 deletions test/bats.bats
Expand Up @@ -1495,3 +1495,9 @@ END_OF_ERR_MSG
[ "${lines[16]}" == "ok 7 normal test2" ]
[ "${#lines[*]}" -eq 17 ]
}

@test "IFS is preserved in all contexts" {
bats_require_minimum_version 1.5.0

reentrant_run -0 bats --print-output-on-failure "$FIXTURE_ROOT/preserve_IFS" --filter-tags ''
}
8 changes: 8 additions & 0 deletions test/fixtures/bats/preserve_IFS/helper.bash
@@ -0,0 +1,8 @@
check_ifs() {
if [[ "$IFS" != $' \t\n' ]]; then
echo "${FUNCNAME[*]}"
echo "${BASH_SOURCE[*]}"
hexdump -c <<<"$IFS"
exit 1
fi >&2
}
11 changes: 11 additions & 0 deletions test/fixtures/bats/preserve_IFS/setup_suite.bash
@@ -0,0 +1,11 @@
load helper.bash

check_ifs

teardown_suite() {
check_ifs
}

setup_suite() {
check_ifs
}
15 changes: 15 additions & 0 deletions test/fixtures/bats/preserve_IFS/test.bats
@@ -0,0 +1,15 @@
load helper.bash

check_ifs

setup_file() {
check_ifs
}

teardown_file() {
check_ifs
}

@test test {
check_ifs
}

0 comments on commit 36451c9

Please sign in to comment.