Skip to content

Commit 1686de5

Browse files
peffgitster
authored andcommitted
tests: replace chainlint subshell with a function
To test that we don't break the &&-chain, test-lib.sh does something like: (exit 117) && $test_commands and checks that the result is exit code 117. We don't care what that initial command is, as long as it exits with a unique code. Using "exit" works and is simple, but is a bit expensive since it requires a subshell (to avoid exiting the whole script!). This isn't usually very noticeable, but it can add up for scripts which have a large number of tests. Using "return" naively won't work here, because we'd return from the function eval-ing the snippet (and it wouldn't find &&-chain breakages). But if we further push that into its own function, it does exactly what we want, without extra subshell overhead. According to hyperfine, this produces a measurable improvement when running t3070 (which has 1800 tests, all of them quite short): 'HEAD' ran 1.09 ± 0.01 times faster than 'HEAD~1' Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7b6555a commit 1686de5

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

t/test-lib.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,10 @@ test_eval_ () {
10861086
return $test_eval_ret_
10871087
}
10881088

1089+
fail_117 () {
1090+
return 117
1091+
}
1092+
10891093
test_run_ () {
10901094
test_cleanup=:
10911095
expecting_failure=$2
@@ -1097,7 +1101,7 @@ test_run_ () {
10971101
trace=
10981102
# 117 is magic because it is unlikely to match the exit
10991103
# code of other programs
1100-
if test "OK-117" != "$(test_eval_ "(exit 117) && $1${LF}${LF}echo OK-\$?" 3>&1)"
1104+
if test "OK-117" != "$(test_eval_ "fail_117 && $1${LF}${LF}echo OK-\$?" 3>&1)"
11011105
then
11021106
BUG "broken &&-chain or run-away HERE-DOC: $1"
11031107
fi

0 commit comments

Comments
 (0)