[PATCH blktests 1/2] check: allow strict error-checking by "set -e" in test cases

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In bash script development, it is a good practice to handle errors
strictly using "set -e" or "set -o errexit". When this option is
enabled, bash exits immediately upon encountering an error. There have
been discussions about implementing this strict error-checking mechanism
in blktests test cases [1]. Recently, these discussions were revisited,
and it has been proposed to enable this strict error-checking for a
limited subset of test cases [2].

However, the error-checking does not work as expected, even when each
test case does "set -e", because the error-checking has certain
exceptions relevant to execution contexts. According to the bash man
page, "The shell doe not exit ... part of the test following the if or
elif reserved words, ... or if the command's return value is being
inverted with !". The blktests test case execution context applies to
these exceptions.

To ensure that "set -e" behaves as intended in test cases, avoid the
if statements and the return value inversions (!) in the test case
execution context.

Link: https://github.com/linux-blktests/blktests/issues/89 [1]
Link: https://lore.kernel.org/linux-block/ckctv7ioomqpxe2iwcg6eh6fvtzamoihnmwxvavd7lanr4y2y6@fbznem3nvw3w/ [2]
Signed-off-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@xxxxxxx>
---
 check | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/check b/check
index dad5e70..f11ce74 100755
--- a/check
+++ b/check
@@ -502,9 +502,10 @@ _check_and_call_test_device() {
 			fi
 		fi
 		RESULTS_DIR="$OUTPUT/$(basename "$TEST_DEV")""$postfix"
-		if ! _call_test test_device; then
-			ret=1
-		fi
+		# Avoid "if" and "!" for errexit in test cases
+		_call_test test_device
+		# shellcheck disable=SC2181
+		(($? != 0)) && ret=1
 		if (( unset_skip_reason )); then
 			unset SKIP_REASONS
 		fi
@@ -637,9 +638,10 @@ _run_group() {
 	local ret=0
 	local test_name
 	for test_name in "${tests[@]}"; do
-		if ! ( _run_test "$test_name" ); then
-			ret=1
-		fi
+		# Avoid "if" and "!" for errexit in test cases
+		( _run_test "$test_name" )
+		# shellcheck disable=SC2181
+		(($? != 0)) && ret=1
 	done
 	return $ret
 }
@@ -695,9 +697,10 @@ _check() {
 		if [[ $group != "$prev_group" ]]; then
 			prev_group="$group"
 			if [[ ${#tests[@]} -gt 0 ]]; then
-				if ! ( _run_group "${tests[@]}" ); then
-					ret=1
-				fi
+				# Avoid "if" and "!" for errexit in test cases
+				( _run_group "${tests[@]}" )
+				# shellcheck disable=SC2181
+				(($? != 0)) && ret=1
 				tests=()
 			fi
 		fi
@@ -705,9 +708,10 @@ _check() {
 	done < <(_find_tests "$@" | sort -zu)
 
 	if [[ ${#tests[@]} -gt 0 ]]; then
-		if ! ( _run_group "${tests[@]}" ); then
-			ret=1
-		fi
+		# Avoid "if" and "!" for errexit in test cases
+		( _run_group "${tests[@]}" )
+		# shellcheck disable=SC2181
+		(($? != 0)) && ret=1
 	fi
 
 	return $ret
-- 
2.49.0





[Index of Archives]     [Linux RAID]     [Linux SCSI]     [Linux ATA RAID]     [IDE]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Device Mapper]

  Powered by Linux