# [PATCH] t/test-lib.sh: minor readability improvements

5 messages from 2011-04-27 to 2011-04-29. Participants: Mathias Lafeldt, Junio C Hamano.
Thread: https://gitlist.dev/t/27200

## Mathias Lafeldt, 2011-04-27 12:49

Subject: [PATCH] t/test-lib.sh: minor readability improvements
Message-ID: <4DB810E1.3080102@debugon.org>
URL: https://gitlist.dev/e/4DB810E1.3080102%40debugon.org

```
Tweak/apply parameter expansion. Also use here document to save
test results instead of appending each line with ">>".

Signed-off-by: Mathias Lafeldt <misfire@debugon.org>
---
 t/test-lib.sh |   18 ++++++++++--------
 1 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/t/test-lib.sh b/t/test-lib.sh
index abc47f3..b30725f 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -24,7 +24,7 @@ done,*)
 *' --tee '*|*' --va'*)
 	mkdir -p test-results
 	BASE=test-results/$(basename "$0" .sh)
-	(GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
+	(GIT_TEST_TEE_STARTED=done ${SHELL-"sh"} "$0" "$@" 2>&1;
 	 echo $? > $BASE.exit) | tee $BASE.out
 	test "$(cat $BASE.exit)" = 0
 	exit
@@ -575,7 +575,7 @@ test_external () {
 test_external_without_stderr () {
 	# The temporary file has no (and must have no) security
 	# implications.
-	tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
+	tmp=${TMPDIR:-"/tmp"}
 	stderr="$tmp/git-external-stderr.$$.tmp"
 	test_external "$@" 4> "$stderr"
 	[ -f "$stderr" ] || error "Internal error: $stderr disappeared."
@@ -801,12 +801,14 @@ test_done () {
 		mkdir -p "$test_results_dir"
 		test_results_path="$test_results_dir/${0%.sh}-$$.counts"
 
-		echo "total $test_count" >> $test_results_path
-		echo "success $test_success" >> $test_results_path
-		echo "fixed $test_fixed" >> $test_results_path
-		echo "broken $test_broken" >> $test_results_path
-		echo "failed $test_failure" >> $test_results_path
-		echo "" >> $test_results_path
+		cat >> "$test_results_path" <<EOF
+total $test_count
+success $test_success
+fixed $test_fixed
+broken $test_broken
+failed $test_failure
+
+EOF
 	fi
 
 	if test "$test_fixed" != 0
-- 
1.7.5

```

## Junio C Hamano, 2011-04-27 17:28

Subject: Re: [PATCH] t/test-lib.sh: minor readability improvements
Message-ID: <7vmxjb8uyu.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vmxjb8uyu.fsf%40alter.siamese.dyndns.org
In-Reply-To: <4DB810E1.3080102@debugon.org>

```
Mathias Lafeldt <misfire@debugon.org> writes:

> Tweak/apply parameter expansion. Also use here document to save
> test results instead of appending each line with ">>".

Thanks.  A few minor nits.

> Signed-off-by: Mathias Lafeldt <misfire@debugon.org>
> ---
>  t/test-lib.sh |   18 ++++++++++--------
>  1 files changed, 10 insertions(+), 8 deletions(-)
>
> diff --git a/t/test-lib.sh b/t/test-lib.sh
> index abc47f3..b30725f 100644
> --- a/t/test-lib.sh
> +++ b/t/test-lib.sh
> @@ -24,7 +24,7 @@ done,*)
>  *' --tee '*|*' --va'*)
>  	mkdir -p test-results
>  	BASE=test-results/$(basename "$0" .sh)
> -	(GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
> +	(GIT_TEST_TEE_STARTED=done ${SHELL-"sh"} "$0" "$@" 2>&1;

Looks unnecessary.  Superstition?

>  	 echo $? > $BASE.exit) | tee $BASE.out
>  	test "$(cat $BASE.exit)" = 0
>  	exit
> @@ -575,7 +575,7 @@ test_external () {
>  test_external_without_stderr () {
>  	# The temporary file has no (and must have no) security
>  	# implications.
> -	tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
> +	tmp=${TMPDIR:-"/tmp"}
>  	stderr="$tmp/git-external-stderr.$$.tmp"
>  	test_external "$@" 4> "$stderr"
>  	[ -f "$stderr" ] || error "Internal error: $stderr disappeared."
> @@ -801,12 +801,14 @@ test_done () {
>  		mkdir -p "$test_results_dir"
>  		test_results_path="$test_results_dir/${0%.sh}-$$.counts"
>  
> -		echo "total $test_count" >> $test_results_path
> -		echo "success $test_success" >> $test_results_path
> -		echo "fixed $test_fixed" >> $test_results_path
> -		echo "broken $test_broken" >> $test_results_path
> -		echo "failed $test_failure" >> $test_results_path
> -		echo "" >> $test_results_path
> +		cat >> "$test_results_path" <<EOF
> +total $test_count
> +success $test_success
> +fixed $test_fixed
> +broken $test_broken
> +failed $test_failure
> +
> +EOF

It may probably be even easier to read if you indented the whole thing,
using the dash before the here-doc marker, like so:

	cat >>"$test_results_path" <<-EOF
	total $test_count
        success $test_success
        ...
        EOF

```

## Mathias Lafeldt, 2011-04-29 10:00

