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 # Sets the variable named `$1` to `$2` if it's not set, and exports it.
29 _bt_tests_set_var_def
() {
33 if [[ -z ${!varname:-} ]]; then
34 eval "$varname='$val'"
40 # Name of the OS on which we're running, if not set.
44 # `mingw`: MinGW (Windows)
47 # `cygwin`: Cygwin (Windows)
48 # `unsupported`: Anything else
50 # See <https://en.wikipedia.org/wiki/Uname#Examples> for possible values
53 # Do some translation to ease our life down the road for comparison.
54 # Export it so that executed commands can use it.
55 if [[ -z ${BT_TESTS_OS_TYPE:-} ]]; then
56 BT_TESTS_OS_TYPE
=$
(uname
-s)
58 case $BT_TESTS_OS_TYPE in
60 BT_TESTS_OS_TYPE
=mingw
63 BT_TESTS_OS_TYPE
=darwin
66 BT_TESTS_OS_TYPE
=linux
69 BT_TESTS_OS_TYPE
=cygwin
72 BT_TESTS_OS_TYPE
=unsupported
77 export BT_TESTS_OS_TYPE
79 # Sets and exports, if not set:
81 # • `BT_TESTS_SRCDIR` to the base source directory of tests.
82 # • `BT_TESTS_BUILDDIR` to the base build directory of tests.
83 _set_vars_srcdir_builddir
() {
84 # If `readlink -f` is available, then get a resolved absolute path
85 # to the tests source directory. Otherwise, make do with a relative
87 local -r scriptdir
=$
(dirname "${BASH_SOURCE[0]}")
90 if readlink
-f .
&> /dev
/null
; then
91 testsdir
=$
(readlink
-f "$scriptdir/..")
93 testsdir
=$scriptdir/..
96 # Base source directory of tests
97 _bt_tests_set_var_def BT_TESTS_SRCDIR
"$testsdir"
99 # Base build directory of tests
100 _bt_tests_set_var_def BT_TESTS_BUILDDIR
"$testsdir"
103 _set_vars_srcdir_builddir
104 unset -f _set_vars_srcdir_builddir
106 # Sources the generated environment file (`env.sh`) if it exists.
108 local -r env_sh_path
=$BT_TESTS_BUILDDIR/utils
/env.sh
110 if [[ -f $env_sh_path ]]; then
111 # shellcheck disable=SC1090,SC1091
117 unset -f _source_env_sh
119 # Path to the `babeltrace2` command, if not set
120 if [[ -z ${BT_TESTS_BT2_BIN:-} ]]; then
121 BT_TESTS_BT2_BIN
=$BT_TESTS_BUILDDIR/..
/src
/cli
/babeltrace2
123 if [[ $BT_TESTS_OS_TYPE == mingw
]]; then
124 BT_TESTS_BT2_BIN
+=.exe
128 export BT_TESTS_BT2_BIN
130 # This doesn't need to be exported, but it needs to remain set for
131 # bt_run_in_py_env() to use it.
133 # TODO: Remove when `tests/bindings/python/bt2/test_plugin.py` is fixed.
134 _bt_tests_plugins_path
=$BT_TESTS_BUILDDIR/..
/src
/plugins
136 # Colon-separated list of project plugin paths, if not set
137 _bt_tests_set_var_def BT_TESTS_BABELTRACE_PLUGIN_PATH \
138 "$_bt_tests_plugins_path/ctf:$_bt_tests_plugins_path/utils:$_bt_tests_plugins_path/text:$_bt_tests_plugins_path/lttng-utils"
140 # Directory containing the Python plugin provider library, if not set
141 _bt_tests_set_var_def BT_TESTS_PROVIDER_DIR
"$BT_TESTS_BUILDDIR/../src/python-plugin-provider/.libs"
143 # Directory containing the built `bt2` Python package, if not set
144 _bt_tests_set_var_def BT_TESTS_PYTHONPATH
"$BT_TESTS_BUILDDIR/../src/bindings/python/bt2/build/build_lib"
146 # Name of the `awk` command to use when testing, if not set
147 _bt_tests_set_var_def BT_TESTS_AWK_BIN
awk
149 # Name of the `grep` command to use when testing, if not set
150 _bt_tests_set_var_def BT_TESTS_GREP_BIN
grep
152 # Name of the `python3` command to use when testing, if not set
153 _bt_tests_set_var_def BT_TESTS_PYTHON_BIN python3
155 # Major and minor version of the `python3` command to use when testing.
157 # This doesn't need to be exported, but it needs to remain set for
158 # bt_run_in_py_utils_env() to use it.
159 _bt_tests_py3_version
=$
($BT_TESTS_PYTHON_BIN -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))')
161 # Name of the `python3-config` command to use when testing, if not set
162 _bt_tests_set_var_def BT_TESTS_PYTHON_CONFIG_BIN python3-config
164 # Name of the `sed` command to use when testing, if not set
165 _bt_tests_set_var_def BT_TESTS_SED_BIN
sed
167 # Name of the `cc` command to use when testing, if not set
168 _bt_tests_set_var_def BT_TESTS_CC_BIN cc
170 # Done with _bt_tests_set_var_def()
171 unset -f _bt_tests_set_var_def
173 # Whether or not to enable AddressSanitizer, `0` (disabled) if not set.
175 # This doesn't need to be exported from the point of view of this file,
176 # but the sourced `env.sh` above does export it.
177 if [[ -z ${BT_TESTS_ENABLE_ASAN:-} ]]; then
178 BT_TESTS_ENABLE_ASAN
=0
181 # Directory containing test data
182 BT_TESTS_DATADIR
=$BT_TESTS_SRCDIR/data
184 # Directory containing test CTF traces
185 BT_CTF_TRACES_PATH
=$BT_TESTS_DATADIR/ctf-traces
187 # Source the shell TAP utilities if `SH_TAP` is `1`
188 if [[ ${SH_TAP:-} == 1 ]]; then
189 # shellcheck source=./tap/tap.sh
190 .
"$BT_TESTS_SRCDIR/utils/tap/tap.sh"
193 # Removes the CR characters from the file having the path `$1`.
195 # This is sometimes needed on Windows with text files.
197 # We can't use the `--string-trailing-cr` option of `diff` because
198 # Solaris doesn't have it.
200 "$BT_TESTS_SED_BIN" -i'' -e 's/\r//g' "$1"
203 # Prints `$1` without CR characters.
204 bt_remove_cr_inline
() {
205 "$BT_TESTS_SED_BIN" 's/\r//g' "$1"
208 # Runs the `$BT_TESTS_BT2_BIN` command within an environment which can
209 # import the `bt2` Python package, redirecting the standard output to
210 # the `$1` file and the standard error to the `$2` file.
212 # The remaining arguments are forwarded to the `$BT_TESTS_BT2_BIN`
215 # Returns the exit status of the executed `$BT_TESTS_BT2_BIN`.
217 local -r stdout_file
=$1
218 local -r stderr_file
=$2
222 local -a bt_cli_args
=("$@")
224 echo "Running: \`$BT_TESTS_BT2_BIN ${bt_cli_args[*]}\`" >&2
225 bt_run_in_py_env
"$BT_TESTS_BT2_BIN" "${bt_cli_args[@]}" 1>"$stdout_file" 2>"$stderr_file"
228 # Checks the differences between:
230 # • The (expected) contents of the file having the path `$1`.
232 # • The contents of another file having the path `$2`.
234 # Both files are passed through bt_remove_cr_inline() to remove CR
237 # Returns 0 if there's no difference, or not zero otherwise.
239 local -r expected_file
=$1
240 local -r actual_file
=$2
242 diff -u <(bt_remove_cr_inline
"$expected_file") <(bt_remove_cr_inline
"$actual_file") 1>&2
245 # Checks the difference between:
247 # • What the `$BT_TESTS_BT2_BIN` command prints to its standard output
248 # when given the third and following arguments of this function.
250 # • The file having the path `$1`.
252 # as well as the difference between:
254 # • What the `$BT_TESTS_BT2_BIN` command prints to its standard error
255 # when given the third and following arguments of this function.
257 # • The file having the path `$2`.
259 # Returns 0 if there's no difference, or 1 otherwise, also printing said
260 # difference to the standard error.
262 local -r expected_stdout_file
=$1
263 local -r expected_stderr_file
=$2
268 local -r temp_stdout_output_file
=$
(mktemp
-t actual-stdout.XXXXXX
)
269 local -r temp_stderr_output_file
=$
(mktemp
-t actual-stderr.XXXXXX
)
271 bt_cli
"$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}"
272 bt_diff
"$expected_stdout_file" "$temp_stdout_output_file" "${args[@]}"
274 local -r ret_stdout
=$?
276 bt_diff
"$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}"
278 local -r ret_stderr
=$?
280 rm -f "$temp_stdout_output_file" "$temp_stderr_output_file"
281 return $
((ret_stdout || ret_stderr
))
284 # Checks the difference between:
286 # • The content of the file having the path `$1`.
288 # • What the `$BT_TESTS_BT2_BIN` command prints to the standard output
289 # when executed with:
291 # 1. The CTF trace directory `$2`.
292 # 2. The arguments `-c` and `sink.text.details`.
293 # 3. The third and following arguments of this function.
295 # Returns 0 if there's no difference, or 1 otherwise, also printing said
296 # difference to the standard error.
297 bt_diff_details_ctf_single
() {
298 local -r expected_stdout_file
=$1
299 local -r trace_dir
=$2
303 local -r extra_details_args
=("$@")
305 # Compare using the CLI with `sink.text.details`
306 bt_diff_cli
"$expected_stdout_file" /dev
/null
"$trace_dir" \
307 -c sink.text.details
"${extra_details_args[@]+${extra_details_args[@]}}"
310 # Like bt_diff_details_ctf_single(), except that `$1` is the path to a
311 # program which generates the CTF trace to compare to.
313 # The program `$1` receives the path to a temporary, empty directory
314 # where to write the CTF trace as its first argument.
315 bt_diff_details_ctf_gen_single
() {
316 local -r ctf_gen_prog_path
=$1
317 local -r expected_stdout_file
=$2
321 local -r gen_extra_details_args
=("$@")
322 local -r temp_trace_dir
=$
(mktemp
-d)
324 # Run the CTF trace generator program to get a CTF trace
325 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev
/null
; then
326 echo "ERROR: \`$ctf_gen_prog_path $temp_trace_dir\` failed" >&2
327 rm -rf "$temp_trace_dir"
331 # Compare using the CLI with `sink.text.details`
332 bt_diff_details_ctf_single
"$expected_stdout_file" "$temp_trace_dir" \
333 "${gen_extra_details_args[@]+${gen_extra_details_args[@]}}"
337 rm -rf "$temp_trace_dir"
341 # Like `grep`, but using `$BT_TESTS_GREP_BIN`.
343 "$BT_TESTS_GREP_BIN" "$@"
346 # Only if `tap.sh` is sourced because bt_grep_ok() uses ok()
347 if [[ ${SH_TAP:-} == 1 ]]; then
348 # ok() with the test name `$3` on the result of bt_grep() matching
349 # the pattern `$1` within the file `$2`.
353 local -r test_name
=$3
355 bt_grep
--silent "$pattern" "$file"
359 if ! ok
$ret "$test_name"; then
361 echo "Pattern \`$pattern\` doesn't match the contents of \`$file\`:"
372 # Forwards the arguments to `coverage run`.
373 _bt_tests_check_coverage
() {
377 # Executes a command within an environment which can import the testing
378 # Python modules (in `tests/utils/python`).
379 bt_run_in_py_utils_env
() {
380 local our_pythonpath
=$BT_TESTS_SRCDIR/utils
/python
382 if [[ $_bt_tests_py3_version =~
3.
[45] ]]; then
383 # Add a local directory containing a `typing.py` to `PYTHONPATH`
384 # for Python 3.4 and Python 3.5 which either don't offer the
385 # `typing` module at all, or offer a partial one.
386 our_pythonpath
=$our_pythonpath:$BT_TESTS_SRCDIR/utils
/python
/typing
389 PYTHONPATH
=$our_pythonpath${PYTHONPATH:+:}${PYTHONPATH:-} "$@"
392 # Executes a command within an environment which can import the testing
393 # Python modules (in `tests/utils/python`) and the `bt2` Python package.
395 local -x BABELTRACE_PLUGIN_PATH
=$BT_TESTS_BABELTRACE_PLUGIN_PATH
396 local -x LIBBABELTRACE2_PLUGIN_PROVIDER_DIR
=$BT_TESTS_PROVIDER_DIR
397 local -x BT_TESTS_DATADIR
=$BT_TESTS_DATADIR
398 local -x BT_CTF_TRACES_PATH
=$BT_CTF_TRACES_PATH
399 local -x BT_PLUGINS_PATH
=$_bt_tests_plugins_path
400 local -x PYTHONPATH
=$BT_TESTS_PYTHONPATH${PYTHONPATH:+:}${PYTHONPATH:-}
401 local -r main_lib_path
=$BT_TESTS_BUILDDIR/..
/src
/lib
/.libs
403 # Set the library search path so that the Python 3 interpreter can
404 # load `libbabeltrace2`.
405 if [[ $BT_TESTS_OS_TYPE == mingw ||
$BT_TESTS_OS_TYPE == cygwin
]]; then
406 local -x PATH
=$main_lib_path${PATH:+:}${PATH:-}
407 elif [[ $BT_TESTS_OS_TYPE == darwin
]]; then
408 local -x DYLD_LIBRARY_PATH
=$main_lib_path${DYLD_LIBRARY_PATH:+:}${DYLD_LIBRARY_PATH:-}
410 local -x LD_LIBRARY_PATH
=$main_lib_path${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH:-}
413 # On Windows, an embedded Python 3 interpreter needs a way to locate
414 # the path to its internal modules: set the `PYTHONHOME` variable to
415 # the prefix from `python3-config`.
416 if [[ $BT_TESTS_OS_TYPE == mingw
]]; then
419 PYTHONHOME
=$
($BT_TESTS_PYTHON_CONFIG_BIN --prefix)
422 # If AddressSanitizer is used, we must preload `libasan.so` so that
423 # libasan doesn't complain about not being the first loaded library.
425 # Python and sed (executed as part of the Libtool wrapper) produce
426 # some leaks, so we must unfortunately disable leak detection.
428 # Append it to existing `ASAN_OPTIONS` variable, such that we
429 # override the user's value if it contains `detect_leaks=1`.
430 if [[ ${BT_TESTS_ENABLE_ASAN:-} == 1 ]]; then
431 if $BT_TESTS_CC_BIN --version |
head -n 1 | bt_grep
-q '^gcc'; then
432 local -r lib_asan
=$
($BT_TESTS_CC_BIN -print-file-name=libasan.so
)
433 local -x LD_PRELOAD
=$lib_asan${LD_PRELOAD:+:}${LD_PRELOAD:-}
436 local -x ASAN_OPTIONS
=${ASAN_OPTIONS:-}${ASAN_OPTIONS:+,}detect_leaks
=0
439 bt_run_in_py_utils_env
"$@"
442 # Runs the Python tests matching the pattern `$2` (optional, `*` if
443 # missing) in the directory `$1` using `testrunner.py`.
445 # This function uses bt_run_in_py_env(), therefore such tests can import
446 # the testing Python modules (in `tests/utils/python`) and the `bt2`
450 local -r test_pattern
=${2:-*}
453 if [[ ${BT_TESTS_COVERAGE:-} == 1 ]]; then
454 python_exec
=_bt_tests_check_coverage
456 python_exec
=$BT_TESTS_PYTHON_BIN
460 "$python_exec" "$BT_TESTS_SRCDIR/utils/python/testrunner.py" \
461 --pattern "$test_pattern" "$test_dir"
465 if [[ ${BT_TESTS_COVERAGE_REPORT:-} == 1 ]]; then
469 if [[ ${BT_TESTS_COVERAGE_HTML:-} == 1 ]]; then
476 # Generates a CTF trace into the directory `$2` from the moultipart
477 # document `$1` using `mctf.py`.
478 bt_gen_mctf_trace
() {
479 local -r input_file
=$1
482 "$BT_TESTS_PYTHON_BIN" "$BT_TESTS_SRCDIR/utils/python/mctf.py"
483 --base-dir "$base_dir"
487 echo "Running: \`${cmd[*]}\`" >&2
488 bt_run_in_py_utils_env
"${cmd[@]}"
491 # Call `diag` with the contents of file `$1`.