From bfa4ebcd0f5b75addedac3361328f73416d1c274 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Mon, 28 Oct 2013 21:01:51 -0500 Subject: [PATCH] Prefer `let x+=1` for incrementing counters The `((x++))` syntax is shorthand for `let x++`. According to `help let`: If the last ARG evaluates to 0, let returns 1; 0 is returned otherwise. Thus the exit status of the expression `x=0; let x++` is 1, since the post-increment `++` operator evaluates to the value of the variable before incrementing. In Bash 4, this non-zero exit status properly triggers `set -e`'s error trap, but in Bash 3 it does not. That's why the tests were passing on OS X (Bash 3) but not Linux (Bash 4). We can work around the problem by choosing an incrementation expression that never evaluates to 0, such as `+=` or the pre-increment `++` operator. For consistency and clarity, I've changed to `x+=1` everywhere. Ref. #25, #27 --- libexec/bats | 2 +- libexec/bats-exec-suite | 6 +++--- libexec/bats-exec-test | 6 +++--- libexec/bats-format-tap-stream | 5 ++--- libexec/bats-preprocess | 2 +- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/libexec/bats b/libexec/bats index cf3fb62..732c56a 100755 --- a/libexec/bats +++ b/libexec/bats @@ -67,7 +67,7 @@ for arg in "$@"; do while option="${arg:$index:1}"; do [ -n "$option" ] || break options[${#options[*]}]="$option" - index=$(($index+1)) + let index+=1 done fi else diff --git a/libexec/bats-exec-suite b/libexec/bats-exec-suite index 96de2a5..29ab255 100755 --- a/libexec/bats-exec-suite +++ b/libexec/bats-exec-suite @@ -17,7 +17,7 @@ trap "kill 0; exit 1" int count=0 for filename in "$@"; do - count=$(($count + $(bats-exec-test -c "$filename"))) + let count+="$(bats-exec-test -c "$filename")" done if [ -n "$count_only_flag" ]; then @@ -35,11 +35,11 @@ for filename in "$@"; do while IFS= read -r line; do case "$line" in "begin "* ) - index=$(($index + 1)) + let index+=1 echo "${line/ $index / $(($offset + $index)) }" ;; "ok "* | "not ok "* ) - [ -n "$extended_syntax_flag" ] || index=$(($index + 1)) + [ -n "$extended_syntax_flag" ] || let index+=1 echo "${line/ $index / $(($offset + $index)) }" [ "${line:0:6}" != "not ok" ] || status=1 ;; diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index bbbd411..6f5d811 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -97,7 +97,7 @@ bats_capture_stack_trace() { if [[ "$frame" = *"$test_pattern" ]]; then break else - ((index++)) + let index+=1 fi done fi @@ -125,7 +125,7 @@ bats_print_stack_trace() { echo "from function \`$fn' in file $filename, line $line," fi - ((index++)) + let index+=1 done } @@ -197,7 +197,7 @@ bats_perform_tests() { status=0 for test_name in "$@"; do "$0" $BATS_EXTENDED_SYNTAX "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=1 - test_number=$(($test_number + 1)) + let test_number+=1 done exit "$status" } diff --git a/libexec/bats-format-tap-stream b/libexec/bats-format-tap-stream index 0f91e55..1780680 100755 --- a/libexec/bats-format-tap-stream +++ b/libexec/bats-format-tap-stream @@ -131,7 +131,7 @@ trap finish EXIT while IFS= read -r line; do case "$line" in "begin "* ) - index=$(( $index + 1 )) + let index+=1 name="${line#* $index }" buffer begin flush @@ -145,11 +145,10 @@ while IFS= read -r line; do fi ;; "not ok "* ) - failures=$(( $failures + 1 )) + let failures+=1 buffer fail ;; "# "* ) - buffer log "${line:2}" ;; esac diff --git a/libexec/bats-preprocess b/libexec/bats-preprocess index 26730e0..3fb039f 100755 --- a/libexec/bats-preprocess +++ b/libexec/bats-preprocess @@ -33,7 +33,7 @@ tests=() index=0 while IFS= read -r line; do - index=$(($index + 1)) + let index+=1 quoted_name="$(expr "$line" : ' *@test *\([^ ].*\) *{ *$' || true)" if [ -n "$quoted_name" ]; then