From c2feee4fefa600e761d64e13a5ca5fe7cdb81f41 Mon Sep 17 00:00:00 2001 From: Michal Berger Date: Wed, 2 Jun 2021 10:40:46 +0200 Subject: [PATCH] test/common: Free stderr fd from tracing Consider this: foo() { echo bar >&2 ; } set -x foobar=$(foo 2>&1) [[ $foobar == "bar" ]] The above test will fail since $foobar will also include tracing strings as stderr is where set -x redirects the output. Since in some cases this may troublesome, replace stderr with a dedicated fd allocated dynamically. Signed-off-by: Michal Berger Change-Id: Ia6609e4463b371c07fab42d2bd291c9e43742df5 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/8158 Reviewed-by: Jim Harris Reviewed-by: Shuhei Matsumoto Reviewed-by: Ben Walker Tested-by: SPDK CI Jenkins --- test/common/autotest_common.sh | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/common/autotest_common.sh b/test/common/autotest_common.sh index 0180adde12..30f5832e6b 100755 --- a/test/common/autotest_common.sh +++ b/test/common/autotest_common.sh @@ -1,5 +1,16 @@ #!/usr/bin/env bash +function xtrace_fd() { + if [[ -n $BASH_XTRACEFD && -e /proc/self/fd/$BASH_XTRACEFD ]]; then + # Close it first to make sure it's sane + exec {BASH_XTRACEFD}>&- + fi + exec {BASH_XTRACEFD}>&2 + + set -x + echo "Tracing to $BASH_XTRACEFD FD" +} + function xtrace_disable() { if [ "$XTRACE_DISABLED" != "yes" ]; then PREV_BASH_OPTS="$-" @@ -1388,7 +1399,7 @@ if $SPDK_AUTOTEST_X; then # explicitly enable xtraces, overriding any tracking information. unset XTRACE_DISABLED unset XTRACE_NESTING_LEVEL - set -x + xtrace_fd xtrace_enable else xtrace_restore