Better not function
In the `not` function, in keep-going mode, arrange to report the
failing command (rather than `"$@"`).
Note that the `!` keyword should not be used, because failures with
`!` are not reported properly.
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh
index cb3c8f2..d95ca7b 100755
--- a/tests/scripts/all.sh
+++ b/tests/scripts/all.sh
@@ -90,6 +90,9 @@
#
# Each component must start by invoking `msg` with a short informative message.
#
+# Warning: due to the way bash detects errors, the failure of a command
+# inside 'if' or '!' is not detected. Use the 'not' function instead of '!'.
+#
# Each component is executed in a separate shell process. The component
# fails if any command in it returns a non-zero status.
#
@@ -484,6 +487,7 @@
previous_failure_status=0
previous_failed_command=
previous_failure_funcall_depth=0
+ unset report_failed_command
start_red=
end_color=
@@ -508,7 +512,7 @@
"msg "*) false;;
*[!A-Za-z]"test"|*[!A-Za-z]"test"[!A-Za-z]*) true;;
"tests/"*) true;;
- "grep "*|"not grep "*) true;;
+ "grep "*|"! grep "*) true;;
*) false;;
esac
}
@@ -520,7 +524,7 @@
# Save $? (status of the failing command). This must be the very
# first thing, before $? is overridden.
last_failure_status=$?
- failed_command=$BASH_COMMAND
+ failed_command=${report_failed_command-$BASH_COMMAND}
if [[ $last_failure_status -eq $previous_failure_status &&
"$failed_command" == "$previous_failed_command" &&
@@ -578,8 +582,14 @@
"$@"
}
-not() {
- ! "$@"
+# '! true' does not trigger the ERR trap. Arrange to trigger it, with
+# a reasonably informative error message (not just "$@").
+not () {
+ if "$@"; then
+ report_failed_command="! $*"
+ false
+ unset report_failed_command
+ fi
}
pre_setup_quiet_redirect () {