Skip to content

Commit 36451c9

Browse files
Merge pull request #879 from bats-core/fix/check_ifs
Ensure IFS is not changed in user contexts
2 parents ab2a86e + 5142f45 commit 36451c9

6 files changed

Lines changed: 56 additions & 11 deletions

File tree

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
2626
`set -u`/`set -o nounset` works as expected (#827)
2727
* fix `--gather-test-outputs-in` fails on tests with multiple `/` (#789)
2828
* install does not create unused `/usr/share/bats` anymore (#857)
29+
* ensure IFS is unchanged in `{setup,teardown}{_suite,_file,}`, `@test` and free code (#879)
2930

3031
### Changed
3132

libexec/bats-core/bats-gather-tests

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,27 @@ abort() {
1818
exit 1
1919
} >&2
2020

21+
read_tags() {
22+
local IFS=,
23+
read -ra tags <<<"$1" || true
24+
if ((${#tags[@]} > 0)); then
25+
for ((i = 0; i < ${#tags[@]}; ++i)); do
26+
bats_trim "tags[$i]" "${tags[$i]}"
27+
done
28+
bats_sort sorted_tags "${tags[@]}"
29+
filter_tags_list+=("${sorted_tags[*]}")
30+
else
31+
filter_tags_list+=("")
32+
fi
33+
}
34+
2135
while [[ "$#" -ne 0 ]]; do
2236
case "$1" in
2337
--dummy-flag)
2438
;;
2539
--filter-tags)
2640
shift
27-
IFS=, read -ra tags <<<"$1" || true
28-
if ((${#tags[@]} > 0)); then
29-
for ((i = 0; i < ${#tags[@]}; ++i)); do
30-
bats_trim "tags[$i]" "${tags[$i]}"
31-
done
32-
bats_sort sorted_tags "${tags[@]}"
33-
IFS=, filter_tags_list+=("${sorted_tags[*]}")
34-
else
35-
filter_tags_list+=("")
36-
fi
41+
read_tags "$1"
3742
;;
3843
--)
3944
shift 1
@@ -250,7 +255,6 @@ fi
250255
# shellcheck source=lib/bats-core/tracing.bash
251256
source "$BATS_ROOT/lib/bats-core/tracing.bash"
252257

253-
254258
bats_gather_tests_exit_trap() {
255259
local bats_gather_tests_exit_status=$?
256260
trap - ERR EXIT DEBUG

test/bats.bats

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,3 +1495,9 @@ END_OF_ERR_MSG
14951495
[ "${lines[16]}" == "ok 7 normal test2" ]
14961496
[ "${#lines[*]}" -eq 17 ]
14971497
}
1498+
1499+
@test "IFS is preserved in all contexts" {
1500+
bats_require_minimum_version 1.5.0
1501+
1502+
reentrant_run -0 bats --print-output-on-failure "$FIXTURE_ROOT/preserve_IFS" --filter-tags ''
1503+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
check_ifs() {
2+
if [[ "$IFS" != $' \t\n' ]]; then
3+
echo "${FUNCNAME[*]}"
4+
echo "${BASH_SOURCE[*]}"
5+
hexdump -c <<<"$IFS"
6+
exit 1
7+
fi >&2
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load helper.bash
2+
3+
check_ifs
4+
5+
teardown_suite() {
6+
check_ifs
7+
}
8+
9+
setup_suite() {
10+
check_ifs
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load helper.bash
2+
3+
check_ifs
4+
5+
setup_file() {
6+
check_ifs
7+
}
8+
9+
teardown_file() {
10+
check_ifs
11+
}
12+
13+
@test test {
14+
check_ifs
15+
}

0 commit comments

Comments
 (0)