git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] t5304: use helper to report failure of "test foo = bar"

From
Jeff King <peff@peff.net>
Date
Oct 10, 2014, 06:11 UTC
Message-ID
<20141010061114.GB15277@peff.net>
In-Reply-To
<20141010060636.GA15057@peff.net>
For small outputs, we sometimes use:
  test "$(some_cmd)" = "something we expect"

instead of a full test_cmp. The downside of this is that when it fails, there is no output at all from the script. Let's introduce a small helper to make tests easier to debug.

Signed-off-by: Jeff King <peff@peff.net>
---
I kind of like the elegance of this, but I'd also be OK with dropping
it. The solution from the third patch does the same thing. And while the
output isn't quite as nice, it magically works for all existing tests,
without having to convert them to use verbose().

I wondered if there was any problem with defining a function with the same name as a variable ($verbose). A few weeks ago, I would have said they were completely different namespaces, but after seeing the shellshock vulnerability I am not so sure. :) This seems to work fine in bash and dash, but maybe there is some more obscure shell that would not like it?

Technically this function is the opposite of test_must_fail. We could call it test_must_succeed, but that is a little wordy for my taste (I actually considered calling it just v() if we are going to use it a lot, but that may be going too far).

 t/t5304-prune.sh        | 16 ++++++++--------
 t/test-lib-functions.sh |  9 +++++++++
 2 files changed, 17 insertions(+), 8 deletions(-)
diff --git a/t/t5304-prune.sh b/t/t5304-prune.sh
index b0ffb05..e32e46d 100755
--- a/t/t5304-prune.sh
+++ b/t/t5304-prune.sh
@@ -13,7 +13,7 @@ add_blob() {
 	before=$(git count-objects | sed "s/ .*//") &&
 	BLOB=$(echo aleph_0 | git hash-object -w --stdin) &&
 	BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
-	test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
+	verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
 	test_path_is_file $BLOB_FILE &&
 	test-chmtime =+0 $BLOB_FILE
 }
@@ -45,11 +45,11 @@ test_expect_success 'prune --expire' '
 
 	add_blob &&
 	git prune --expire=1.hour.ago &&
-	test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
+	verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
 	test_path_is_file $BLOB_FILE &&
 	test-chmtime =-86500 $BLOB_FILE &&
 	git prune --expire 1.day &&
-	test $before = $(git count-objects | sed "s/ .*//") &&
+	verbose test $before = $(git count-objects | sed "s/ .*//") &&
 	test_path_is_missing $BLOB_FILE
 
 '
@@ -59,11 +59,11 @@ test_expect_success 'gc: implicit prune --expire' '
 	add_blob &&
 	test-chmtime =-$((2*$week-30)) $BLOB_FILE &&
 	git gc &&
-	test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
+	verbose test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
 	test_path_is_file $BLOB_FILE &&
 	test-chmtime =-$((2*$week+1)) $BLOB_FILE &&
 	git gc &&
-	test $before = $(git count-objects | sed "s/ .*//") &&
+	verbose test $before = $(git count-objects | sed "s/ .*//") &&
 	test_path_is_missing $BLOB_FILE
 
 '
@@ -144,7 +144,7 @@ test_expect_success 'gc --no-prune' '
 	test-chmtime =-$((5001*$day)) $BLOB_FILE &&
 	git config gc.pruneExpire 2.days.ago &&
 	git gc --no-prune &&
-	test 1 = $(git count-objects | sed "s/ .*//") &&
+	verbose test 1 = $(git count-objects | sed "s/ .*//") &&
 	test_path_is_file $BLOB_FILE
 
 '
@@ -209,10 +209,10 @@ test_expect_success 'gc: prune old objects after local clone' '
 	git clone --no-hardlinks . aclone &&
 	(
 		cd aclone &&
-		test 1 = $(git count-objects | sed "s/ .*//") &&
+		verbose test 1 = $(git count-objects | sed "s/ .*//") &&
 		test_path_is_file $BLOB_FILE &&
 		git gc --prune &&
-		test 0 = $(git count-objects | sed "s/ .*//") &&
+		verbose test 0 = $(git count-objects | sed "s/ .*//") &&
 		test_path_is_missing $BLOB_FILE
 	)
 '
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index dafd6ad..b7957b8 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -634,6 +634,15 @@ test_cmp_bin() {
 	cmp "$@"
 }
 
+# Call any command "$@" but be more verbose about its
+# failure. This is handy for commands like "test" which do
+# not output anything when they fail.
+verbose () {
+	"$@" && return 0
+	echo >&2 "command failed: $(git rev-parse --sq-quote "$@")"
+	return 1
+}
+
 # Check if the file expected to be empty is indeed empty, and barfs
 # otherwise.
 
-- 
2.1.2.596.g7379948
Previous: Jeff KingNext: Jonathan Nieder
Message 3 of 26 in “"-x" tracing option for tests”
  1. 0/3 "-x" tracing option for testsJeff King, Oct 10, 2014
  2. 1/3 t5304: use test_path_is_* instead of "test -f"Jeff King, Oct 10, 2014
  3. 2/3 t5304: use helper to report failure of "test foo = bar"Jeff King, Oct 10, 2014
  4. Jonathan NiederOct 13, 2014
  5. Jeff KingOct 13, 2014
  6. Jonathan NiederOct 13, 2014
  7. Junio C HamanoOct 13, 2014
  8. Jonathan NiederOct 13, 2014
  9. Junio C HamanoOct 13, 2014
  10. Jeff KingOct 13, 2014
  11. 3/3 test-lib.sh: support -x option for shell-tracingJeff King, Oct 10, 2014
  12. Jeff KingOct 10, 2014
  13. Jeff KingOct 10, 2014
  14. 3/3 test-lib.sh: support -x option for shell-tracingJeff King, Oct 10, 2014
  15. Junio C HamanoOct 13, 2014
  16. Junio C HamanoOct 13, 2014
  17. Junio C HamanoOct 13, 2014
  18. Jeff KingOct 13, 2014
  19. Junio C HamanoOct 13, 2014
  20. Jeff KingOct 13, 2014
  21. Junio C HamanoOct 13, 2014
  22. Jeff KingOct 14, 2014
  23. Junio C HamanoOct 13, 2014
  24. Jeff KingOct 13, 2014
  25. Michael HaggertyOct 14, 2014
  26. Jeff KingOct 14, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.