Skip to content

Commit

Permalink
test-lib-functions: detect test_when_finished in subshell
Browse files Browse the repository at this point in the history
test_when_finished does nothing in a subshell because the change to
test_cleanup does not affect the parent.

There is no POSIX way to detect that we are in a subshell ($$ and $PPID
are specified to remain unchanged), but we can detect it on Bash and
fall back to ignoring the bug on other shells.

Signed-off-by: John Keeping <john@keeping.me.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
John Keeping authored and Junio C Hamano committed Sep 8, 2015
1 parent da568b6 commit 0968f12
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions t/test-lib-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,11 @@ test_seq () {
# what went wrong.

test_when_finished () {
# We cannot detect when we are in a subshell in general, but by
# doing so on Bash is better than nothing (the test will
# silently pass on other shells).
test "${BASH_SUBSHELL-0}" = 0 ||
error "bug in test script: test_when_finished does nothing in a subshell"
test_cleanup="{ $*
} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
}
Expand Down

0 comments on commit 0968f12

Please sign in to comment.