From: Richard Hansen Date: Mon, 24 Feb 2025 00:21:17 +0000 (-0500) Subject: tests: add shell function support to `command_ok_' X-Git-Tag: v1.17.90~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f988a1f7fb0762eba04447004fed015476501372;p=thirdparty%2Fautomake.git tests: add shell function support to `command_ok_' * t/ax/tap-functions.sh (command_ok_): Run the command without testing the exit status with `||' in case the command is a shell function. This avoids unintentionally disabling `set -e' inside the shell function. --- diff --git a/t/ax/tap-functions.sh b/t/ax/tap-functions.sh index 5b36b1c87..c5dbc75f6 100644 --- a/t/ax/tap-functions.sh +++ b/t/ax/tap-functions.sh @@ -227,7 +227,29 @@ command_ok_ () esac shift done - tap_result_="ok"; "$@" || tap_result_="not ok" + tap_result_="ok" + # Temporarily disable `set -e` if enabled so that the shell does not abort + # if the command fails. (See the comment below for why `|| handle_error` + # can't be used.) The `set +o` command could be used instead of examining + # `$-`, but that would add lots of `set -x` log spam. + case $- in + *e*) restore_set_e_='set -e';; + *) restore_set_e_='';; + esac + set +e + # Run the command in a subshell in case the command is a shell function that + # invokes `exit` (perhaps indirectly via `set -e`). This also prevents the + # function from modifying the outer shell execution environment (e.g., + # change variables), which may be an upside or a downside depending on what + # the function wants to do. + # + # Do NOT put the command or the subshell on the left-hand side of a `||` (or + # as the condition of an `if` statement, etc.). Otherwise, if the command + # is a shell function, `set -e` would be effectively disabled inside the + # function, potentially causing failures to be treated as successes. + (eval "$restore_set_e_" && "$@") + test "$?" -eq 0 || tap_result_="not ok" + eval "$restore_set_e_" result_ "$tap_result_" -D "$tap_directive_" -r "$tap_reason_" \ -- "$tap_description_" }