diff mbox series

[2/2] t0030-t0050: avoid pipes with Git on LHS

Message ID 20220224054720.23996-3-shivam828787@gmail.com (mailing list archive)
State New, archived
Headers show
Series Subject: [PATCH 0/2] [GSoC][PATCH 0/2] t0000-t0050: avoid pipes with Git on LHS | expand

Commit Message

Shubham Mishra Feb. 24, 2022, 5:47 a.m. UTC
Pipes ignore error codes of LHS command and thus we should not use
them with Git in tests. As an alternative, use a 'tmp' file to write
the Git output so we can test the exit code.

Signed-off-by: Shubham Mishra <shivam828787@gmail.com>
---
 t/t0030-stripspace.sh | 81 ++++++++++++++++++++++++++++---------------
 t/t0050-filesystem.sh |  3 +-
 2 files changed, 56 insertions(+), 28 deletions(-)

Comments

Ævar Arnfjörð Bjarmason Feb. 24, 2022, 8:59 a.m. UTC | #1
On Thu, Feb 24 2022, Shubham Mishra wrote:

> Pipes ignore error codes of LHS command and thus we should not use
> them with Git in tests. As an alternative, use a 'tmp' file to write
> the Git output so we can test the exit code.
>
> Signed-off-by: Shubham Mishra <shivam828787@gmail.com>
> ---
>  t/t0030-stripspace.sh | 81 ++++++++++++++++++++++++++++---------------
>  t/t0050-filesystem.sh |  3 +-
>  2 files changed, 56 insertions(+), 28 deletions(-)
>
> diff --git a/t/t0030-stripspace.sh b/t/t0030-stripspace.sh
> index ae1ca380c1..4d0af9bf98 100755
> --- a/t/t0030-stripspace.sh
> +++ b/t/t0030-stripspace.sh
> @@ -225,32 +225,48 @@ test_expect_success \
>  
>  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
> +    printf "$ttt" | git stripspace >tmp &&
> +    test $(wc -l <tmp) -gt 0 &&
> +    printf "$ttt$ttt" | git stripspace >tmp &&
> +    test $(wc -l <tmp) -gt 0 &&
> +    printf "$ttt$ttt$ttt" | git stripspace >tmp &&
> +    test $(wc -l <tmp) -gt 0 &&
> +    printf "$ttt$ttt$ttt$ttt" | git stripspace >tmp &&
> +    test $(wc -l <tmp) -gt 0
>  '

You're modifying some tests here that are using some old coding style,
so maybe it's better to adjust it while we're at it?

Also I think this would be a lot nicer with test_stdout_line_count and a
helper to deal with that pritnf, e.g.:
	
	diff --git a/t/t0030-stripspace.sh b/t/t0030-stripspace.sh
	index ae1ca380c1a..d48a3579511 100755
	--- a/t/t0030-stripspace.sh
	+++ b/t/t0030-stripspace.sh
	@@ -223,12 +223,15 @@ 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
	+printf_git_stripspace () {
	+	printf "$1" | git stripspace
	+}
	+
	+test_expect_success 'text without newline at end should end with newline' '
	+	test_stdout_line_count -gt 0 printf_git_stripspace "$ttt" &&
	+	test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt" &&
	+	test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$ttt" &&
	+	test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$ttt$ttt"
	 '
	 
	 # text plus spaces at the end:

>  # text plus spaces at the end:
>  
>  test_expect_success \
>      'text plus spaces without newline at end should end with newline' '
> -    test $(printf "$ttt$sss" | git stripspace | wc -l) -gt 0 &&
> -    test $(printf "$ttt$ttt$sss" | git stripspace | wc -l) -gt 0 &&
> -    test $(printf "$ttt$ttt$ttt$sss" | git stripspace | wc -l) -gt 0 &&
> -    test $(printf "$ttt$sss$sss" | git stripspace | wc -l) -gt 0 &&
> -    test $(printf "$ttt$ttt$sss$sss" | git stripspace | wc -l) -gt 0 &&
> -    test $(printf "$ttt$sss$sss$sss" | git stripspace | wc -l) -gt 0
> +    printf "$ttt$sss" | git stripspace >tmp &&
> +    test $(wc -l <tmp) -gt 0 &&
> +    printf "$ttt$ttt$sss" | git stripspace >tmp &&
> +    test $(wc -l <tmp) -gt 0 &&
> +    printf "$ttt$ttt$ttt$sss" | git stripspace >tmp &&
> +    test $(wc -l <tmp) -gt 0 &&
> +    printf "$ttt$sss$sss" | git stripspace >tmp &&
> +    test $(wc -l <tmp) -gt 0 &&
> +    printf "$ttt$ttt$sss$sss" | git stripspace >tmp &&
> +    test $(wc -l <tmp) -gt 0 &&
> +    printf "$ttt$sss$sss$sss" | git stripspace >tmp &&
> +    test $(wc -l <tmp) -gt 0
>  '

