chainlint.pl: don't flag broken &&-chain if `$?` handled explicitly

There are cases in which tests capture and check a command's exit code explicitly without employing test_expect_code(). They do so by intentionally breaking the &&-chain since it would be impossible to capture "$?" in the failing case if the `status=$?` assignment was part of the &&-chain. Since such constructs are manually checking the exit code, their &&-chain breakage is legitimate and safe, thus should not be flagged. Therefore, stop flagging &&-chain breakage in such cases. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Sep 1, 2022 at 00:29 UTC a8f30ee0502b89ecb660af36784f653a8c3fb20d
3 files changed +26
t/chainlint.pl
+6
@@ -497,6 +497,12 @@ sub accumulate {
497 # did previous command end with "&&", "|", "|| return" or similar?
498 goto DONE if match_ending($tokens, \@safe_endings);
499
500 + # if this command handles "$?" specially, then okay for previous
501 + # command to be missing "&&"
502 + for my $token (@$cmd) {
503 + goto DONE if $token =~ /\$\?/;
504 + }
505 +
506 # flag missing "&&" at end of previous command
507 my $n = find_non_nl($tokens);
508 splice(@$tokens, $n + 1, 0, '?!AMP?!') unless $n < 0;
t/chainlint/chain-break-status.expect new
+9
@@ -0,0 +1,9 @@
1 +OUT=$(( ( large_git ; echo $? 1 >& 3 ) | : ) 3 >& 1) &&
2 +test_match_signal 13 "$OUT" &&
3 +
4 +{ test-tool sigchain > actual ; ret=$? ; } &&
5 +{
6 + test_match_signal 15 "$ret" ||
7 + test "$ret" = 3
8 +} &&
9 +test_cmp expect actual
t/chainlint/chain-break-status.test new
+11
@@ -0,0 +1,11 @@
1 +# LINT: broken &&-chain okay if next command handles "$?" explicitly
2 +OUT=$( ((large_git; echo $? 1>&3) | :) 3>&1 ) &&
3 +test_match_signal 13 "$OUT" &&
4 +
5 +# LINT: broken &&-chain okay if next command handles "$?" explicitly
6 +{ test-tool sigchain >actual; ret=$?; } &&
7 +{
8 + test_match_signal 15 "$ret" ||
9 + test "$ret" = 3
10 +} &&
11 +test_cmp expect actual