Subject: Re: [PATCH v3 1/9] t5520: fixup file contents comparisons

From: Junio C Hamano <[email-masked]>
Date: Fri, 15 May 2015 12:22:00 -0700

Junio C Hamano <[email-masked]> writes:

> You might have noticed, while adding them, there were something
> common that we currently do with a bare 'test' only because we
> haven't identified common needs....
> ...
> in t/test-lib-functions.sh and convert them to say
>
> 	test_file_contents file expected_string
>
> That would be an improvement (and that is the remaining 2/3 ;-).

I haven't made up my mind on this other example, but since I started
writing it...

It may be that counting the number of lines in output, "cmd | wc -l",
is a common pattern.  We already have test_line_count to check the
number of lines in a file, but having test_output_count may help.

 t/test-lib-functions.sh | 11 +++++++++++
 t/t0000-basic.sh        |  3 +--
 t/t0030-stripspace.sh   | 11 +++++------
 3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 0d93e33..624a8c5 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -538,6 +538,17 @@ test_line_count () {
 	fi
 }
 
+test_output_count () {
+	if test $# != 3
+	then
+		error "bug in the test script: not 3 parameters to test_output_count"
+	elif ! test $(eval "$2" | wc -l) "$1" "$3"
+	then
+		echo "test_output_count: line count for output from '$2' !$1 $3"
+		return 1
+	fi
+}
+
 # This is not among top-level (test_expect_success | test_expect_failure)
 # but is a prefix that can be used in the test script, like:
 #

diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh
index f10ba4a..bd5930d 100755
--- a/t/t0000-basic.sh
+++ b/t/t0000-basic.sh
@@ -1039,8 +1039,7 @@ test_expect_success 'update-index D/F conflict' '
 	mv path2 path0 &&
 	mv tmp path2 &&
 	git update-index --add --replace path2 path0/file2 &&
-	numpath0=$(git ls-files path0 | wc -l) &&
-	test $numpath0 = 1
+	test_output_count = "git ls-files path0" 1
 '
 
 test_expect_success 'very long name in the index handled sanely' '
diff --git a/t/t0030-stripspace.sh b/t/t0030-stripspace.sh
index 0333dd9..9502938 100755
--- a/t/t0030-stripspace.sh
+++ b/t/t0030-stripspace.sh
@@ -223,12 +223,11 @@ test_expect_success \
     test_cmp expect actual
 '
 
-test_expect_success \
-    'text without newline at end should end with newline' '
-    test $(printf "$ttt" | git stripspace | wc -l) -gt 0 &&
-    test $(printf "$ttt$ttt" | git stripspace | wc -l) -gt 0 &&
-    test $(printf "$ttt$ttt$ttt" | git stripspace | wc -l) -gt 0 &&
-    test $(printf "$ttt$ttt$ttt$ttt" | git stripspace | wc -l) -gt 0
+test_expect_success 'text without newline at end should end with newline' '
+	test_output_count -gt '\''printf "$ttt" | git stripspace'\'' 0 &&
+	test_output_count -gt '\''printf "$ttt$ttt" | git stripspace'\'' 0 &&
+	test_output_count -gt '\''printf "$ttt$ttt$ttt" | git stripspace'\'' 0 &&
+	test_output_count -gt '\''printf "$ttt$ttt$ttt$ttt" | git stripspace'\'' 0
 '
 
 # text plus spaces at the end: