On Tue, Jun 11, 2024 at 08:54:13PM GMT, Bruno Haible wrote:
> I'm debugging a unit test that consists of a dozen of shell statement groups,
> each with a 'returns_ 1 ...' invocation. The log file shows an error message,
> but no indication from which of the statement groups the error came. So,
> naturally, I add a 'set -x' command near the beginning of the test file.
> But it doesn't work: The first 'returns_ 1 ...' command turns off the 'x'
> flag! - defeating the purpose of my 'set -x'.
> 
> This patch fixes it.
> 
> 
> 2024-06-11  Bruno Haible  <br...@clisp.org>
> 
>       test-framework-sh: Fix 'returns_' to not turn off tracing permanently.
>       * tests/init.sh (returns_): Restore tracing if it was enabled before.
> 
> diff --git a/tests/init.sh b/tests/init.sh
> index 4689b6b758..2724f5ab67 100644
> --- a/tests/init.sh
> +++ b/tests/init.sh
> @@ -594,6 +594,7 @@ fi
>  #   returns_ 1 command ... || fail
>  returns_ () {
>    # Disable tracing so it doesn't interfere with stderr of the wrapped 
> command
> +  local is_tracing=`{ :; } 2>&1`

'local' is a bashism, but I can already see that tests/init.sh has
gone to lengths to require that local exists and has "sane" semantics
(by excluding dash semantics).

But as long as we're relying on Bash semantics, why not use the simpler:

local -; set -x

which will auto-restore $- (and hence set +x if needed) when the
function exits, rather than having to futz around with $is_tracing?

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org


Reply via email to