Skip to content

Commit 5d5366a

Browse files
committed
t: lint and convert grep assertions to test_grep
Extend lint-style.pl with a rule that detects bare 'grep' used as a test assertion and converts it to test_grep. test_grep prints the file contents on failure, making test debugging significantly easier. The rule reuses the same parser-based architecture as the negation rule added earlier. parse_commands() is extended to track pipeline and redirection context so that grep used as a data filter (pipes, redirects, -c/-l/-L/-r flags, command substitution, control flow conditions, for-loop item lists) is distinguished from grep used as an assertion (checking exit status against a file argument). is_grep_assertion() classifies each grep command: convertible assertion (pattern and file present), filter (not an assertion), or missing file argument (assertion-level grep reading from empty stdin, flagged as a likely bug but not auto-fixed). The --fix mode handles: - Replacing 'grep' with 'test_grep' - Moving negation from '! grep' to 'test_grep !' - Stripping the -q flag (test_grep inherently checks match status) Since check_bare_grep handles '! grep' -> 'test_grep !' directly, the fallback raw-line scan for '! test_grep' outside test bodies (introduced in the previous negation-only commit) is no longer needed and is removed. Three files require '# lint-ok' annotations for intentional grep usage that cannot be mechanically converted: t3901 (piped stdin via case block), t6437 (glob argument breaks test_grep's test -f check), and t7527 ($? capture on the next line). The test-lint-style scope is extended to include sourced test fragments in subdirectories (t5411/*.sh and similar) via a new TSOURCED variable. Run '--fix' to convert all ~2800 grep assertions across ~340 files in the test suite. test-lib-functions.sh and lib-rebase.sh are excluded from linting since they implement test infrastructure rather than test assertions. Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
1 parent 6012890 commit 5d5366a

353 files changed

Lines changed: 3186 additions & 2862 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

t/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ test-lint-executable:
162162
test-lint-shell-syntax:
163163
@'$(PERL_PATH_SQ)' check-non-portable-shell.pl $(T) $(THELPERS) $(TPERF)
164164

165+
TSOURCED = $(sort $(wildcard t[0-9]*/*.sh))
166+
165167
test-lint-style:
166-
@'$(PERL_PATH_SQ)' lint-style.pl $(T) $(THELPERS) $(TPERF)
168+
@'$(PERL_PATH_SQ)' lint-style.pl $(T) $(THELPERS) $(TPERF) $(TSOURCED)
167169

168170
check-lint-style:
169171
@rc=0; for t in $(LINT_STYLE_TESTS); do \

t/for-each-ref-tests.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ test_expect_success 'Verify descending sort' '
523523

524524
test_expect_success 'Give help even with invalid sort atoms' '
525525
test_expect_code 129 ${git_for_each_ref} --sort=bogus -h >actual 2>&1 &&
526-
grep "^usage: ${git_for_each_ref}" actual
526+
test_grep "^usage: ${git_for_each_ref}" actual
527527
'
528528

529529
cat >expected <<\EOF
@@ -622,7 +622,7 @@ test_expect_success 'Quoting style: tcl' '
622622
for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
623623
test_expect_success "more than one quoting style: $i" "
624624
test_must_fail ${git_for_each_ref} $i 2>err &&
625-
grep '^error: more than one quoting style' err
625+
test_grep '^error: more than one quoting style' err
626626
"
627627
done
628628

@@ -1892,7 +1892,7 @@ test_expect_success "${git_for_each_ref} --stdin: fails if extra args" '
18921892
>in &&
18931893
test_must_fail ${git_for_each_ref} --format="%(refname)" \
18941894
--stdin refs/heads/extra <in 2>err &&
1895-
grep "unknown arguments supplied with --stdin" err
1895+
test_grep "unknown arguments supplied with --stdin" err
18961896
'
18971897

18981898
test_expect_success "${git_for_each_ref} --stdin: matches" '
@@ -1955,11 +1955,11 @@ test_expect_success "${git_for_each_ref} with nested tags" '
19551955

19561956
test_expect_success 'is-base atom with non-commits' '
19571957
${git_for_each_ref} --format="%(is-base:HEAD) %(refname)" >out 2>err &&
1958-
grep "(HEAD) refs/heads/main" out &&
1958+
test_grep "(HEAD) refs/heads/main" out &&
19591959
19601960
test_line_count = 2 err &&
1961-
grep "error: object .* is a commit, not a blob" err &&
1962-
grep "error: bad tag pointer to" err
1961+
test_grep "error: object .* is a commit, not a blob" err &&
1962+
test_grep "error: bad tag pointer to" err
19631963
'
19641964

19651965
GRADE_FORMAT="%(signature:grade)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"

t/lib-bitmap.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ rev_list_tests_head () {
173173

174174
test_expect_success "bitmap --objects handles non-commit objects ($state, $branch)" '
175175
git rev-list --objects --use-bitmap-index $branch tagged-blob >actual &&
176-
grep $blob actual
176+
test_grep $blob actual
177177
'
178178
}
179179

@@ -242,16 +242,16 @@ basic_bitmap_tests () {
242242
GIT_PROGRESS_DELAY=0 \
243243
git pack-objects --all --stdout --progress \
244244
</dev/null >/dev/null 2>stderr &&
245-
grep "Enumerating objects: $count, done" stderr &&
246-
grep "pack-reused $count" stderr &&
245+
test_grep "Enumerating objects: $count, done" stderr &&
246+
test_grep "pack-reused $count" stderr &&
247247
248248
# now the same but with one non-reused object
249249
git commit --allow-empty -m "an extra commit object" &&
250250
GIT_PROGRESS_DELAY=0 \
251251
git pack-objects --all --stdout --progress \
252252
</dev/null >/dev/null 2>stderr &&
253-
grep "Enumerating objects: $((count+1)), done" stderr &&
254-
grep "pack-reused $count" stderr
253+
test_grep "Enumerating objects: $((count+1)), done" stderr &&
254+
test_grep "pack-reused $count" stderr
255255
'
256256
}
257257

@@ -283,7 +283,7 @@ test_rev_exists () {
283283
then
284284
test_path_is_file $midx-$(midx_checksum $objdir).rev
285285
fi &&
286-
grep "\"category\":\"load_midx_revindex\",\"key\":\"source\",\"value\":\"$kind\"" event.trace
286+
test_grep "\"category\":\"load_midx_revindex\",\"key\":\"source\",\"value\":\"$kind\"" event.trace
287287
'
288288
}
289289

t/lib-bundle-uri-protocol.sh

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ test_expect_success "connect with $BUNDLE_URI_PROTOCOL:// using protocol v2: no
6363
>actual 2>err &&
6464
6565
# Server responded using protocol v2
66-
grep "< version 2" log &&
66+
test_grep "< version 2" log &&
6767
68-
! grep bundle-uri log
68+
test_grep ! bundle-uri log
6969
'
7070

7171
test_expect_success "connect with $BUNDLE_URI_PROTOCOL:// using protocol v2: have bundle-uri" '
@@ -78,10 +78,10 @@ test_expect_success "connect with $BUNDLE_URI_PROTOCOL:// using protocol v2: hav
7878
>actual 2>err &&
7979
8080
# Server responded using protocol v2
81-
grep "< version 2" log &&
81+
test_grep "< version 2" log &&
8282
8383
# Server advertised bundle-uri capability
84-
grep "< bundle-uri" log
84+
test_grep "< bundle-uri" log
8585
'
8686

8787
test_expect_success "clone with $BUNDLE_URI_PROTOCOL:// using protocol v2: request bundle-uris" '
@@ -95,13 +95,13 @@ test_expect_success "clone with $BUNDLE_URI_PROTOCOL:// using protocol v2: reque
9595
>actual 2>err &&
9696
9797
# Server responded using protocol v2
98-
grep "< version 2" log &&
98+
test_grep "< version 2" log &&
9999
100100
# Server advertised bundle-uri capability
101-
grep "< bundle-uri" log &&
101+
test_grep "< bundle-uri" log &&
102102
103103
# Client did not issue bundle-uri command
104-
! grep "> command=bundle-uri" log &&
104+
test_grep ! "> command=bundle-uri" log &&
105105
106106
GIT_TRACE_PACKET="$PWD/log" \
107107
git \
@@ -111,13 +111,13 @@ test_expect_success "clone with $BUNDLE_URI_PROTOCOL:// using protocol v2: reque
111111
>actual 2>err &&
112112
113113
# Server responded using protocol v2
114-
grep "< version 2" log &&
114+
test_grep "< version 2" log &&
115115
116116
# Server advertised bundle-uri capability
117-
grep "< bundle-uri" log &&
117+
test_grep "< bundle-uri" log &&
118118
119119
# Client issued bundle-uri command
120-
grep "> command=bundle-uri" log &&
120+
test_grep "> command=bundle-uri" log &&
121121
122122
GIT_TRACE_PACKET="$PWD/log3" \
123123
git \
@@ -128,13 +128,13 @@ test_expect_success "clone with $BUNDLE_URI_PROTOCOL:// using protocol v2: reque
128128
>actual 2>err &&
129129
130130
# Server responded using protocol v2
131-
grep "< version 2" log3 &&
131+
test_grep "< version 2" log3 &&
132132
133133
# Server advertised bundle-uri capability
134-
grep "< bundle-uri" log3 &&
134+
test_grep "< bundle-uri" log3 &&
135135
136136
# Client did not issue bundle-uri command (--bundle-uri override)
137-
! grep "> command=bundle-uri" log3
137+
test_grep ! "> command=bundle-uri" log3
138138
'
139139

140140
# The remaining tests will all assume transfer.bundleURI=true

t/lib-httpd.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ test_http_push_nonff () {
275275
'
276276

277277
test_expect_success 'non-fast-forward push show ref status' '
278-
grep "^ ! \[rejected\][ ]*$BRANCH -> $BRANCH (non-fast-forward)$" output
278+
test_grep "^ ! \[rejected\][ ]*$BRANCH -> $BRANCH (non-fast-forward)$" output
279279
'
280280

281281
test_expect_success 'non-fast-forward push shows help message' '

0 commit comments

Comments
 (0)