Skip to content

Commit 35ebb1e

Browse files
sunshinecogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 29fb2ec commit 35ebb1e

7 files changed

+63
-2
lines changed

t/chainlint.pl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,29 @@ sub ends_with {
473473
return 1;
474474
}
475475

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+
476492
sub accumulate {
477493
my ($self, $tokens, $cmd) = @_;
478494
goto DONE unless @$tokens;
479495
goto DONE if @$cmd == 1 && $$cmd[0] eq "\n";
480496

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);
483499

484500
# flag missing "&&" at end of previous command
485501
my $n = find_non_nl($tokens);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)