]> git.ipfire.org Git - thirdparty/automake.git/commitdiff
tests: add shell function support to `command_ok_'
authorRichard Hansen <rhansen@rhansen.org>
Mon, 24 Feb 2025 00:21:17 +0000 (19:21 -0500)
committerKarl Berry <karl@freefriends.org>
Mon, 24 Feb 2025 22:25:39 +0000 (14:25 -0800)
* 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.

t/ax/tap-functions.sh

index 5b36b1c872fe30a5a289efe5b47c40084a814fde..c5dbc75f6d70c692e9dde7b0236ba02bc16dfa9c 100644 (file)
@@ -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_"
 }