Skip to content

Commit 9b67c99

Browse files
peffgitster
authored andcommitted
tests: factor portable signal check out of t0005
In POSIX shells, a program which exits due to a signal generally has an exit code of 128 plus the signal number. However, ksh uses 256 plus the signal number. We've accounted for that in t0005, but not in other tests. Let's pull out the logic so we can use it elsewhere. It would be nice for debugging if this additionally printed errors to stderr, like our other test_* helpers. But we're going to need to use it in other places besides the innards of a test_expect block. So let's leave it as generic as possible. Note that we also leave the magic "3" for Windows out of the generic helper. This is an artifact of the way we use raise() to kill ourselves in test-sigchain.c, and will not necessarily apply to all programs. So it's better to keep it out of the helper, to reduce the chance of confusing it with a real call to exit(3). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 05219a1 commit 9b67c99

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

t/t0005-signals.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ EOF
1111

1212
test_expect_success 'sigchain works' '
1313
{ test-sigchain >actual; ret=$?; } &&
14-
case "$ret" in
15-
143) true ;; # POSIX w/ SIGTERM=15
16-
271) true ;; # ksh w/ SIGTERM=15
17-
3) true ;; # Windows
18-
*) false ;;
19-
esac &&
14+
{
15+
# Signal death by raise() on Windows acts like exit(3),
16+
# regardless of the signal number. So we must allow that
17+
# as well as the normal signal check.
18+
test_match_signal 15 "$ret" ||
19+
test "$ret" = 3
20+
} &&
2021
test_cmp expect actual
2122
'
2223

t/test-lib-functions.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,3 +961,18 @@ test_env () {
961961
done
962962
)
963963
}
964+
965+
# Returns true if the numeric exit code in "$2" represents the expected signal
966+
# in "$1". Signals should be given numerically.
967+
test_match_signal () {
968+
if test "$2" = "$((128 + $1))"
969+
then
970+
# POSIX
971+
return 0
972+
elif test "$2" = "$((256 + $1))"
973+
then
974+
# ksh
975+
return 0
976+
fi
977+
return 1
978+
}

0 commit comments

Comments
 (0)