Subject: Re: [PATCH 03/11] t6002: make test "set -e" clean

From: Patrick Steinhardt <[email-masked]>
Date: Wed, 25 Mar 2026 08:15:32 +0100

On Tue, Mar 24, 2026 at 11:21:06PM -0700, Junio C Hamano wrote:
> In order to catch mistakes like misspelling "test_expect_success",
> we would like to eventually be able to run our test suite with the
> "-e" option on.
> 
> We often use
> 
>       val=$(expr expression)
> 
> only for the computation, and it is good that "expr" exits non-zero
> with syntactically invalid expression (it exits with 2) and other
> errors (with 3), as we do want to notice such errors.
> 
> "expr" however also exits with "1" if it yields 0 or null X-<.
> 
> Make sure we do not fail unnecessarily under "set -e".
> 
> Signed-off-by: Junio C Hamano <[email-masked]>
> ---
>  t/t6002-rev-list-bisect.sh | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/t/t6002-rev-list-bisect.sh b/t/t6002-rev-list-bisect.sh
> index daa009c9a1..1a6ffd8fbd 100755
> --- a/t/t6002-rev-list-bisect.sh
> +++ b/t/t6002-rev-list-bisect.sh
> @@ -27,9 +27,9 @@ test_bisection_diff()
>  	# Test if bisection size is close to half of list size within
>  	# tolerance.
>  	#
> -	_bisect_err=$(expr $_list_size - $_bisection_size \* 2)
> +	_bisect_err=$(expr $_list_size - $_bisection_size \* 2) && test $? -le 1
>  	test "$_bisect_err" -lt 0 && _bisect_err=$(expr 0 - $_bisect_err)
> -	_bisect_err=$(expr $_bisect_err / 2) ; # floor
> +	_bisect_err=$(expr $_bisect_err / 2) && test $? -le 1; # floor
>  
>  	test_expect_success \
>  	"bisection diff $_bisect_option $_head $* <= $_max_diff" \

I've got this alternate fix, which I find a bit cleaner overall. With
`$((...))` we don't have to worry about the return value of expr.

Patrick

diff --git a/t/t6002-rev-list-bisect.sh b/t/t6002-rev-list-bisect.sh
index daa009c9a1..f2de40b5ed 100755
--- a/t/t6002-rev-list-bisect.sh
+++ b/t/t6002-rev-list-bisect.sh
@@ -27,13 +27,16 @@ test_bisection_diff()
 	# Test if bisection size is close to half of list size within
 	# tolerance.
 	#
-	_bisect_err=$(expr $_list_size - $_bisection_size \* 2)
-	test "$_bisect_err" -lt 0 && _bisect_err=$(expr 0 - $_bisect_err)
-	_bisect_err=$(expr $_bisect_err / 2) ; # floor
-
-	test_expect_success \
-	"bisection diff $_bisect_option $_head $* <= $_max_diff" \
-	'test $_bisect_err -le $_max_diff'
+	_bisect_err=$(($_list_size - $_bisection_size * 2))
+	if test "$_bisect_err" -lt 0
+	then
+		_bisect_err=$((0 - $_bisect_err))
+	fi
+	_bisect_err=$(($_bisect_err / 2)) ; # floor
+
+	test_expect_success "bisection diff $_bisect_option $_head $* <= $_max_diff" '
+		test $_bisect_err -le $_max_diff
+	'
 }
 
 date >path0