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>
This commit is contained in:
Gilles Peskine 2020-03-28 21:27:40 +01:00 committed by Thomas Daubney
parent 4848d7bb45
commit 39a3b11006

View file

@ -90,6 +90,9 @@
# #
# Each component must start by invoking `msg` with a short informative message. # 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 # Each component is executed in a separate shell process. The component
# fails if any command in it returns a non-zero status. # fails if any command in it returns a non-zero status.
# #
@ -479,6 +482,7 @@ pre_setup_keep_going () {
previous_failure_status=0 previous_failure_status=0
previous_failed_command= previous_failed_command=
previous_failure_funcall_depth=0 previous_failure_funcall_depth=0
unset report_failed_command
start_red= start_red=
end_color= end_color=
@ -503,7 +507,7 @@ pre_setup_keep_going () {
"msg "*) false;; "msg "*) false;;
*[!A-Za-z]"test"|*[!A-Za-z]"test"[!A-Za-z]*) true;; *[!A-Za-z]"test"|*[!A-Za-z]"test"[!A-Za-z]*) true;;
"tests/"*) true;; "tests/"*) true;;
"grep "*|"not grep "*) true;; "grep "*|"! grep "*) true;;
*) false;; *) false;;
esac esac
} }
@ -515,7 +519,7 @@ pre_setup_keep_going () {
# Save $? (status of the failing command). This must be the very # Save $? (status of the failing command). This must be the very
# first thing, before $? is overridden. # first thing, before $? is overridden.
last_failure_status=$? last_failure_status=$?
failed_command=$BASH_COMMAND failed_command=${report_failed_command-$BASH_COMMAND}
if [[ $last_failure_status -eq $previous_failure_status && if [[ $last_failure_status -eq $previous_failure_status &&
"$failed_command" == "$previous_failed_command" && "$failed_command" == "$previous_failed_command" &&
@ -573,8 +577,14 @@ if_build_succeeded () {
"$@" "$@"
} }
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 () { pre_setup_quiet_redirect () {