ditto.

>  test_expect_success \
>      'text plus spaces without newline at end should not show spaces' '
> -    ! (printf "$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
> -    ! (printf "$ttt$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
> -    ! (printf "$ttt$ttt$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
> -    ! (printf "$ttt$sss$sss" | git stripspace | grep "  " >/dev/null) &&
> -    ! (printf "$ttt$ttt$sss$sss" | git stripspace | grep "  " >/dev/null) &&
> -    ! (printf "$ttt$sss$sss$sss" | git stripspace | grep "  " >/dev/null)
> +    printf "$ttt$sss" | git stripspace >tmp &&
> +    ! (grep "  " tmp >/dev/null) &&
> +    printf "$ttt$ttt$sss" | git stripspace &&
> +    ! (grep "  " tmp >/dev/null) &&
> +    printf "$ttt$ttt$ttt$sss" | git stripspace &&
> +    ! (grep "  " tmp >/dev/nul) &&
> +    printf "$ttt$sss$sss" | git stripspace &&
> +    ! (grep "  " tmp >/dev/null) &&
> +    printf "$ttt$ttt$sss$sss" | git stripspace &&
> +    ! (grep "  " tmp >/dev/null) &&
> +    printf "$ttt$sss$sss$sss" | git stripspace &&
> +    ! (grep "  " tmp >/dev/null)
>  '

This is not on you, but generally we don't pipe "grep" to >/dev/null,
and just let the --verbose option do its work.

With/without that change you no longer need the () subshell here.

>  test_expect_success \
> @@ -282,12 +298,18 @@ test_expect_success \
>  
>  test_expect_success \
>      'text plus spaces at end should not show spaces' '
> -    ! (echo "$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
> -    ! (echo "$ttt$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
> -    ! (echo "$ttt$ttt$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
> -    ! (echo "$ttt$sss$sss" | git stripspace | grep "  " >/dev/null) &&
> -    ! (echo "$ttt$ttt$sss$sss" | git stripspace | grep "  " >/dev/null) &&
> -    ! (echo "$ttt$sss$sss$sss" | git stripspace | grep "  " >/dev/null)
> +    echo "$ttt$sss" | git stripspace >tmp &&
> +    ! (grep "  " tmp >/dev/null) &&
> +    echo "$ttt$ttt$sss" | git stripspace >tmp &&
> +    ! (grep "  " tmp>/dev/null) &&
> +    echo "$ttt$ttt$ttt$sss" &&
> +    ! (grep "  " tmp >/dev/null) &&
> +    echo "$ttt$sss$sss" | git stripspace >tmp &&
> +    ! (grep "  " tmp >/dev/null) &&
> +    echo "$ttt$ttt$sss$sss" | git stripspace >tmp &&
> +    ! (grep "  " tmp >/dev/null) &&
> +    echo "$ttt$sss$sss$sss" | git stripspace >tmp &&
> +    ! (grep "  " tmp >/dev/null)
>  '
>  
>  test_expect_success \
> @@ -339,11 +361,16 @@ test_expect_success \
>  
>  test_expect_success \
>      'spaces without newline at end should not show spaces' '
> -    ! (printf "" | git stripspace | grep " " >/dev/null) &&
> -    ! (printf "$sss" | git stripspace | grep " " >/dev/null) &&
> -    ! (printf "$sss$sss" | git stripspace | grep " " >/dev/null) &&
> -    ! (printf "$sss$sss$sss" | git stripspace | grep " " >/dev/null) &&
> -    ! (printf "$sss$sss$sss$sss" | git stripspace | grep " " >/dev/null)
> +    printf "" | git stripspace >tmp &&
> +    ! ( grep " " tmp >/dev/null) &&
> +    printf "$sss" | git stripspace >tmp &&
> +    ! ( grep " " tmp >/dev/null) &&
> +    printf "$sss$sss" | git stripspace >tmp &&
> +    ! (grep " " tmp >/dev/null) &&
> +    printf "$sss$sss$sss" | git stripspace >tmp &&
> +    ! (grep " " tmp >/dev/null) &&
> +    printf "$sss$sss$sss$sss" | git stripspace >tmp &&
> +    ! (grep " " tmp >/dev/null)
>  '

ditto for these.

>  test_expect_success \
> diff --git a/t/t0050-filesystem.sh b/t/t0050-filesystem.sh
> index afc343cf9b..5c9dc90d0b 100755
> --- a/t/t0050-filesystem.sh
> +++ b/t/t0050-filesystem.sh
> @@ -104,7 +104,8 @@ test_expect_failure CASE_INSENSITIVE_FS 'add (with different case)' '
>  	rm camelcase &&
>  	echo 1 >CamelCase &&
>  	git add CamelCase &&
> -	camel=$(git ls-files | grep -i camelcase) &&
> +	git ls-files >tmp &&
> +	camel=$(grep -i camelcase tmp) &&
>  	test $(echo "$camel" | wc -l) = 1 &&
>  	test "z$(git cat-file blob :$camel)" = z1
>  '
Shubham Mishra Feb. 26, 2022, 12:09 p.m. UTC | #2
Hi,

Thanks for Reviewing.

> You're modifying some tests here that are using some old coding style,
> so maybe it's better to adjust it while we're at it?
>
> Also I think this would be a lot nicer with test_stdout_line_count and a
> helper to deal with that pritnf, e.g.:
>
>         diff --git a/t/t0030-stripspace.sh b/t/t0030-stripspace.sh
>         index ae1ca380c1a..d48a3579511 100755
>         --- a/t/t0030-stripspace.sh
>         +++ b/t/t0030-stripspace.sh
>         @@ -223,12 +223,15 @@ 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
>         +printf_git_stripspace () {
>         +       printf "$1" | git stripspace
>         +}
>         +
>         +test_expect_success 'text without newline at end should end with newline' '
>         +       test_stdout_line_count -gt 0 printf_git_stripspace "$ttt" &&
>         +       test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt" &&
>         +       test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$ttt" &&
>         +       test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$ttt$ttt"
>          '
>
>          # text plus spaces at the end:
>

it makes sense to improve this code as we are touching it, this way
looks much better. I will implement it.


> This is not on you, but generally we don't pipe "grep" to >/dev/null,
> and just let the --verbose option do its work.
I don't think I understood this, I guess you are talking about the
"-v" flag that stands for invert match? I didn't find "--verbose" with
grep.
Please correct me if I am wrong.

> With/without that change you no longer need the () subshell here.
sure.

Thanks,
Shubham
Taylor Blau Feb. 26, 2022, 5:32 p.m. UTC | #3
On Thu, Feb 24, 2022 at 09:59:45AM +0100, Ævar Arnfjörð Bjarmason wrote:
> You're modifying some tests here that are using some old coding style,
> so maybe it's better to adjust it while we're at it?
>
> Also I think this would be a lot nicer with test_stdout_line_count and a
> helper to deal with that pritnf, e.g.:

Thanks, Ævar for the quality review. I have nothing to add here.

Thanks,
Taylor
diff mbox series

Patch

diff --git a/t/t0030-stripspace.sh b/t/t0030-stripspace.sh
index ae1ca380c1..4d0af9bf98 100755
--- a/t/t0030-stripspace.sh
+++ b/t/t0030-stripspace.sh
@@ -225,32 +225,48 @@  test_expect_success \
 
 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
+    printf "$ttt" | git stripspace >tmp &&
+    test $(wc -l <tmp) -gt 0 &&
+    printf "$ttt$ttt" | git stripspace >tmp &&
+    test $(wc -l <tmp) -gt 0 &&
+    printf "$ttt$ttt$ttt" | git stripspace >tmp &&
+    test $(wc -l <tmp) -gt 0 &&
+    printf "$ttt$ttt$ttt$ttt" | git stripspace >tmp &&
+    test $(wc -l <tmp) -gt 0
 '
 
 # text plus spaces at the end:
 
 test_expect_success \
     'text plus spaces without newline at end should end with newline' '
-    test $(printf "$ttt$sss" | git stripspace | wc -l) -gt 0 &&
-    test $(printf "$ttt$ttt$sss" | git stripspace | wc -l) -gt 0 &&
-    test $(printf "$ttt$ttt$ttt$sss" | git stripspace | wc -l) -gt 0 &&
-    test $(printf "$ttt$sss$sss" | git stripspace | wc -l) -gt 0 &&
-    test $(printf "$ttt$ttt$sss$sss" | git stripspace | wc -l) -gt 0 &&
-    test $(printf "$ttt$sss$sss$sss" | git stripspace | wc -l) -gt 0
+    printf "$ttt$sss" | git stripspace >tmp &&
+    test $(wc -l <tmp) -gt 0 &&
+    printf "$ttt$ttt$sss" | git stripspace >tmp &&
+    test $(wc -l <tmp) -gt 0 &&
+    printf "$ttt$ttt$ttt$sss" | git stripspace >tmp &&
+    test $(wc -l <tmp) -gt 0 &&
+    printf "$ttt$sss$sss" | git stripspace >tmp &&
+    test $(wc -l <tmp) -gt 0 &&
+    printf "$ttt$ttt$sss$sss" | git stripspace >tmp &&
+    test $(wc -l <tmp) -gt 0 &&
+    printf "$ttt$sss$sss$sss" | git stripspace >tmp &&
+    test $(wc -l <tmp) -gt 0
 '
 
 test_expect_success \
     'text plus spaces without newline at end should not show spaces' '
-    ! (printf "$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
-    ! (printf "$ttt$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
-    ! (printf "$ttt$ttt$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
-    ! (printf "$ttt$sss$sss" | git stripspace | grep "  " >/dev/null) &&
-    ! (printf "$ttt$ttt$sss$sss" | git stripspace | grep "  " >/dev/null) &&
-    ! (printf "$ttt$sss$sss$sss" | git stripspace | grep "  " >/dev/null)
+    printf "$ttt$sss" | git stripspace >tmp &&
+    ! (grep "  " tmp >/dev/null) &&
+    printf "$ttt$ttt$sss" | git stripspace &&
+    ! (grep "  " tmp >/dev/null) &&
+    printf "$ttt$ttt$ttt$sss" | git stripspace &&
+    ! (grep "  " tmp >/dev/nul) &&
+    printf "$ttt$sss$sss" | git stripspace &&
+    ! (grep "  " tmp >/dev/null) &&
+    printf "$ttt$ttt$sss$sss" | git stripspace &&
+    ! (grep "  " tmp >/dev/null) &&
+    printf "$ttt$sss$sss$sss" | git stripspace &&
+    ! (grep "  " tmp >/dev/null)
 '
 
 test_expect_success \
@@ -282,12 +298,18 @@  test_expect_success \
 
 test_expect_success \
     'text plus spaces at end should not show spaces' '
-    ! (echo "$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
-    ! (echo "$ttt$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
-    ! (echo "$ttt$ttt$ttt$sss" | git stripspace | grep "  " >/dev/null) &&
-    ! (echo "$ttt$sss$sss" | git stripspace | grep "  " >/dev/null) &&
-    ! (echo "$ttt$ttt$sss$sss" | git stripspace | grep "  " >/dev/null) &&
-    ! (echo "$ttt$sss$sss$sss" | git stripspace | grep "  " >/dev/null)
+    echo "$ttt$sss" | git stripspace >tmp &&
+    ! (grep "  " tmp >/dev/null) &&
+    echo "$ttt$ttt$sss" | git stripspace >tmp &&
+    ! (grep "  " tmp>/dev/null) &&
+    echo "$ttt$ttt$ttt$sss" &&
+    ! (grep "  " tmp >/dev/null) &&
+    echo "$ttt$sss$sss" | git stripspace >tmp &&
+    ! (grep "  " tmp >/dev/null) &&
+    echo "$ttt$ttt$sss$sss" | git stripspace >tmp &&
+    ! (grep "  " tmp >/dev/null) &&
+    echo "$ttt$sss$sss$sss" | git stripspace >tmp &&
+    ! (grep "  " tmp >/dev/null)
 '
 
 test_expect_success \
@@ -339,11 +361,16 @@  test_expect_success \
 
 test_expect_success \
     'spaces without newline at end should not show spaces' '
-    ! (printf "" | git stripspace | grep " " >/dev/null) &&
-    ! (printf "$sss" | git stripspace | grep " " >/dev/null) &&
-    ! (printf "$sss$sss" | git stripspace | grep " " >/dev/null) &&
-    ! (printf "$sss$sss$sss" | git stripspace | grep " " >/dev/null) &&
-    ! (printf "$sss$sss$sss$sss" | git stripspace | grep " " >/dev/null)
+    printf "" | git stripspace >tmp &&
+    ! ( grep " " tmp >/dev/null) &&
+    printf "$sss" | git stripspace >tmp &&
+    ! ( grep " " tmp >/dev/null) &&
+    printf "$sss$sss" | git stripspace >tmp &&
+    ! (grep " " tmp >/dev/null) &&
+    printf "$sss$sss$sss" | git stripspace >tmp &&
+    ! (grep " " tmp >/dev/null) &&
+    printf "$sss$sss$sss$sss" | git stripspace >tmp &&
+    ! (grep " " tmp >/dev/null)
 '
 
 test_expect_success \
diff --git a/t/t0050-filesystem.sh b/t/t0050-filesystem.sh
index afc343cf9b..5c9dc90d0b 100755
--- a/t/t0050-filesystem.sh
+++ b/t/t0050-filesystem.sh
@@ -104,7 +104,8 @@  test_expect_failure CASE_INSENSITIVE_FS 'add (with different case)' '
 	rm camelcase &&
 	echo 1 >CamelCase &&
 	git add CamelCase &&
-	camel=$(git ls-files | grep -i camelcase) &&
+	git ls-files >tmp &&
+	camel=$(grep -i camelcase tmp) &&
 	test $(echo "$camel" | wc -l) = 1 &&
 	test "z$(git cat-file blob :$camel)" = z1
 '