3 # SPDX-License-Identifier: GPL-2.0-only
5 # Copyright (c) 2019 Michael Jeanson <mjeanson@efficios.com>
6 # Copyright (C) 2019-2023 Philippe Proulx <pproulx@efficios.com>
8 # Source this file at the beginning of a shell script test to access
9 # useful testing variables and functions:
13 # if [[ -n ${BT_TESTS_SRCDIR:-} ]]; then
14 # UTILSSH=$BT_TESTS_SRCDIR/utils/utils.sh
16 # UTILSSH=$(dirname "$0")/../utils/utils.sh
19 # # shellcheck source=../utils/utils.sh
22 # Make sure the relative path to `utils.sh` (this file) above is
25 # An unbound variable is an error
28 # Name of the OS on which we're running, if not set.
32 # `mingw`: MinGW (Windows)
35 # `cygwin`: Cygwin (Windows)
36 # `unsupported`: Anything else
38 # See <https://en.wikipedia.org/wiki/Uname#Examples> for possible values
41 # Do some translation to ease our life down the road for comparison.
42 # Export it so that executed commands can use it.
43 if [ -z "${BT_TESTS_OS_TYPE:-}" ]; then
44 BT_TESTS_OS_TYPE
="$(uname -s)"
45 case "$BT_TESTS_OS_TYPE" in
47 BT_TESTS_OS_TYPE
="mingw"
50 BT_TESTS_OS_TYPE
="darwin"
53 BT_TESTS_OS_TYPE
="linux"
56 BT_TESTS_OS_TYPE
="cygwin"
59 BT_TESTS_OS_TYPE
="unsupported"
63 export BT_TESTS_OS_TYPE
65 # Sets and exports, if not set:
67 # • `BT_TESTS_SRCDIR` to the base source directory of tests.
68 # • `BT_TESTS_BUILDDIR` to the base build directory of tests.
69 _set_vars_srcdir_builddir
() {
70 # If `readlink -f` is available, then get a resolved absolute path
71 # to the tests source directory. Otherwise, make do with a relative
73 local -r scriptdir
="$(dirname "${BASH_SOURCE[0]}")"
76 if readlink
-f "." >/dev
/null
2>&1; then
77 testsdir
=$
(readlink
-f "$scriptdir/..")
79 testsdir
="$scriptdir/.."
82 # Base source directory of tests
83 if [ -z "${BT_TESTS_SRCDIR:-}" ]; then
84 BT_TESTS_SRCDIR
="$testsdir"
86 export BT_TESTS_SRCDIR
88 # Base build directory of tests
89 if [ -z "${BT_TESTS_BUILDDIR:-}" ]; then
90 BT_TESTS_BUILDDIR
="$testsdir"
92 export BT_TESTS_BUILDDIR
95 _set_vars_srcdir_builddir
96 unset -f _set_vars_srcdir_builddir
98 # Sources the generated environment file (`env.sh`) if it exists.
100 local -r env_sh_path
="$BT_TESTS_BUILDDIR/utils/env.sh"
102 if [ -f "${env_sh_path}" ]; then
103 # shellcheck disable=SC1090,SC1091
109 unset -f _source_env_sh
111 # Path to the `babeltrace2` command, if not set
112 if [ -z "${BT_TESTS_BT2_BIN:-}" ]; then
113 BT_TESTS_BT2_BIN
="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2"
114 if [ "$BT_TESTS_OS_TYPE" = "mingw" ]; then
115 BT_TESTS_BT2_BIN
="${BT_TESTS_BT2_BIN}.exe"
118 export BT_TESTS_BT2_BIN
120 # This doesn't need to be exported, but it needs to remain set for
121 # run_python_bt2() to use it.
123 # TODO: Remove when `tests/bindings/python/bt2/test_plugin.py` is fixed.
124 BT_PLUGINS_PATH
="${BT_TESTS_BUILDDIR}/../src/plugins"
126 # Colon-separated list of project plugin paths, if not set
127 if [ -z "${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" ]; then
128 BT_TESTS_BABELTRACE_PLUGIN_PATH
="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text:${BT_PLUGINS_PATH}/lttng-utils"
130 export BT_TESTS_BABELTRACE_PLUGIN_PATH
132 # Directory containing the Python plugin provider library, if not set
133 if [ -z "${BT_TESTS_PROVIDER_DIR:-}" ]; then
134 BT_TESTS_PROVIDER_DIR
="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs"
136 export BT_TESTS_PROVIDER_DIR
138 # Directory containing the built `bt2` Python package, if not set
139 if [ -z "${BT_TESTS_PYTHONPATH:-}" ]; then
140 BT_TESTS_PYTHONPATH
="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib"
142 export BT_TESTS_PYTHONPATH
144 # Name of the `awk` command to use when testing, if not set
145 if [ -z "${BT_TESTS_AWK_BIN:-}" ]; then
146 BT_TESTS_AWK_BIN
="awk"
148 export BT_TESTS_AWK_BIN
150 # Name of the `grep` command to use when testing, if not set
151 if [ -z "${BT_TESTS_GREP_BIN:-}" ]; then
152 BT_TESTS_GREP_BIN
="grep"
154 export BT_TESTS_GREP_BIN
156 # Name of the `python3` command to use when testing, if not set
157 if [ -z "${BT_TESTS_PYTHON_BIN:-}" ]; then
158 BT_TESTS_PYTHON_BIN
="python3"
160 export BT_TESTS_PYTHON_BIN
162 # Major and minor version of the `python3` command to use when testing.
164 # This doesn't need to be exported, but it needs to remain set for
165 # run_python() to use it.
166 BT_TESTS_PYTHON_VERSION
=$
("$BT_TESTS_PYTHON_BIN" -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))')
168 # Name of the `python3-config` command to use when testing, if not set
169 if [ -z "${BT_TESTS_PYTHON_CONFIG_BIN:-}" ]; then
170 BT_TESTS_PYTHON_CONFIG_BIN
="python3-config"
172 export BT_TESTS_PYTHON_CONFIG_BIN
174 # Name of the `sed` command to use when testing, if not set
175 if [ -z "${BT_TESTS_SED_BIN:-}" ]; then
176 BT_TESTS_SED_BIN
="sed"
178 export BT_TESTS_SED_BIN
180 # Name of the `cc` command to use when testing, if not set
181 if [ -z "${BT_TESTS_CC_BIN:-}" ]; then
184 export BT_TESTS_CC_BIN
186 # Whether or not to enable AddressSanitizer, `0` (disabled) if not set.
187 if [ -z "${BT_TESTS_ENABLE_ASAN:-}" ]; then
188 BT_TESTS_ENABLE_ASAN
="0"
190 export BT_TESTS_ENABLE_ASAN
192 # Directory containing test data
193 BT_TESTS_DATADIR
="${BT_TESTS_SRCDIR}/data"
195 # Directory containing test CTF traces
196 BT_CTF_TRACES_PATH
="${BT_TESTS_DATADIR}/ctf-traces"
198 # Source the shell TAP utilities if `SH_TAP` is `1`
199 if [ "${SH_TAP:-}" = 1 ]; then
200 # shellcheck source=./tap/tap.sh
201 .
"${BT_TESTS_SRCDIR}/utils/tap/tap.sh"
204 # Removes the CR characters from the file having the path `$1`.
206 # This is sometimes needed on Windows with text files.
208 # We can't use the `--string-trailing-cr` option of `diff` because
209 # Solaris doesn't have it.
211 "$BT_TESTS_SED_BIN" -i'' -e 's/\r//g' "$1"
214 # Prints `$1` without CR characters.
215 bt_remove_cr_inline
() {
216 "$BT_TESTS_SED_BIN" 's/\r//g' "$1"
219 # Runs the `$BT_TESTS_BT2_BIN` command within an environment which can
220 # import the `bt2` Python package, redirecting the standard output to
221 # the `$1` file and the standard error to the `$2` file.
223 # The remaining arguments are forwarded to the `$BT_TESTS_BT2_BIN`
226 # Returns the exit status of the executed `$BT_TESTS_BT2_BIN`.
228 local stdout_file
="$1"
229 local stderr_file
="$2"
233 echo "Running: $BT_TESTS_BT2_BIN ${args[*]}" >&2
234 run_python_bt2
"$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$stdout_file" 2>"$stderr_file"
237 # Checks the differences between:
239 # • The (expected) contents of the file having the path `$1`.
241 # • The contents of another file having the path `$2`.
243 # Both files are passed through bt_remove_cr_inline() to remove CR
246 # Returns 0 if there's no difference, or not zero otherwise.
248 local expected_file
="$1"
249 local actual_file
="$2"
252 diff -u <(bt_remove_cr_inline
"$expected_file") <(bt_remove_cr_inline
"$actual_file") 1>&2
257 # Checks the difference between:
259 # • What the `$BT_TESTS_BT2_BIN` command prints to its standard output
260 # when given the third and following arguments of this function.
262 # • The file having the path `$1`.
264 # as well as the difference between:
266 # • What the `$BT_TESTS_BT2_BIN` command prints to its standard error
267 # when given the third and following arguments of this function.
269 # • The file having the path `$2`.
271 # Returns 0 if there's no difference, or 1 otherwise, also printing said
272 # difference to the standard error.
274 local expected_stdout_file
="$1"
275 local expected_stderr_file
="$2"
279 local temp_stdout_output_file
280 local temp_stderr_output_file
285 temp_stdout_output_file
="$(mktemp -t actual-stdout.XXXXXX)"
286 temp_stderr_output_file
="$(mktemp -t actual-stderr.XXXXXX)"
288 bt_cli
"$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}"
290 bt_diff
"$expected_stdout_file" "$temp_stdout_output_file" "${args[@]}"
292 bt_diff
"$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}"
295 if ((ret_stdout
!= 0 || ret_stderr
!= 0)); then
299 rm -f "$temp_stdout_output_file" "$temp_stderr_output_file"
304 # Checks the difference between:
306 # • The content of the file having the path `$1`.
308 # • What the `$BT_TESTS_BT2_BIN` command prints to the standard output
309 # when executed with:
311 # 1. The CTF trace directory `$2`.
312 # 2. The arguments `-c` and `sink.text.details`.
313 # 3. The third and following arguments of this function.
315 # Returns 0 if there's no difference, or 1 otherwise, also printing said
316 # difference to the standard error.
317 bt_diff_details_ctf_single
() {
318 local expected_stdout_file
="$1"
321 local extra_details_args
=("$@")
322 expected_stderr_file
="/dev/null"
324 # Compare using the CLI with `sink.text.details`
325 bt_diff_cli
"$expected_stdout_file" "$expected_stderr_file" "$trace_dir" \
326 "-c" "sink.text.details" "${extra_details_args[@]+${extra_details_args[@]}}"
329 # Like bt_diff_details_ctf_single(), except that `$1` is the path to a
330 # program which generates the CTF trace to compare to.
332 # The program `$1` receives the path to a temporary, empty directory
333 # where to write the CTF trace as its first argument.
334 bt_diff_details_ctf_gen_single
() {
335 local ctf_gen_prog_path
="$1"
336 local expected_stdout_file
="$2"
338 local extra_details_args
=("$@")
343 temp_trace_dir
="$(mktemp -d)"
345 # Run the CTF trace generator program to get a CTF trace
346 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev
/null
; then
347 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
348 rm -rf "$temp_trace_dir"
352 # Compare using the CLI with `sink.text.details`
353 bt_diff_details_ctf_single
"$expected_stdout_file" "$temp_trace_dir" \
354 "${extra_details_args[@]+${extra_details_args[@]}}"
356 rm -rf "$temp_trace_dir"
360 # Like `grep`, but using `$BT_TESTS_GREP_BIN`.
362 "$BT_TESTS_GREP_BIN" "$@"
365 # ok() with the test name `$3` on the result of bt_grep() matching the
366 # pattern `$1` within the file `$2`.
372 bt_grep
--silent "$pattern" "$file"
376 if ! ok
$ret "$test_name"; then
378 echo "Pattern \`$pattern\` doesn't match the contents of \`$file\`:"
388 # Forwards the arguments to `coverage run`.
393 # Executes a command within an environment which can import the testing
394 # Python modules (in `tests/utils/python`).
396 local our_pythonpath
="${BT_TESTS_SRCDIR}/utils/python"
398 if [[ $BT_TESTS_PYTHON_VERSION =~
3.
[45] ]]; then
399 # Add a local directory containing a `typing.py` to `PYTHONPATH`
400 # for Python 3.4 and Python 3.5 which either don't offer the
401 # `typing` module at all, or offer a partial one.
402 our_pythonpath
="$our_pythonpath:${BT_TESTS_SRCDIR}/utils/python/typing"
405 PYTHONPATH
="${our_pythonpath}${PYTHONPATH:+:}${PYTHONPATH:-}" "$@"
408 # Executes a command within an environment which can import the testing
409 # Python modules (in `tests/utils/python`) and the `bt2` Python package.
412 local -x "BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1"
413 local -x "BABELTRACE_PLUGIN_PATH=${BT_TESTS_BABELTRACE_PLUGIN_PATH}"
414 local -x "LIBBABELTRACE2_PLUGIN_PROVIDER_DIR=${BT_TESTS_PROVIDER_DIR}"
415 local -x "BT_TESTS_DATADIR=${BT_TESTS_DATADIR}"
416 local -x "BT_CTF_TRACES_PATH=${BT_CTF_TRACES_PATH}"
417 local -x "BT_PLUGINS_PATH=${BT_PLUGINS_PATH}"
418 local -x "PYTHONPATH=${BT_TESTS_PYTHONPATH}${PYTHONPATH:+:}${PYTHONPATH:-}"
420 local main_lib_path
="${BT_TESTS_BUILDDIR}/../src/lib/.libs"
422 # Set the library search path so that the Python 3 interpreter can
423 # load `libbabeltrace2`.
424 if [ "$BT_TESTS_OS_TYPE" = "mingw" ] ||
[ "$BT_TESTS_OS_TYPE" = "cygwin" ]; then
425 local -x PATH
="${main_lib_path}${PATH:+:}${PATH:-}"
426 elif [ "$BT_TESTS_OS_TYPE" = "darwin" ]; then
427 local -x DYLD_LIBRARY_PATH
="${main_lib_path}${DYLD_LIBRARY_PATH:+:}${DYLD_LIBRARY_PATH:-}"
429 local -x LD_LIBRARY_PATH
="${main_lib_path}${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH:-}"
432 # On Windows, an embedded Python 3 interpreter needs a way to locate
433 # the path to its internal modules: set the `PYTHONHOME` variable to
434 # the prefix from `python3-config`.
435 if [ "$BT_TESTS_OS_TYPE" = "mingw" ]; then
438 PYTHONHOME
=$
("$BT_TESTS_PYTHON_CONFIG_BIN" --prefix)
441 # If AddressSanitizer is used, we must preload `libasan.so` so that
442 # libasan doesn't complain about not being the first loaded library.
444 # Python and sed (executed as part of the Libtool wrapper) produce
445 # some leaks, so we must unfortunately disable leak detection.
447 # Append it to existing `ASAN_OPTIONS` variable, such that we
448 # override the user's value if it contains `detect_leaks=1`.
449 if [ "${BT_TESTS_ENABLE_ASAN:-}" = "1" ]; then
450 if "${BT_TESTS_CC_BIN}" --version |
head -n 1 | bt_grep
-q '^gcc'; then
451 lib_asan
="$("${BT_TESTS_CC_BIN}" -print-file-name=libasan.so)"
452 local -x LD_PRELOAD
="${lib_asan}${LD_PRELOAD:+:}${LD_PRELOAD:-}"
455 local -x "ASAN_OPTIONS=${ASAN_OPTIONS:-}${ASAN_OPTIONS:+,}detect_leaks=0"
461 # Runs the Python tests matching the pattern `$2` (optional, `*` if
462 # missing) in the directory `$1` using `testrunner.py`.
464 # This function uses run_python_bt2(), therefore such tests can import
465 # the testing Python modules (in `tests/utils/python`) and the `bt2`
467 run_python_bt2_test
() {
469 local test_pattern
="${2:-'*'}"
472 local test_runner_args
=()
474 test_runner_args
+=("$test_dir")
475 if [ -n "${test_pattern}" ]; then
476 test_runner_args
+=("${test_pattern}")
479 if test "${BT_TESTS_COVERAGE:-}" = "1"; then
480 python_exec
="check_coverage"
482 python_exec
="${BT_TESTS_PYTHON_BIN}"
487 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
488 --pattern "$test_pattern" \
493 if test "${BT_TESTS_COVERAGE_REPORT:-}" = "1"; then
497 if test "${BT_TESTS_COVERAGE_HTML:-}" = "1"; then
504 # Generates a CTF trace into the directory `$2` from the moultipart
505 # document `$1` using `mctf.py`.
507 local input_file
="$1"
510 diag
"Running: ${BT_TESTS_PYTHON_BIN} ${BT_TESTS_SRCDIR}/utils/python/mctf.py --base-dir ${base_dir} ${input_file}"
511 run_python
"${BT_TESTS_PYTHON_BIN}" "${BT_TESTS_SRCDIR}/utils/python/mctf.py" \
512 --base-dir "${base_dir}" "${input_file}"