From: "Michael Montalbo via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "D. Ben Knoble" <ben.knoble@gmail.com>,
"Eric Sunshine" <sunshine@sunshineco.com>,
"SZEDER Gábor" <szeder.dev@gmail.com>,
"Michael Montalbo" <mmontalbo@gmail.com>,
"Michael Montalbo" <mmontalbo@gmail.com>
Subject: [PATCH v4 1/6] t/README: document test_grep helper
Date: Mon, 06 Jul 2026 05:01:53 +0000 [thread overview]
Message-ID: <44d5db91bc3e951a6d2e34acc545b7093783bdaf.1783314119.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2135.v4.git.1783314119.gitgitgadget@gmail.com>
From: Michael Montalbo <mmontalbo@gmail.com>
test_grep is a wrapper around grep for test assertions that prints
the file contents on failure for easier debugging. It also accepts
'!' as its first argument for negation, which preserves the
diagnostic output that '! test_grep' would suppress.
Despite being widely used (and the preferred replacement for bare
grep in assertions), test_grep has no entry in t/README alongside
the other documented helpers like test_cmp and test_line_count.
Add one.
Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
---
t/README | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/t/README b/t/README
index 085921be4b..4252774f86 100644
--- a/t/README
+++ b/t/README
@@ -1039,6 +1039,40 @@ see test-lib-functions.sh for the full list and their options.
Check whether a file has the length it is expected to.
+ - test_grep [!] [<grep-options>] <pattern> <file>
+
+ Check whether <file> contains a line matching <pattern>, or
+ with '!' that no line matches. Use this instead of bare
+ 'grep <pattern> <file>' in test assertions. On failure,
+ test_grep prints the contents of <file> for easier debugging,
+ whereas a bare 'grep' would fail silently.
+
+ For negation, pass '!' as the first argument:
+
+ test_grep ! "^diff --git" actual
+
+ Do not negate by writing '! test_grep', as that suppresses the
+ diagnostic output.
+
+ test_grep should only be used as a test assertion. When grep
+ is used as a data filter (e.g. 'grep -v "^index" actual >filtered')
+ or inside a command substitution (e.g. '$(grep -c ...)'), plain
+ 'grep' is the right choice because the exit code is not the
+ assertion itself.
+
+ test_grep requires <file> to exist and will BUG otherwise, so
+ use it only where the file is guaranteed to exist at that point.
+ When a file's presence is conditional (a backend-specific file,
+ or a path that only exists on some platforms, such as an NTFS
+ 8.3 short name), guard the assertion on that condition (a
+ prerequisite, or a 'test -e' on the path) and use test_grep
+ inside the guard:
+
+ if test_have_prereq REFFILES
+ then
+ test_grep ! "$refname" .git/packed-refs
+ fi
+
- test_path_is_file <path>
test_path_is_dir <path>
test_path_is_missing <path>
--
gitgitgadget
next prev parent reply other threads:[~2026-07-06 5:02 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-04 7:45 [PATCH 0/6] t: add lint-style.pl and convert grep to test_grep Michael Montalbo via GitGitGadget
2026-06-04 7:45 ` [PATCH 1/6] t/README: document test_grep helper Michael Montalbo via GitGitGadget
2026-06-04 7:45 ` [PATCH 2/6] t: extract chainlint's parser into shared module Michael Montalbo via GitGitGadget
2026-06-04 7:45 ` [PATCH 3/6] t: fix Lexer line count for $() inside double-quoted strings Michael Montalbo via GitGitGadget
2026-06-04 7:45 ` [PATCH 4/6] t: add lint-style.pl with test_grep negation rule Michael Montalbo via GitGitGadget
2026-06-04 18:34 ` D. Ben Knoble
2026-06-04 19:36 ` Michael Montalbo
2026-06-04 7:45 ` [PATCH 5/6] t: fix grep assertions missing file arguments Michael Montalbo via GitGitGadget
2026-06-04 7:45 ` [PATCH 6/6] t: lint and convert grep assertions to test_grep Michael Montalbo via GitGitGadget
2026-06-08 21:36 ` [PATCH 0/6] t: add lint-style.pl and convert grep " Junio C Hamano
2026-06-13 16:28 ` Michael Montalbo
2026-06-13 4:06 ` [PATCH v2 0/6] t: add greplint.pl " Michael Montalbo via GitGitGadget
2026-06-13 4:06 ` [PATCH v2 1/6] t/README: document test_grep helper Michael Montalbo via GitGitGadget
2026-06-13 4:06 ` [PATCH v2 2/6] t: fix grep assertions missing file arguments Michael Montalbo via GitGitGadget
2026-06-13 4:06 ` [PATCH v2 3/6] t: extract chainlint's parser into shared module Michael Montalbo via GitGitGadget
2026-06-13 4:06 ` [PATCH v2 4/6] t: fix Lexer line count for $() inside double-quoted strings Michael Montalbo via GitGitGadget
2026-06-13 4:06 ` [PATCH v2 5/6] t: convert grep assertions to test_grep Michael Montalbo via GitGitGadget
2026-06-27 7:08 ` SZEDER Gábor
2026-06-27 14:36 ` Junio C Hamano
2026-06-28 1:41 ` Junio C Hamano
2026-06-28 2:03 ` Junio C Hamano
2026-06-29 21:21 ` Junio C Hamano
2026-07-02 4:14 ` Michael Montalbo
2026-06-13 4:06 ` [PATCH v2 6/6] t: add greplint to detect bare grep assertions Michael Montalbo via GitGitGadget
2026-07-03 4:54 ` [PATCH v3 0/6] t: add greplint.pl and convert grep to test_grep Michael Montalbo via GitGitGadget
2026-07-03 4:54 ` [PATCH v3 1/6] t/README: document test_grep helper Michael Montalbo via GitGitGadget
2026-07-03 4:54 ` [PATCH v3 2/6] t: fix grep assertions missing file arguments Michael Montalbo via GitGitGadget
2026-07-03 4:54 ` [PATCH v3 3/6] t: extract chainlint's parser into shared module Michael Montalbo via GitGitGadget
2026-07-03 4:54 ` [PATCH v3 4/6] t: fix Lexer line count for $() inside double-quoted strings Michael Montalbo via GitGitGadget
2026-07-03 4:54 ` [PATCH v3 5/6] t: convert grep assertions to test_grep Michael Montalbo via GitGitGadget
2026-07-03 4:54 ` [PATCH v3 6/6] t: add greplint to detect bare grep assertions Michael Montalbo via GitGitGadget
2026-07-05 1:38 ` [PATCH v3 0/6] t: add greplint.pl and convert grep to test_grep Junio C Hamano
2026-07-05 2:49 ` Michael Montalbo
2026-07-06 5:01 ` [PATCH v4 " Michael Montalbo via GitGitGadget
2026-07-06 5:01 ` Michael Montalbo via GitGitGadget [this message]
2026-07-06 5:01 ` [PATCH v4 2/6] t: fix grep assertions missing file arguments Michael Montalbo via GitGitGadget
2026-07-06 5:01 ` [PATCH v4 3/6] t: extract chainlint's parser into shared module Michael Montalbo via GitGitGadget
2026-07-06 5:01 ` [PATCH v4 4/6] t: fix Lexer line count for $() inside double-quoted strings Michael Montalbo via GitGitGadget
2026-07-06 5:01 ` [PATCH v4 5/6] t: convert grep assertions to test_grep Michael Montalbo via GitGitGadget
2026-07-06 5:01 ` [PATCH v4 6/6] t: add greplint to detect bare grep assertions Michael Montalbo via GitGitGadget
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=44d5db91bc3e951a6d2e34acc545b7093783bdaf.1783314119.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=ben.knoble@gmail.com \
--cc=git@vger.kernel.org \
--cc=mmontalbo@gmail.com \
--cc=sunshine@sunshineco.com \
--cc=szeder.dev@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.