Skip to content

Commit ee3c171

Browse files
committed
t: add greplint to detect bare grep assertions
Without a lint guard, bare grep assertions will creep back into tests over time, defeating the previous commit's conversion. Add greplint.pl to catch bare 'grep' used as a test assertion (where 'test_grep' should be used) and '! test_grep' (where 'test_grep !' should be used). greplint.pl reuses the shared shell parser from lib-shell-parser.pl to tokenize test bodies. The Lexer collapses heredocs, command substitutions, and quoted strings into single tokens, so 'grep' appearing inside these contexts is not flagged. A flat walk over the token stream tracks command position and pipeline state to distinguish assertion greps from filter greps. For double-quoted test bodies, a splice adjustment table compensates for backslash-continuation lines that the Lexer consumes without emitting into the body text, ensuring accurate line numbers. Add test fixtures in greplint/ (modeled on chainlint/) covering detection of bare grep assertions, correct skipping of filters, pipelines, redirects, command substitutions, and lint-ok annotations. Wire into the Makefile as: - test-greplint: runs greplint.pl on $(T) $(THELPERS) $(TPERF) - check-greplint: runs greplint.pl on fixtures, diffs against expected - clean-greplint: removes temp dir Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
1 parent 34ceda3 commit ee3c171

40 files changed

Lines changed: 353 additions & 5 deletions

