1
0
mirror of https://github.com/sstephenson/bats.git synced 2026-02-25 17:28:11 +01:00

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
This commit is contained in:
Sam Stephenson
2013-10-28 21:01:51 -05:00
parent 417acfff66
commit bfa4ebcd0f
5 changed files with 10 additions and 11 deletions

View File

@@ -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"
}