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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 30, 2016 at 04:16 UTC 9b67c9942eba3b1a6b008ddab8ee6e542bd3de6b
2 files changed +22 -6
t/t0005-signals.sh
+7 -6
@@ -11,12 +11,13 @@ EOF
11
12 test_expect_success 'sigchain works' '
13 { 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 + } &&
21 test_cmp expect actual
22 '
23
t/test-lib-functions.sh
+15
@@ -961,3 +961,18 @@ test_env () {
961 done
962 )
963 }
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 +}