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 if [[ ! -e $expected_file ]]; then
243 echo "ERROR: expected file \`$expected_file\` doesn't exist" >&2
247 if [[ ! -e $actual_file ]]; then
248 echo "ERROR: actual file \`$actual_file\` doesn't exist" >&2
252 diff -u <(bt_remove_cr_inline
"$expected_file") <(bt_remove_cr_inline
"$actual_file") 1>&2
255 # Checks the difference between:
257 # • What the `$BT_TESTS_BT2_BIN` command prints to its standard output
258 # when given the third and following arguments of this function.
260 # • The file having the path `$1`.
262 # as well as the difference between:
264 # • What the `$BT_TESTS_BT2_BIN` command prints to its standard error
265 # when given the third and following arguments of this function.
267 # • The file having the path `$2`.
269 # Returns 0 if there's no difference, or 1 otherwise, also printing said
270 # difference to the standard error.
272 local -r expected_stdout_file
=$1
273 local -r expected_stderr_file
=$2
278 local -r temp_stdout_output_file
=$
(mktemp
-t actual-stdout.XXXXXX
)
279 local -r temp_stderr_output_file
=$
(mktemp
-t actual-stderr.XXXXXX
)
281 bt_cli
"$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}"
282 bt_diff
"$expected_stdout_file" "$temp_stdout_output_file" "${args[@]}"
284 local -r ret_stdout
=$?
286 bt_diff
"$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}"
288 local -r ret_stderr
=$?
290 rm -f "$temp_stdout_output_file" "$temp_stderr_output_file"
291 return $
((ret_stdout || ret_stderr
))
294 # Checks the difference between:
296 # • The content of the file having the path `$1`.
298 # • What the `$BT_TESTS_BT2_BIN` command prints to the standard output
299 # when executed with:
301 # 1. The CTF trace directory `$2`.
302 # 2. The arguments `-c` and `sink.text.details`.
303 # 3. The third and following arguments of this function.
305 # Returns 0 if there's no difference, or 1 otherwise, also printing said
306 # difference to the standard error.
307 bt_diff_details_ctf_single
() {
308 local -r expected_stdout_file
=$1
309 local -r trace_dir
=$2
313 local -r extra_details_args
=("$@")
315 # Compare using the CLI with `sink.text.details`
316 bt_diff_cli
"$expected_stdout_file" /dev
/null
"$trace_dir" \
317 -c sink.text.details
"${extra_details_args[@]+${extra_details_args[@]}}"
320 # Like bt_diff_details_ctf_single(), except that `$1` is the path to a
321 # program which generates the CTF trace to compare to.
323 # The program `$1` receives the path to a temporary, empty directory
324 # where to write the CTF trace as its first argument.
325 bt_diff_details_ctf_gen_single
() {
326 local -r ctf_gen_prog_path
=$1
327 local -r expected_stdout_file
=$2
331 local -r gen_extra_details_args
=("$@")
332 local -r temp_trace_dir
=$
(mktemp
-d)
334 # Run the CTF trace generator program to get a CTF trace
335 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev
/null
; then
336 echo "ERROR: \`$ctf_gen_prog_path $temp_trace_dir\` failed" >&2
337 rm -rf "$temp_trace_dir"
341 # Compare using the CLI with `sink.text.details`
342 bt_diff_details_ctf_single
"$expected_stdout_file" "$temp_trace_dir" \
343 "${gen_extra_details_args[@]+${gen_extra_details_args[@]}}"
347 rm -rf "$temp_trace_dir"
351 # Like `grep`, but using `$BT_TESTS_GREP_BIN`.
353 "$BT_TESTS_GREP_BIN" "$@"
356 # Only if `tap.sh` is sourced because bt_grep_ok() uses ok()
357 if [[ ${SH_TAP:-} == 1 ]]; then
358 # ok() with the test name `$3` on the result of bt_grep() matching
359 # the pattern `$1` within the file `$2`.
363 local -r test_name
=$3
365 bt_grep
--silent "$pattern" "$file"
369 if ! ok
$ret "$test_name"; then
371 echo "Pattern \`$pattern\` doesn't match the contents of \`$file\`:"
382 # Forwards the arguments to `coverage run`.
383 _bt_tests_check_coverage
() {
387 # Executes a command within an environment which can import the testing
388 # Python modules (in `tests/utils/python`).
389 bt_run_in_py_utils_env
() {
390 local our_pythonpath
=$BT_TESTS_SRCDIR/utils
/python
392 if [[ $_bt_tests_py3_version =~
3.
[45] ]]; then
393 # Add a local directory containing a `typing.py` to `PYTHONPATH`
394 # for Python 3.4 and Python 3.5 which either don't offer the
395 # `typing` module at all, or offer a partial one.
396 our_pythonpath
=$our_pythonpath:$BT_TESTS_SRCDIR/utils
/python
/typing
399 PYTHONPATH
=$our_pythonpath${PYTHONPATH:+:}${PYTHONPATH:-} "$@"
402 # Executes a command within an environment which can import the testing
403 # Python modules (in `tests/utils/python`) and the `bt2` Python package.
405 local -x BABELTRACE_PLUGIN_PATH
=$BT_TESTS_BABELTRACE_PLUGIN_PATH
406 local -x LIBBABELTRACE2_PLUGIN_PROVIDER_DIR
=$BT_TESTS_PROVIDER_DIR
407 local -x BT_TESTS_DATADIR
=$BT_TESTS_DATADIR
408 local -x BT_CTF_TRACES_PATH
=$BT_CTF_TRACES_PATH
409 local -x BT_PLUGINS_PATH
=$_bt_tests_plugins_path
410 local -x PYTHONPATH
=$BT_TESTS_PYTHONPATH${PYTHONPATH:+:}${PYTHONPATH:-}
411 local -r main_lib_path
=$BT_TESTS_BUILDDIR/..
/src
/lib
/.libs
413 # Set the library search path so that the Python 3 interpreter can
414 # load `libbabeltrace2`.
415 if [[ $BT_TESTS_OS_TYPE == mingw ||
$BT_TESTS_OS_TYPE == cygwin
]]; then
416 local -x PATH
=$main_lib_path${PATH:+:}${PATH:-}
417 elif [[ $BT_TESTS_OS_TYPE == darwin
]]; then
418 local -x DYLD_LIBRARY_PATH
=$main_lib_path${DYLD_LIBRARY_PATH:+:}${DYLD_LIBRARY_PATH:-}
420 local -x LD_LIBRARY_PATH
=$main_lib_path${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH:-}
423 # On Windows, an embedded Python 3 interpreter needs a way to locate
424 # the path to its internal modules: set the `PYTHONHOME` variable to
425 # the prefix from `python3-config`.
426 if [[ $BT_TESTS_OS_TYPE == mingw
]]; then
429 PYTHONHOME
=$
($BT_TESTS_PYTHON_CONFIG_BIN --prefix)
432 # If AddressSanitizer is used, we must preload `libasan.so` so that
433 # libasan doesn't complain about not being the first loaded library.
435 # Python and sed (executed as part of the Libtool wrapper) produce
436 # some leaks, so we must unfortunately disable leak detection.
438 # Append it to existing `ASAN_OPTIONS` variable, such that we
439 # override the user's value if it contains `detect_leaks=1`.
440 if [[ ${BT_TESTS_ENABLE_ASAN:-} == 1 ]]; then
441 if $BT_TESTS_CC_BIN --version |
head -n 1 | bt_grep
-q '^gcc'; then
442 local -r lib_asan
=$
($BT_TESTS_CC_BIN -print-file-name=libasan.so
)
443 local -r lib_stdcxx
=$
($BT_TESTS_CC_BIN -print-file-name=libstdc
++.so
)
444 local -x LD_PRELOAD
=$lib_asan:$lib_stdcxx${LD_PRELOAD:+:}${LD_PRELOAD:-}
447 local -x ASAN_OPTIONS
=${ASAN_OPTIONS:-}${ASAN_OPTIONS:+,}detect_leaks
=0
450 bt_run_in_py_utils_env
"$@"
453 # Runs the Python tests matching the pattern `$2` (optional, `*` if
454 # missing) in the directory `$1` using `testrunner.py`.
456 # This function uses bt_run_in_py_env(), therefore such tests can import
457 # the testing Python modules (in `tests/utils/python`) and the `bt2`
461 local -r test_pattern
=${2:-*}
464 if [[ ${BT_TESTS_COVERAGE:-} == 1 ]]; then
465 python_exec
=_bt_tests_check_coverage
467 python_exec
=$BT_TESTS_PYTHON_BIN
471 "$python_exec" "$BT_TESTS_SRCDIR/utils/python/testrunner.py" \
472 --pattern "$test_pattern" "$test_dir"
476 if [[ ${BT_TESTS_COVERAGE_REPORT:-} == 1 ]]; then
480 if [[ ${BT_TESTS_COVERAGE_HTML:-} == 1 ]]; then
487 # Generates a CTF trace into the directory `$2` from the moultipart
488 # document `$1` using `mctf.py`.
489 bt_gen_mctf_trace
() {
490 local -r input_file
=$1
493 "$BT_TESTS_PYTHON_BIN" "$BT_TESTS_SRCDIR/utils/python/mctf.py"
494 --base-dir "$base_dir"
498 echo "Running: \`${cmd[*]}\`" >&2
499 bt_run_in_py_utils_env
"${cmd[@]}"
502 # Call `diag` with the contents of file `$1`.