chainlint.pl: don't require `return|exit|continue` to end with `&&`

In order to check for &&-chain breakage, each time TestParser encounters a new command, it checks whether the previous command ends with `&&`, and -- with a couple exceptions -- signals breakage if it does not. The first exception is that a command may validly end with `||`, which is commonly employed as `command || return 1` at the very end of a loop body to terminate the loop early. The second is that piping one command's output with `|` to another command does not constitute a &&-chain break (the exit status of the pipe is the exit status of the final command in the pipe). However, it turns out that there are a few additional cases found in the wild in which it is likely safe for `&&` to be missing even when other commands follow. For instance: while {condition-1} do test {condition-2} || return 1 # or `exit 1` within a subshell more-commands done while {condition-1} do test {condition-2} || continue more-commands done Such cases indicate deliberate thought about failure modes by the test author, thus flagging them as breaking the &&-chain is not helpful. Therefore, take these special cases into consideration when checking for &&-chain breakage. 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 35ebb1e37b25b9d799d1064d36a2ce668ad20264
7 files changed +63 -2
t/chainlint.pl
+18 -2
@@ -473,13 +473,29 @@ sub ends_with {
473 return 1;
474 }
475
476 +sub match_ending {
477 + my ($tokens, $endings) = @_;
478 + for my $needles (@$endings) {
479 + next if @$tokens < scalar(grep {$_ ne "\n"} @$needles);
480 + return 1 if ends_with($tokens, $needles);
481 + }
482 + return undef;
483 +}
484 +
485 +my @safe_endings = (
486 + [qr/^(?:&&|\|\||\|)$/],
487 + [qr/^(?:exit|return)$/, qr/^(?:\d+|\$\?)$/],
488 + [qr/^(?:exit|return)$/, qr/^(?:\d+|\$\?)$/, qr/^;$/],
489 + [qr/^(?:exit|return|continue)$/],
490 + [qr/^(?:exit|return|continue)$/, qr/^;$/]);
491 +
492 sub accumulate {
493 my ($self, $tokens, $cmd) = @_;
494 goto DONE unless @$tokens;
495 goto DONE if @$cmd == 1 && $$cmd[0] eq "\n";
496
481 - # did previous command end with "&&", "||", "|"?
482 - goto DONE if ends_with($tokens, [qr/^(?:&&|\|\||\|)$/]);
497 + # did previous command end with "&&", "|", "|| return" or similar?
498 + goto DONE if match_ending($tokens, \@safe_endings);
499
500 # flag missing "&&" at end of previous command
501 my $n = find_non_nl($tokens);
t/chainlint/chain-break-continue.expect new
+12
@@ -0,0 +1,12 @@
1 +git ls-tree --name-only -r refs/notes/many_notes |
2 +while read path
3 +do
4 + test "$path" = "foobar/non-note.txt" && continue
5 + test "$path" = "deadbeef" && continue
6 + test "$path" = "de/adbeef" && continue
7 +
8 + if test $(expr length "$path") -ne $hexsz
9 + then
10 + return 1
11 + fi
12 +done
t/chainlint/chain-break-continue.test new
+13
@@ -0,0 +1,13 @@
1 +git ls-tree --name-only -r refs/notes/many_notes |
2 +while read path
3 +do
4 +# LINT: broken &&-chain okay if explicit "continue"
5 + test "$path" = "foobar/non-note.txt" && continue
6 + test "$path" = "deadbeef" && continue
7 + test "$path" = "de/adbeef" && continue
8 +
9 + if test $(expr length "$path") -ne $hexsz
10 + then
11 + return 1
12 + fi
13 +done
t/chainlint/chain-break-return-exit.expect new
+4
@@ -0,0 +1,4 @@
1 +for i in 1 2 3 4 ; do
2 + git checkout main -b $i || return $?
3 + test_commit $i $i $i tag$i || return $?
4 +done
t/chainlint/chain-break-return-exit.test new
+5
@@ -0,0 +1,5 @@
1 +for i in 1 2 3 4 ; do
2 +# LINT: broken &&-chain okay if explicit "return $?" signals failure
3 + git checkout main -b $i || return $?
4 + test_commit $i $i $i tag$i || return $?
5 +done
t/chainlint/return-loop.expect new
+5
@@ -0,0 +1,5 @@
1 +while test $i -lt $((num - 5))
2 +do
3 + git notes add -m "notes for commit$i" HEAD~$i || return 1
4 + i=$((i + 1))
5 +done
t/chainlint/return-loop.test new
+6
@@ -0,0 +1,6 @@
1 +while test $i -lt $((num - 5))
2 +do
3 +# LINT: "|| return {n}" valid loop escape outside subshell; no "&&" needed
4 + git notes add -m "notes for commit$i" HEAD~$i || return 1
5 + i=$((i + 1))
6 +done