Subject: Re: [PATCH] t/test-lib.sh: minor readability improvements
Message-ID: <4DBA8C43.4040804@debugon.org>
URL: https://gitlist.dev/e/4DBA8C43.4040804%40debugon.org
In-Reply-To: <7vmxjb8uyu.fsf@alter.siamese.dyndns.org>

```
On 04/27/2011 07:28 PM, Junio C Hamano wrote:
> Mathias Lafeldt <misfire@debugon.org> writes:
> 
>> Tweak/apply parameter expansion. Also use here document to save
>> test results instead of appending each line with ">>".
> 
> Thanks.  A few minor nits.
> 
>> Signed-off-by: Mathias Lafeldt <misfire@debugon.org>
>> ---
>>  t/test-lib.sh |   18 ++++++++++--------
>>  1 files changed, 10 insertions(+), 8 deletions(-)
>>
>> diff --git a/t/test-lib.sh b/t/test-lib.sh
>> index abc47f3..b30725f 100644
>> --- a/t/test-lib.sh
>> +++ b/t/test-lib.sh
>> @@ -24,7 +24,7 @@ done,*)
>>  *' --tee '*|*' --va'*)
>>  	mkdir -p test-results
>>  	BASE=test-results/$(basename "$0" .sh)
>> -	(GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
>> +	(GIT_TEST_TEE_STARTED=done ${SHELL-"sh"} "$0" "$@" 2>&1;
> 
> Looks unnecessary.  Superstition?

IMHO, ${SHELL-sh} is kind of hard to read, especially when you
don't know about the ${parameter-word} format. In hindsight, I
understand that the change isn't really necessary.
 
>>  	 echo $? > $BASE.exit) | tee $BASE.out
>>  	test "$(cat $BASE.exit)" = 0
>>  	exit
>> @@ -575,7 +575,7 @@ test_external () {
>>  test_external_without_stderr () {
>>  	# The temporary file has no (and must have no) security
>>  	# implications.
>> -	tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
>> +	tmp=${TMPDIR:-"/tmp"}

I guess, you'd prefer ${TMPDIR:-/tmp} here too.

>>  	stderr="$tmp/git-external-stderr.$$.tmp"
>>  	test_external "$@" 4> "$stderr"
>>  	[ -f "$stderr" ] || error "Internal error: $stderr disappeared."
>> @@ -801,12 +801,14 @@ test_done () {
>>  		mkdir -p "$test_results_dir"
>>  		test_results_path="$test_results_dir/${0%.sh}-$$.counts"
>>  
>> -		echo "total $test_count" >> $test_results_path
>> -		echo "success $test_success" >> $test_results_path
>> -		echo "fixed $test_fixed" >> $test_results_path
>> -		echo "broken $test_broken" >> $test_results_path
>> -		echo "failed $test_failure" >> $test_results_path
>> -		echo "" >> $test_results_path
>> +		cat >> "$test_results_path" <<EOF
>> +total $test_count
>> +success $test_success
>> +fixed $test_fixed
>> +broken $test_broken
>> +failed $test_failure
>> +
>> +EOF
> 
> It may probably be even easier to read if you indented the whole thing,
> using the dash before the here-doc marker, like so:
> 
> 	cat >>"$test_results_path" <<-EOF
> 	total $test_count
>         success $test_success
>         ...
>         EOF

OK, I'll be re-rolling the patch using "<<-EOF".

-Mathias

```

## Mathias Lafeldt, 2011-04-29 12:30

Subject: [PATCH v2] t/test-lib.sh: minor readability improvements
Message-ID: <1304080230-11670-1-git-send-email-misfire@debugon.org>
URL: https://gitlist.dev/e/1304080230-11670-1-git-send-email-misfire%40debugon.org
In-Reply-To: <4DBA8C43.4040804@debugon.org>

```
Apply parameter expansion. Also use here document to save
test results instead of appending each line with ">>".

Signed-off-by: Mathias Lafeldt <misfire@debugon.org>
---
 t/test-lib.sh |   16 +++++++++-------
 1 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/t/test-lib.sh b/t/test-lib.sh
index abc47f3..aca03d2 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -575,7 +575,7 @@ test_external () {
 test_external_without_stderr () {
 	# The temporary file has no (and must have no) security
 	# implications.
-	tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
+	tmp=${TMPDIR:-/tmp}
 	stderr="$tmp/git-external-stderr.$$.tmp"
 	test_external "$@" 4> "$stderr"
 	[ -f "$stderr" ] || error "Internal error: $stderr disappeared."
@@ -801,12 +801,14 @@ test_done () {
 		mkdir -p "$test_results_dir"
 		test_results_path="$test_results_dir/${0%.sh}-$$.counts"
 
-		echo "total $test_count" >> $test_results_path
-		echo "success $test_success" >> $test_results_path
-		echo "fixed $test_fixed" >> $test_results_path
-		echo "broken $test_broken" >> $test_results_path
-		echo "failed $test_failure" >> $test_results_path
-		echo "" >> $test_results_path
+		cat >>"$test_results_path" <<-EOF
+		total $test_count
+		success $test_success
+		fixed $test_fixed
+		broken $test_broken
+		failed $test_failure
+
+		EOF
 	fi
 
 	if test "$test_fixed" != 0
-- 
1.7.5

```

## Junio C Hamano, 2011-04-29 16:28

Subject: Re: [PATCH v2] t/test-lib.sh: minor readability improvements
Message-ID: <7vpqo5xbru.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vpqo5xbru.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1304080230-11670-1-git-send-email-misfire@debugon.org>

```
Thanks.

```
