3 # SPDX-License-Identifier: GPL-2.0-only
5 # Copyright (c) 2019 Michael Jeanson <mjeanson@efficios.com>
6 # Copyright (C) 2019 Philippe Proulx <pproulx@efficios.com>
9 # This file is meant to be sourced at the start of shell script-based tests.
12 # Error out when encountering an undefined variable
15 # If "readlink -f" is available, get a resolved absolute path to the
16 # tests source dir, otherwise make do with a relative path.
17 scriptdir
="$(dirname "${BASH_SOURCE[0]}")"
18 if readlink
-f "." >/dev
/null
2>&1; then
19 testsdir
=$
(readlink
-f "$scriptdir/..")
21 testsdir
="$scriptdir/.."
24 # The OS on which we are running. See [1] for possible values of 'uname -s'.
25 # We do a bit of translation to ease our life down the road for comparison.
26 # Export it so that called executables can use it.
27 # [1] https://en.wikipedia.org/wiki/Uname#Examples
28 if [ "x${BT_TESTS_OS_TYPE:-}" = "x" ]; then
29 BT_TESTS_OS_TYPE
="$(uname -s)"
30 case "$BT_TESTS_OS_TYPE" in
32 BT_TESTS_OS_TYPE
="mingw"
35 BT_TESTS_OS_TYPE
="darwin"
38 BT_TESTS_OS_TYPE
="linux"
41 BT_TESTS_OS_TYPE
="cygwin"
44 BT_TESTS_OS_TYPE
="unsupported"
48 export BT_TESTS_OS_TYPE
50 # Allow overriding the source and build directories
51 if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then
52 BT_TESTS_SRCDIR
="$testsdir"
54 export BT_TESTS_SRCDIR
56 if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then
57 BT_TESTS_BUILDDIR
="$testsdir"
59 export BT_TESTS_BUILDDIR
62 # Source the generated environment file if it's present.
63 if [ -f "${BT_TESTS_BUILDDIR}/utils/env.sh" ]; then
64 # shellcheck source=./env.sh
65 .
"${BT_TESTS_BUILDDIR}/utils/env.sh"
68 # Allow overriding the babeltrace2 executables
69 if [ "x${BT_TESTS_BT2_BIN:-}" = "x" ]; then
70 BT_TESTS_BT2_BIN
="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2"
71 if [ "$BT_TESTS_OS_TYPE" = "mingw" ]; then
72 BT_TESTS_BT2_BIN
="${BT_TESTS_BT2_BIN}.exe"
75 export BT_TESTS_BT2_BIN
77 # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed
78 BT_PLUGINS_PATH
="${BT_TESTS_BUILDDIR}/../src/plugins"
80 # Allow overriding the babeltrace2 plugin path
81 if [ "x${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" = "x" ]; then
82 BT_TESTS_BABELTRACE_PLUGIN_PATH
="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text:${BT_PLUGINS_PATH}/lttng-utils"
84 export BT_TESTS_BABELTRACE_PLUGIN_PATH
86 if [ "x${BT_TESTS_PROVIDER_DIR:-}" = "x" ]; then
87 BT_TESTS_PROVIDER_DIR
="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs"
89 export BT_TESTS_PROVIDER_DIR
91 # Allow overriding the babeltrace2 executables
92 if [ "x${BT_TESTS_PYTHONPATH:-}" = "x" ]; then
93 BT_TESTS_PYTHONPATH
="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib"
95 export BT_TESTS_PYTHONPATH
98 ### External Tools ###
99 if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then
100 BT_TESTS_AWK_BIN
="awk"
102 export BT_TESTS_AWK_BIN
104 if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then
105 BT_TESTS_GREP_BIN
="grep"
107 export BT_TESTS_GREP_BIN
109 if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then
110 BT_TESTS_PYTHON_BIN
="python3"
112 export BT_TESTS_PYTHON_BIN
114 if [ "x${BT_TESTS_PYTHON_CONFIG_BIN:-}" = "x" ]; then
115 BT_TESTS_PYTHON_CONFIG_BIN
="python3-config"
117 export BT_TESTS_PYTHON_CONFIG_BIN
119 if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then
120 BT_TESTS_SED_BIN
="sed"
122 export BT_TESTS_SED_BIN
126 BT_TESTS_DATADIR
="${BT_TESTS_SRCDIR}/data"
127 BT_CTF_TRACES_PATH
="${BT_TESTS_DATADIR}/ctf-traces"
129 # By default, it will not source tap.sh. If you want to output tap directly
130 # from the test script, define the 'SH_TAP' variable to '1' before sourcing
132 if [ "x${SH_TAP:-}" = x1
]; then
133 # shellcheck source=./tap/tap.sh
134 .
"${BT_TESTS_SRCDIR}/utils/tap/tap.sh"
138 # Remove CR characters in file "$1".
141 "$BT_TESTS_SED_BIN" -i 's/\r//g' "$1"
144 # Run the Babeltrace CLI, redirecting stdout and stderr to specified files.
146 # $1: file to redirect stdout to
147 # $2: file to redirect stderr to
148 # remaining args: arguments to pass to the CLI
150 # Return the exit code of the CLI.
153 local stdout_file
="$1"
154 local stderr_file
="$2"
158 echo "Running: $BT_TESTS_BT2_BIN ${args[*]}" >&2
159 run_python_bt2
"$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$stdout_file" 2>"$stderr_file"
162 ### Diff Functions ###
164 # Check the differences between two files (typically some expected output vs
165 # some actual output). If there are differences, print the diff to stderr.
167 # $1: file 1 (expected)
168 # $2: file 2 (actual)
170 # Return 0 if there's no difference, and non-zero if there are.
172 # Note that this function modifies the actual output file ($2) _in-place_ to
173 # remove any \r character.
176 local expected_file
="$1"
177 local actual_file
="$2"
180 # Strip any \r present due to Windows (\n -> \r\n).
181 # "diff --string-trailing-cr" is not used since it is not present on
183 bt_remove_cr
"$actual_file"
185 diff -u "$expected_file" "$actual_file" 1>&2
190 # Checks the difference between:
192 # 1. What the CLI outputs on its standard output when given the arguments
193 # "$@" (excluding the first two arguments).
194 # 2. The file with path "$1".
196 # And the difference between:
198 # 1. What the CLI outputs on its standard error when given the arguments
199 # "$@" (excluding the first two arguments).
200 # 2. The file with path "$2".
202 # Returns 0 if there's no difference, and 1 if there is, also printing
203 # said difference to the standard error.
205 local expected_stdout_file
="$1"
206 local expected_stderr_file
="$2"
210 local temp_stdout_output_file
211 local temp_stderr_output_file
216 temp_stdout_output_file
="$(mktemp -t actual_stdout.XXXXXX)"
217 temp_stderr_output_file
="$(mktemp -t actual_stderr.XXXXXX)"
219 # Run the CLI to get a detailed file.
220 bt_cli
"$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}"
222 bt_diff
"$expected_stdout_file" "$temp_stdout_output_file" "${args[@]}"
224 bt_diff
"$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}"
227 if ((ret_stdout
!= 0 || ret_stderr
!= 0)); then
231 rm -f "$temp_stdout_output_file" "$temp_stderr_output_file"
236 # Checks the difference between the content of the file with path "$1"
237 # and the output of the CLI when called on the directory path "$2" with
238 # the arguments '-c sink.text.details' and the rest of the arguments to
241 # Returns 0 if there's no difference, and 1 if there is, also printing
242 # said difference to the standard error.
243 bt_diff_details_ctf_single
() {
244 local expected_stdout_file
="$1"
247 local extra_details_args
=("$@")
248 expected_stderr_file
="/dev/null"
250 # Compare using the CLI with `sink.text.details`
251 bt_diff_cli
"$expected_stdout_file" "$expected_stderr_file" "$trace_dir" \
252 "-c" "sink.text.details" "${extra_details_args[@]+${extra_details_args[@]}}"
255 # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a
256 # program which generates the CTF trace to compare to. The program "$1"
257 # receives the path to a temporary, empty directory where to write the
258 # CTF trace as its first argument.
259 bt_diff_details_ctf_gen_single
() {
260 local ctf_gen_prog_path
="$1"
261 local expected_stdout_file
="$2"
263 local extra_details_args
=("$@")
268 temp_trace_dir
="$(mktemp -d)"
270 # Run the CTF trace generator program to get a CTF trace
271 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev
/null
; then
272 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
273 rm -rf "$temp_trace_dir"
277 # Compare using the CLI with `sink.text.details`
278 bt_diff_details_ctf_single
"$expected_stdout_file" "$temp_trace_dir" \
279 "${extra_details_args[@]+${extra_details_args[@]}}"
281 rm -rf "$temp_trace_dir"
292 # Execute a shell command in the appropriate environment to have access to the
293 # bt2 Python bindings.
298 "BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1" \
299 "BABELTRACE_PLUGIN_PATH=${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \
300 "LIBBABELTRACE2_PLUGIN_PROVIDER_DIR=${BT_TESTS_PROVIDER_DIR}" \
301 "BT_TESTS_DATADIR=${BT_TESTS_DATADIR}" \
302 "BT_CTF_TRACES_PATH=${BT_CTF_TRACES_PATH}" \
303 "BT_PLUGINS_PATH=${BT_PLUGINS_PATH}" \
304 "PYTHONPATH=${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python"
307 local main_lib_path
="${BT_TESTS_BUILDDIR}/../src/lib/.libs"
309 # Set the library search path so the python interpreter can load libbabeltrace2
310 if [ "$BT_TESTS_OS_TYPE" = "mingw" ] ||
[ "$BT_TESTS_OS_TYPE" = "cygwin" ]; then
311 env_args
+=("PATH=${main_lib_path}:${PATH:-}")
312 elif [ "$BT_TESTS_OS_TYPE" = "darwin" ]; then
313 env_args
+=("DYLD_LIBRARY_PATH=${main_lib_path}:${DYLD_LIBRARY_PATH:-}")
315 env_args
+=("LD_LIBRARY_PATH=${main_lib_path}:${LD_LIBRARY_PATH:-}")
318 # On Windows, an embedded Python interpreter needs a way to locate the path
319 # to it's internal modules, set the prefix from python-config to the
320 # PYTHONHOME variable.
321 if [ "$BT_TESTS_OS_TYPE" = "mingw" ]; then
322 env_args
+=("PYTHONHOME=$($BT_TESTS_PYTHON_CONFIG_BIN --prefix)")
325 env
"${env_args[@]}" "$@"
328 # Set the environment and run python tests in the directory.
330 # $1 : The directory containing the python test scripts
331 # $2 : The pattern to match python test script names (optional)
332 run_python_bt2_test
() {
334 local test_pattern
="${2:-'*'}" # optional, if none default to "*"
337 local test_runner_args
=()
339 test_runner_args
+=("$test_dir")
340 if [ "x${test_pattern}" != "x" ]; then
341 test_runner_args
+=("${test_pattern}")
344 if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then
345 python_exec
="check_coverage"
347 python_exec
="${BT_TESTS_PYTHON_BIN}"
352 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
353 --pattern "$test_pattern" \
358 if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then
362 if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then