t/.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
t[0-9][0-9][0-9][0-9]/* -whitespace
22
/chainlint/*.expect eol=lf -whitespace
3+
/greplint/*.expect eol=lf -whitespace
4+
/greplint/*.test eol=lf -whitespace
35
/t0110/url-* binary
46
/t3206/* eol=lf
57
/t3900/*.txt eol=lf

t/Makefile

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ TEST_LINT ?= test-lint
2727
ifdef TEST_OUTPUT_DIRECTORY
2828
TEST_RESULTS_DIRECTORY = $(TEST_OUTPUT_DIRECTORY)/test-results
2929
CHAINLINTTMP = $(TEST_OUTPUT_DIRECTORY)/chainlinttmp
30+
GREPLINTTMP = $(TEST_OUTPUT_DIRECTORY)/greplinttmp
3031
else
3132
TEST_RESULTS_DIRECTORY = test-results
3233
CHAINLINTTMP = chainlinttmp
34+
GREPLINTTMP = greplinttmp
3335
endif
3436

3537
# Shell quote;
@@ -38,13 +40,15 @@ TEST_SHELL_PATH_SQ = $(subst ','\'',$(TEST_SHELL_PATH))
3840
PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH))
3941
TEST_RESULTS_DIRECTORY_SQ = $(subst ','\'',$(TEST_RESULTS_DIRECTORY))
4042
CHAINLINTTMP_SQ = $(subst ','\'',$(CHAINLINTTMP))
43+
GREPLINTTMP_SQ = $(subst ','\'',$(GREPLINTTMP))
4144

4245
T = $(sort $(wildcard t[0-9][0-9][0-9][0-9]-*.sh))
4346
THELPERS = $(sort $(filter-out $(T),$(wildcard *.sh)))
4447
TLIBS = $(sort $(wildcard lib-*.sh)) annotate-tests.sh
4548
TPERF = $(sort $(wildcard perf/p[0-9][0-9][0-9][0-9]-*.sh))
4649
TINTEROP = $(sort $(wildcard interop/i[0-9][0-9][0-9][0-9]-*.sh))
4750
CHAINLINTTESTS = $(sort $(patsubst chainlint/%.test,%,$(wildcard chainlint/*.test)))
51+
GREPLINTTESTS = $(sort $(patsubst greplint/%.test,%,$(wildcard greplint/*.test)))
4852
CHAINLINT = '$(PERL_PATH_SQ)' chainlint.pl
4953
UNIT_TEST_SOURCES = $(wildcard unit-tests/t-*.c)
5054
UNIT_TEST_PROGRAMS = $(patsubst unit-tests/%.c,unit-tests/bin/%$(X),$(UNIT_TEST_SOURCES))
@@ -63,8 +67,8 @@ test: pre-clean check-meson $(TEST_LINT)
6367
$(CHAINLINTSUPPRESS) $(MAKE) aggregate-results-and-cleanup
6468

6569
ifneq ($(PERL_PATH),)
66-
test: check-chainlint
67-
prove: check-chainlint
70+
test: check-chainlint check-greplint
71+
prove: check-chainlint check-greplint
6872
endif
6973

7074
failed:
@@ -102,7 +106,7 @@ unit-tests-test-tool:
102106
pre-clean:
103107
$(RM) -r '$(TEST_RESULTS_DIRECTORY_SQ)'
104108

105-
clean-except-prove-cache: clean-chainlint
109+
clean-except-prove-cache: clean-chainlint clean-greplint
106110
$(RM) -r 'trash directory'.*
107111
$(RM) -r valgrind/bin
108112

@@ -120,6 +124,17 @@ check-chainlint:
120124
{ $(CHAINLINT) --emit-all '$(CHAINLINTTMP_SQ)'/tests >'$(CHAINLINTTMP_SQ)'/actual || true; } && \
121125
diff -u '$(CHAINLINTTMP_SQ)'/expect '$(CHAINLINTTMP_SQ)'/actual
122126

127+
clean-greplint:
128+
$(RM) -r '$(GREPLINTTMP_SQ)'
129+
130+
check-greplint:
131+
@mkdir -p '$(GREPLINTTMP_SQ)' && \
132+
'$(PERL_PATH_SQ)' greplint-cat.pl '$(GREPLINTTMP_SQ)' $(GREPLINTTESTS) && \
133+
{ '$(PERL_PATH_SQ)' greplint.pl \
134+
$(patsubst %,greplint/%.test,$(GREPLINTTESTS)) \
135+
>'$(GREPLINTTMP_SQ)'/actual 2>&1 || true; } && \
136+
diff -u '$(GREPLINTTMP_SQ)'/expect '$(GREPLINTTMP_SQ)'/actual
137+
123138
check-meson:
124139
@# awk acts up when trying to match single quotes, so we use \047 instead.
125140
@mkdir -p mesontmp && \
@@ -139,7 +154,7 @@ check-meson:
139154
test-lint: test-lint-duplicates test-lint-executable \
140155
test-lint-filenames
141156
ifneq ($(PERL_PATH),)
142-
test-lint: test-lint-shell-syntax
157+
test-lint: test-lint-shell-syntax test-greplint
143158
else
144159
GIT_TEST_CHAIN_LINT = 0
145160
endif
@@ -160,6 +175,9 @@ test-lint-executable:
160175
test-lint-shell-syntax:
161176
@'$(PERL_PATH_SQ)' check-non-portable-shell.pl $(T) $(THELPERS) $(TPERF)
162177

178+
test-greplint:
179+
@'$(PERL_PATH_SQ)' greplint.pl $(T) $(THELPERS) $(TPERF)
180+
163181
test-lint-filenames:
164182
@# We do *not* pass a glob to ls-files but use grep instead, to catch
165183
@# non-ASCII characters (which are quoted within double-quotes)
@@ -185,7 +203,8 @@ perf:
185203
$(MAKE) -C perf/ all
186204

187205
.PHONY: pre-clean $(T) aggregate-results clean valgrind perf \
188-
check-chainlint clean-chainlint test-chainlint $(UNIT_TESTS)
206+
check-chainlint clean-chainlint test-chainlint \
207+
check-greplint clean-greplint test-greplint $(UNIT_TESTS)
189208

190209
.PHONY: libgit-sys-test libgit-rs-test
191210
libgit-sys-test:

t/greplint-cat.pl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
6+
# Assemble expected output for check-greplint target.
7+
# Usage: greplint-cat.pl <outdir> <test-name> ...
8+
#
9+
# For each <test-name>, reads greplint/<test-name>.expect and
10+
# prepends "greplint/<test-name>.test:" to every non-empty line,
11+
# matching the output format of greplint.pl. Writes combined
12+
# expected output to <outdir>/expect.
13+
14+
my $outdir = shift;
15+
open(my $expect, '>', "$outdir/expect")
16+
or die "unable to open $outdir/expect: $!";
17+
18+
for my $name (@ARGV) {
19+
open(my $fh, '<', "greplint/$name.expect")
20+
or die "unable to open greplint/$name.expect: $!";
21+
while (<$fh>) {
22+
print $expect "greplint/$name.test:$_";
23+
}
24+
close $fh;
25+
}
26+
27+
close $expect;

t/greplint.pl

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
#!/usr/bin/perl
2+
3+
# Detect bare 'grep' used as a test assertion where 'test_grep'
4+
# should be used, and '! test_grep' where 'test_grep !' should
5+
# be used.
6+
#
7+
# The shared shell parser tokenizes test bodies so that 'grep'
8+
# inside heredocs, command substitutions like $(grep ...), and
9+
# quoted strings is collapsed into a single token and never seen
10+
# by our check. A line-oriented approach would need to track
11+
# heredoc delimiters, nested $() depth, and cross-line pipe
12+
# state to avoid false positives on patterns like:
13+
#
14+
# write_script foo.sh <<-\EOF
15+
# grep pattern file # data, not an assertion
16+
# EOF
17+
#
18+
# The Lexer already handles these.
19+
20+
use strict;
21+
use warnings;
22+
use File::Basename;
23+
do(dirname($0) . "/lib-shell-parser.pl")
24+
or die "$0: failed to load lib-shell-parser.pl: $@$!\n";
25+
26+
my $exit_code = 0;
27+
28+
# GrepLintParser inherits ScriptParser's ability to find
29+
# test_expect_success/failure blocks and call check_test()
30+
# on each body. We override check_test() to walk the token
31+
# stream looking for bare grep assertions.
32+
package GrepLintParser;
33+
34+
our @ISA = ('ScriptParser');
35+
36+
# After these tokens, the next token is a command word.
37+
# For example, in 'echo foo && grep bar file', the 'grep'
38+
# after '&&' is at command position and should be flagged.
39+
my %cmd_start = map { $_ => 1 } qw(&& || ; ;; do then else elif), "\n", '{', '(';
40+
41+
# Tokens indicating grep's output is piped or redirected.
42+
my %filter_op = map { $_ => 1 } qw(| > >> <);
43+
44+
# A token is at "command word" position if the shell would
45+
# interpret it as a program name rather than an argument.
46+
# Only 'grep' at command position is an assertion we should
47+
# flag; 'grep' as an argument ('test_must_fail grep') or
48+
# value ('for cmd in grep sed') is not.
49+
sub is_command_word {
50+
my ($tokens, $pos) = @_;
51+
return 1 if $pos == 0;
52+
for (my $j = $pos - 1; $j >= 0; $j--) {
53+
my $t = $tokens->[$j]->[0];
54+
# After a separator or pipe, a new command starts.
55+
return 1 if $cmd_start{$t} || $t eq '|';
56+
# After '}' or ')', what follows is a separator or
57+
# redirect on the compound command, not a new command.
58+
return 0 if $t eq '}' || $t eq ')';
59+
# '!' is a prefix that does not consume command
60+
# position; keep scanning to find what precedes it.
61+
next if $t eq '!';
62+
# Any other word means we are past the command word.
63+
return 0;
64+
}
65+
return 1;
66+
}
67+
68+
# Some bare greps are intentional (e.g. file may not exist,
69+
# data filter). A '# lint-ok' annotation on the source line
70+
# suppresses the warning.
71+
sub lint_ok {
72+
my ($raw_lines, $ln) = @_;
73+
return 0 if $ln < 1 || $ln > @$raw_lines;
74+
return $raw_lines->[$ln - 1] =~ /lint-ok/;
75+
}
76+
77+
# Grep is a filter (not an assertion) if it receives piped
78+
# input or sends its output to a pipe or redirect. Check
79+
# both directions from grep's position in the token stream.
80+
sub is_filter {
81+
my ($tokens, $pos) = @_;
82+
# Backward: is grep receiving piped input?
83+
# Newlines don't break pipes ('cmd |\n grep' is one
84+
# pipeline), so skip past them.
85+
for (my $j = $pos - 1; $j >= 0; $j--) {
86+
my $t = $tokens->[$j]->[0];
87+
return 1 if $t eq '|';
88+
next if $t eq "\n";
89+
last if $cmd_start{$t} || $t eq '}' || $t eq ')';
90+
}
91+
# Forward: is grep piping or redirecting output?
92+
for (my $j = $pos + 1; $j < @$tokens; $j++) {
93+
my $t = $tokens->[$j]->[0];
94+
return 0 if $cmd_start{$t};
95+
return 1 if $filter_op{$t};
96+
}
97+
return 0;
98+
}
99+
100+
# Map a body-relative line number to a file line number.
101+
# For double-quoted bodies, backslash-continuation lines
102+
# (\<newline>) are consumed by the Lexer without appearing
103+
# in the body text, so the inner parser sees fewer lines
104+
# than the source file has. We walk the source lines to
105+
# count continuations and adjust accordingly.
106+
sub body_to_file_line {
107+
my ($body_lineno, $body_token, $raw_lines, $lineno) = @_;
108+
my $file_lineno = $body_lineno + ($lineno || 1) - 1;
109+
# Only double-quoted bodies have line splices.
110+
return $file_lineno unless $body_token->[0] =~ /^"/;
111+
my $adj = 0;
112+
my $body_line = 0;
113+
for my $src_ln ($lineno .. ($body_token->[4] || $lineno)) {
114+
my $line = $raw_lines->[$src_ln - 1];
115+
# Odd trailing backslashes = continuation (\<nl>).
116+
# Even = escaped backslashes (\\), not a continuation.
117+
if (defined($line) && $line =~ /(\\*)\s*$/ &&
118+
length($1) % 2 == 1) {
119+
$adj++;
120+
} else {
121+
last if ++$body_line >= $body_lineno;
122+
}
123+
}
124+
return $file_lineno + $adj;
125+
}
126+
127+
# ScriptParser calls this for each test body found in the script.
128+
sub check_test {
129+
my $self = shift @_;
130+
my $title = ScriptParser::unwrap(shift @_);
131+
my $body_token = shift @_;
132+
my $lineno = $body_token->[3];
133+
my $body = ScriptParser::unwrap($body_token);
134+
# Handle heredoc-style test bodies:
135+
# test_expect_success 'title' - <<\EOF
136+
# grep pattern file
137+
# EOF
138+
# The '-' signals that the body follows as a heredoc.
139+
if ($body eq '-') {
140+
my $herebody = shift @_;
141+
if ($herebody) {
142+
$body = $herebody->{content};
143+
$lineno = $herebody->{start_line};
144+
}
145+
}
146+
return unless $body;
147+
148+
my $raw_lines = $self->{raw_lines};
149+
150+
# The outer parser gives us the body as an opaque string.
151+
# Parse it to get individual tokens with command boundaries.
152+
my $parser = ShellParser->new(\$body);
153+
my @tokens = $parser->parse();
154+
155+
my $file = $self->{file};
156+
157+
for (my $i = 0; $i < @tokens; $i++) {
158+
my $text = $tokens[$i]->[0];
159+
next unless is_command_word(\@tokens, $i);
160+
161+
my $file_lineno = body_to_file_line(
162+
$tokens[$i]->[3] || 0,
163+
$body_token, $raw_lines, $lineno);
164+
165+
# '!' negates the exit code without consuming command
166+
# position. '! test_grep' is an anti-pattern because
167+
# test_grep only prints diagnostics on grep failure,
168+
# and '!' inverts after that decision is already made.
169+
if ($text eq '!') {
170+
if ($i + 1 < @tokens &&
171+
$tokens[$i + 1]->[0] eq 'test_grep' &&
172+
!lint_ok($raw_lines, $file_lineno)) {
173+
print "$file:$file_lineno: error: ",
174+
'use "test_grep !" instead of ',
175+
'"! test_grep"', "\n";
176+
$exit_code = 1;
177+
}
178+
next;
179+
}
180+
181+
# Bare grep as a command (not a filter) is a test
182+
# assertion that should use test_grep for better
183+
# failure diagnostics.
184+
if ($text eq 'grep' &&
185+
!is_filter(\@tokens, $i) &&
186+
!lint_ok($raw_lines, $file_lineno)) {
187+
print "$file:$file_lineno: error: ",
188+
"bare grep outside pipeline ",
189+
"(use test_grep)\n";
190+
$exit_code = 1;
191+
}
192+
}
193+
}
194+
195+
package main;
196+
197+
for my $file (@ARGV) {
198+
open(my $fh, '<:unix:crlf', $file) or die "$0: $file: $!\n";
199+
my @raw_lines = <$fh>;
200+
close $fh;
201+
my $s = join('', @raw_lines);
202+
my $parser = GrepLintParser->new(\$s);
203+
$parser->{file} = $file;
204+
$parser->{raw_lines} = \@raw_lines;
205+
$parser->parse();
206+
}
207+
exit $exit_code;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3: error: bare grep outside pipeline (use test_grep)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test_expect_success 'grep after && is flagged' '
2+
cmd &&
3+
grep pattern file
4+
'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
4: error: bare grep outside pipeline (use test_grep)
2+
8: error: bare grep outside pipeline (use test_grep)
3+
15: error: bare grep outside pipeline (use test_grep)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
test_expect_success 'grep after then/do/else is flagged' '
2+
if true
3+
then
4+
grep pattern file
5+
fi &&
6+
while true
7+
do
8+
grep pattern file &&
9+
break
10+
done &&
11+
if true
12+
then
13+
echo yes
14+
else
15+
grep pattern file
16+
fi
17+
'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2: error: bare grep outside pipeline (use test_grep)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test_expect_success 'grep -c is flagged (not special-cased)' '
2+
grep -c pattern file
3+
'

0 commit comments

Comments
 (0)