tests: add and use bt_grep
[babeltrace.git] / tests / utils / utils.sh
index d40011a127398e50c8fff84628b25e579111887d..24f1b2d26b6c9af59655bf303c4f5e324935d476 100644 (file)
@@ -111,6 +111,8 @@ if [ -z "${BT_TESTS_PYTHON_BIN:-}" ]; then
 fi
 export BT_TESTS_PYTHON_BIN
 
+BT_TESTS_PYTHON_VERSION=$($BT_TESTS_PYTHON_BIN -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))')
+
 if [ -z "${BT_TESTS_PYTHON_CONFIG_BIN:-}" ]; then
        BT_TESTS_PYTHON_CONFIG_BIN="python3-config"
 fi
@@ -151,7 +153,7 @@ fi
 # Remove CR characters in file "$1".
 
 bt_remove_cr() {
-       "$BT_TESTS_SED_BIN" -i 's/\r//g' "$1"
+       "$BT_TESTS_SED_BIN" -i'' -e 's/\r//g' "$1"
 }
 
 # Run the Babeltrace CLI, redirecting stdout and stderr to specified files.
@@ -226,8 +228,8 @@ bt_diff_cli() {
        local ret_stdout
        local ret_stderr
 
-       temp_stdout_output_file="$(mktemp -t actual_stdout.XXXXXX)"
-       temp_stderr_output_file="$(mktemp -t actual_stderr.XXXXXX)"
+       temp_stdout_output_file="$(mktemp -t actual-stdout.XXXXXX)"
+       temp_stderr_output_file="$(mktemp -t actual-stderr.XXXXXX)"
 
        # Run the CLI to get a detailed file.
        bt_cli "$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}"
@@ -295,6 +297,10 @@ bt_diff_details_ctf_gen_single() {
        return $ret
 }
 
+# Run the grep binary configured for the tests.
+bt_grep() {
+       "$BT_TESTS_GREP_BIN" "$@"
+}
 
 ### Functions ###
 
@@ -305,7 +311,15 @@ check_coverage() {
 # Execute a shell command in the appropriate environment to access the Python
 # test utility modules in `tests/utils/python`.
 run_python() {
-       PYTHONPATH="${BT_TESTS_SRCDIR}/utils/python${PYTHONPATH:+:}${PYTHONPATH:-}" "$@"
+       local our_pythonpath="${BT_TESTS_SRCDIR}/utils/python"
+
+       if [[ $BT_TESTS_PYTHON_VERSION =~ 3.[45] ]]; then
+               # Add a local directory containing a `typing.py` to `PYTHONPATH` for
+               # Python 3.4 which doesn't offer the `typing` module.
+               our_pythonpath="$our_pythonpath:${BT_TESTS_SRCDIR}/utils/python/typing"
+       fi
+
+       PYTHONPATH="${our_pythonpath}${PYTHONPATH:+:}${PYTHONPATH:-}" "$@"
 }
 
 # Execute a shell command in the appropriate environment to have access to the
@@ -348,7 +362,7 @@ run_python_bt2() {
        # existing ASAN_OPTIONS, such that we override the user's value if it
        # contains detect_leaks=1.
        if [ "${BT_TESTS_ENABLE_ASAN:-}" = "1" ]; then
-               if ${BT_TESTS_CC_BIN} --version | head -n 1 | grep -q '^gcc'; then
+               if ${BT_TESTS_CC_BIN} --version | head -n 1 | bt_grep -q '^gcc'; then
                        lib_asan="$(${BT_TESTS_CC_BIN} -print-file-name=libasan.so)"
                        local -x LD_PRELOAD="${lib_asan}${LD_PRELOAD:+:}${LD_PRELOAD:-}"
                fi
@@ -399,3 +413,16 @@ run_python_bt2_test() {
 
        return $ret
 }
+
+# Generate a CTF trace using `mctf.py`.
+#
+# $1: Input filename
+# $2: Base directory path for output files
+gen_mctf_trace() {
+       local input_file="$1"
+       local base_dir="$2"
+
+       diag "Running: ${BT_TESTS_PYTHON_BIN} ${BT_TESTS_SRCDIR}/utils/python/mctf.py --base-dir ${base_dir} ${input_file}"
+       run_python "${BT_TESTS_PYTHON_BIN}" "${BT_TESTS_SRCDIR}/utils/python/mctf.py" \
+               --base-dir "${base_dir}" "${input_file}"
+}
This page took 0.026564 seconds and 4 git commands to generate.