Subject: [PATCH v2] t3700: avoid hidden failures and use test_grep helper

From: Siddharth Shrimali <[email-masked]>
Date: Sat, 28 Feb 2026 12:30:20 +0530

Replace pipelines involving git commands with temporary files to ensure
that any crashes or unexpected exit codes from the git commands are
properly caught by the test suite. A simple pipeline like
'git foo | grep bar' ignores the exit code of 'git', which can
hide regressions.

Additionally, replace standard 'grep' with the 'test_grep' helper.
This improves debuggability by automatically dumping the contents of
the 'actual' file when a match is not found. In cases where we were
counting lines with 'wc -l' to ensure a pattern was absent,
simplify to 'test_grep !'.

Suggested-by: Junio C Hamano <[email-masked]>

Signed-off-by: Siddharth Shrimali <[email-masked]>
---
Inter-patch notes (v1 -> v2):
 - Replaced standard 'grep' with 'test_grep' for better diagnostics.
 - Used 'test_grep !' for negative assertions as per project style.
 - Simplified 'wc -l' logic to 'test_grep !' to avoid subshells.
 - Removed unnecessary '<' redirection from function calls.

 t/t3700-add.sh | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/t/t3700-add.sh b/t/t3700-add.sh
index 66c6114b54..d61bf784d2 100755
--- a/t/t3700-add.sh
+++ b/t/t3700-add.sh
@@ -39,7 +39,7 @@ test_expect_success 'Test with no pathspecs' '
 
 test_expect_success 'Post-check that foo is in the index' '
 	git ls-files foo >actual &&
-	grep foo <actual
+	test_grep foo actual
 '
 
 test_expect_success 'Test that "git add -- -q" works' '
@@ -197,8 +197,8 @@ test_expect_success 'git add with filemode=0, symlinks=0, and unmerged entries'
 	echo new > symlink &&
 	git add file symlink &&
 	git ls-files --stage >actual &&
-	grep "^100755 .* 0	file$" <actual &&
-	grep "^120000 .* 0	symlink$" actual
+	test_grep "^100755 .* 0	file$" actual &&
+	test_grep "^120000 .* 0	symlink$" actual
 '
 
 test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
@@ -215,8 +215,8 @@ test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over st
 	echo new > symlink &&
 	git add file symlink &&
 	git ls-files --stage >actual &&
-	grep "^100755 .* 0	file$" actual &&
-	grep "^120000 .* 0	symlink$" actual
+	test_grep "^100755 .* 0	file$" actual &&
+	test_grep "^120000 .* 0	symlink$" actual
 '
 
 test_expect_success 'git add --refresh' '
@@ -258,7 +258,7 @@ test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unr
 	chmod 0 foo2 &&
 	test_must_fail git add --verbose . &&
 	git ls-files foo1 >actual &&
-	! grep foo1 actual
+	test_grep ! foo1 actual
 '
 
 rm -f foo2
@@ -293,7 +293,7 @@ test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors = false)' '
 	chmod 0 foo2 &&
 	test_must_fail git add --verbose . &&
 	git ls-files foo1 >actual &&
-	! grep foo1 actual
+	test_grep ! foo1 actual
 '
 rm -f foo2
 
@@ -305,7 +305,7 @@ test_expect_success POSIXPERM,SANITY '--no-ignore-errors overrides config' '
 	chmod 0 foo2 &&
 	test_must_fail git add --verbose --no-ignore-errors . &&
 	git ls-files foo1 >actual &&
-	! grep foo1 actual &&
+	test_grep ! foo1 actual &&
 	git config add.ignore-errors 0
 '
 rm -f foo2
@@ -316,7 +316,7 @@ test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" '
 	git add '\''fo\[ou\]bar'\'' &&
 	git ls-files fo\[ou\]bar | grep -F fo\[ou\]bar &&
 	git ls-files foobar >actual &&
-	! grep foobar actual
+	test_grep ! foobar actual
 '
 
 test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
@@ -334,7 +334,7 @@ test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
 test_expect_success '"add non-existent" should fail' '
 	test_must_fail git add non-existent &&
 	git ls-files >actual &&
-	! grep "non-existent" actual 
+	test_grep ! "non-existent" actual 
 '
 
 test_expect_success 'git add -A on empty repo does not error out' '
@@ -544,9 +544,11 @@ test_expect_success 'all statuses changed in folder if . is given' '
 		touch x y z sub/a sub/dir/b &&
 		git add -A &&
 		git add --chmod=+x . &&
-		test $(git ls-files --stage >actual && grep ^100644 actual | wc -l) -eq 0 &&
+		git ls-files --stage >actual &&
+		test_grep ! "^100644" actual &&
 		git add --chmod=-x . &&
-		test $(git ls-files --stage >actual && grep ^100755 actual | wc -l) -eq 0
+		git ls-files --stage >actual &&
+		test_grep ! "^100755" actual
 	)
 '
 
@@ -582,4 +584,4 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
 	git add "$downcased"
 '
 
-test_done
\ No newline at end of file
+test_done
-- 
2.51.2