Files
git-mirror/t/greplint-cat.pl
Michael Montalbo be7112d1bb 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 source-line walk counts
backslash-continuation lines that the Lexer consumes without
emitting into the body text, adjusting the reported line number
accordingly.

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

Add eol=lf entries in t/.gitattributes for greplint fixtures,
matching chainlint, so that check-greplint passes on Windows
where core.autocrlf would otherwise cause CRLF mismatches
between expected and actual output.

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2026-07-06 13:27:00 -07:00

28 lines
700 B
Perl

#!/usr/bin/env perl
use strict;
use warnings;
# Assemble expected output for check-greplint target.
# Usage: greplint-cat.pl <outdir> <test-name> ...
#
# For each <test-name>, reads greplint/<test-name>.expect and
# prepends "greplint/<test-name>.test:" to every non-empty line,
# matching the output format of greplint.pl. Writes combined
# expected output to <outdir>/expect.
my $outdir = shift;
open(my $expect, '>', "$outdir/expect")
or die "unable to open $outdir/expect: $!";
for my $name (@ARGV) {
open(my $fh, '<', "greplint/$name.expect")
or die "unable to open greplint/$name.expect: $!";
while (<$fh>) {
print $expect "greplint/$name.test:$_";
}
close $fh;
}
close $expect;