Use compiler-agnostic defines to silence warning
[lttng-tools.git] / tests / utils / tap-driver.sh
index cbe2952210223ce6dc46f9d42da2cb2f60263d1b..e2d9ad9c64893aec3a075a74f2562dde1fb1f1f7 100755 (executable)
@@ -49,6 +49,7 @@ print_usage ()
   cat <<END
 Usage:
   tap-driver.sh --test-name=NAME --log-file=PATH --trs-file=PATH
   cat <<END
 Usage:
   tap-driver.sh --test-name=NAME --log-file=PATH --trs-file=PATH
+                [--log-file-d={yes|no}]
                 [--expect-failure={yes|no}] [--color-tests={yes|no}]
                 [--enable-hard-errors={yes|no}] [--ignore-exit]
                 [--diagnostic-string=STRING] [--merge|--no-merge]
                 [--expect-failure={yes|no}] [--color-tests={yes|no}]
                 [--enable-hard-errors={yes|no}] [--ignore-exit]
                 [--diagnostic-string=STRING] [--merge|--no-merge]
@@ -61,6 +62,7 @@ END
 # TODO: $log_file, $trs_file and $test_name are defined).
 test_name= # Used for reporting.
 log_file=  # Where to save the result and output of the test script.
 # TODO: $log_file, $trs_file and $test_name are defined).
 test_name= # Used for reporting.
 log_file=  # Where to save the result and output of the test script.
+log_file_d=yes  # If a directory "${log_file}.d" should be created and used for LTTng logs
 trs_file=  # Where to save the metadata of the test run.
 post_script= # Script to be run after the test.
 expect_failure=0
 trs_file=  # Where to save the metadata of the test run.
 post_script= # Script to be run after the test.
 expect_failure=0
@@ -75,6 +77,7 @@ while test $# -gt 0; do
   --version) echo "$me $scriptversion"; exit $?;;
   --test-name) test_name=$2; shift;;
   --log-file) log_file=$2; shift;;
   --version) echo "$me $scriptversion"; exit $?;;
   --test-name) test_name=$2; shift;;
   --log-file) log_file=$2; shift;;
+  --log-file-d) log_file_d=$2; shift;;
   --trs-file) trs_file=$2; shift;;
   --color-tests) color_tests=$2; shift;;
   --expect-failure) expect_failure=$2; shift;;
   --trs-file) trs_file=$2; shift;;
   --color-tests) color_tests=$2; shift;;
   --expect-failure) expect_failure=$2; shift;;
@@ -117,6 +120,8 @@ else
   init_colors=''
 fi
 
   init_colors=''
 fi
 
+TIME_SCRIPT="$(realpath -e -- "$(dirname "$0")")/tap/clock"
+
 # :; is there to work around a bug in bash 3.2 (and earlier) which
 # does not always set '$?' properly on redirection failure.
 # See the Autoconf manual for more details.
 # :; is there to work around a bug in bash 3.2 (and earlier) which
 # does not always set '$?' properly on redirection failure.
 # See the Autoconf manual for more details.
@@ -142,8 +147,32 @@ fi
     else
       exec 2>&3
     fi
     else
       exec 2>&3
     fi
-    "$@"
-    echo $?
+    # Since stderr and stdout may go to the same file and the script
+    # may run in situations with different types of buffering, enforce
+    # line buffering for both FDs. This helps to prevent interleave between
+    # the outputs in the resulting file for half written lines, eg.
+    #   ok 93 - Tes# PERROR - xxxx
+    #   t some function
+    ORIG_LTTNG_TEST_LOG_DIR="${LTTNG_TEST_LOG_DIR:-}"
+    LTTNG_TEST_LOG_DIR=""
+    if test "${log_file_d}" = yes && [ ! -e "${log_file}.d" ]; then
+      mkdir -p "${log_file}.d"
+      LTTNG_TEST_LOG_DIR="$(realpath "${log_file}.d")"
+      export LTTNG_TEST_LOG_DIR
+    fi
+    if [ "${LTTNG_TESTS_TAP_AUTOTIME:-}" != 0 ]; then
+      stdbuf -eL -oL -- "$@"
+    else
+      "$@"
+    fi
+    ret=$?
+    if [ "${ret}" = "0" ] && test "${log_file_d}" = yes && [ -d "${LTTNG_TEST_LOG_DIR}" ]; then
+      rm -rf "${LTTNG_TEST_LOG_DIR:?}/"
+    fi
+    if [ -n "${ORIG_LTTNG_TEST_LOG_DIR}" ]; then
+      LTTNG_TEST_LOG_DIR="${ORIG_LTTNG_TEST_LOG_DIR}"
+    fi
+    echo $ret
   ) | LC_ALL=C ${AM_TAP_AWK-awk} \
         -v me="$me" \
         -v test_script_name="$test_name" \
   ) | LC_ALL=C ${AM_TAP_AWK-awk} \
         -v me="$me" \
         -v test_script_name="$test_name" \
@@ -154,6 +183,7 @@ fi
         -v ignore_exit="$ignore_exit" \
         -v comments="$comments" \
         -v diag_string="$diag_string" \
         -v ignore_exit="$ignore_exit" \
         -v comments="$comments" \
         -v diag_string="$diag_string" \
+        -v time_script="${TIME_SCRIPT}" \
 '
 # TODO: the usages of "cat >&3" below could be optimized when using
 #       GNU awk, and/on on systems that supports /dev/fd/.
 '
 # TODO: the usages of "cat >&3" below could be optimized when using
 #       GNU awk, and/on on systems that supports /dev/fd/.
@@ -211,14 +241,13 @@ function copy_in_global_log()
   return 0
 }
 
   return 0
 }
 
-function gettime_ns(    r,    cmd)
+function gettime_ns(    r)
 {
 {
-   cmd = "date +%s.%N"
    r = ""
    r = ""
-   if (cmd | getline r) {
+   if (time_script | getline r) {
      r = r
    }
      r = r
    }
-   close(cmd)
+   close(time_script)
    return sprintf("%f", r)
 }
 
    return sprintf("%f", r)
 }
 
This page took 0.024331 seconds and 4 git commands to generate.