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)
46 case $BT_TESTS_OS_TYPE in
48 BT_TESTS_OS_TYPE
=mingw
51 BT_TESTS_OS_TYPE
=darwin
54 BT_TESTS_OS_TYPE
=linux
57 BT_TESTS_OS_TYPE
=cygwin
60 BT_TESTS_OS_TYPE
=unsupported
65 export BT_TESTS_OS_TYPE
67 # Sets and exports, if not set:
69 # • `BT_TESTS_SRCDIR` to the base source directory of tests.
70 # • `BT_TESTS_BUILDDIR` to the base build directory of tests.
71 _set_vars_srcdir_builddir
() {
72 # If `readlink -f` is available, then get a resolved absolute path
73 # to the tests source directory. Otherwise, make do with a relative
75 local -r scriptdir
=$
(dirname "${BASH_SOURCE[0]}")
78 if readlink
-f .
&> /dev
/null
; then
79 testsdir
=$
(readlink
-f "$scriptdir/..")
81 testsdir
=$scriptdir/..
84 # Base source directory of tests
85 if [[ -z ${BT_TESTS_SRCDIR:-} ]]; then
86 BT_TESTS_SRCDIR
=$testsdir
89 export BT_TESTS_SRCDIR
91 # Base build directory of tests
92 if [[ -z ${BT_TESTS_BUILDDIR:-} ]]; then
93 BT_TESTS_BUILDDIR
=$testsdir
96 export BT_TESTS_BUILDDIR
99 _set_vars_srcdir_builddir
100 unset -f _set_vars_srcdir_builddir
102 # Sources the generated environment file (`env.sh`) if it exists.
104 local -r env_sh_path
=$BT_TESTS_BUILDDIR/utils
/env.sh
106 if [[ -f $env_sh_path ]]; then
107 # shellcheck disable=SC1090,SC1091
113 unset -f _source_env_sh
115 # Path to the `babeltrace2` command, if not set
116 if [[ -z ${BT_TESTS_BT2_BIN:-} ]]; then
117 BT_TESTS_BT2_BIN
=$BT_TESTS_BUILDDIR/..
/src
/cli
/babeltrace2
119 if [[ $BT_TESTS_OS_TYPE == mingw
]]; then
120 BT_TESTS_BT2_BIN
+=.exe
124 export BT_TESTS_BT2_BIN
126 # This doesn't need to be exported, but it needs to remain set for
127 # run_python_bt2() to use it.
129 # TODO: Remove when `tests/bindings/python/bt2/test_plugin.py` is fixed.
130 _bt_tests_plugins_path
=$BT_TESTS_BUILDDIR/..
/src
/plugins
132 # Colon-separated list of project plugin paths, if not set
133 if [[ -z ${BT_TESTS_BABELTRACE_PLUGIN_PATH:-} ]]; then
134 BT_TESTS_BABELTRACE_PLUGIN_PATH
=$_bt_tests_plugins_path/ctf
:$_bt_tests_plugins_path/utils
:$_bt_tests_plugins_path/text
:$_bt_tests_plugins_path/lttng-utils
137 export BT_TESTS_BABELTRACE_PLUGIN_PATH
139 # Directory containing the Python plugin provider library, if not set
140 if [[ -z ${BT_TESTS_PROVIDER_DIR:-} ]]; then
141 BT_TESTS_PROVIDER_DIR
=$BT_TESTS_BUILDDIR/..
/src
/python-plugin-provider
/.libs
144 export BT_TESTS_PROVIDER_DIR
146 # Directory containing the built `bt2` Python package, if not set
147 if [[ -z ${BT_TESTS_PYTHONPATH:-} ]]; then
148 BT_TESTS_PYTHONPATH
=$BT_TESTS_BUILDDIR/..
/src
/bindings
/python
/bt
2/build
/build_lib
151 export BT_TESTS_PYTHONPATH
153 # Name of the `awk` command to use when testing, if not set
154 if [[ -z ${BT_TESTS_AWK_BIN:-} ]]; then
155 BT_TESTS_AWK_BIN
="awk"
158 export BT_TESTS_AWK_BIN
160 # Name of the `grep` command to use when testing, if not set
161 if [[ -z ${BT_TESTS_GREP_BIN:-} ]]; then
162 BT_TESTS_GREP_BIN
="grep"
165 export BT_TESTS_GREP_BIN
167 # Name of the `python3` command to use when testing, if not set
168 if [[ -z ${BT_TESTS_PYTHON_BIN:-} ]]; then
169 BT_TESTS_PYTHON_BIN
=python3
172 export BT_TESTS_PYTHON_BIN
174 # Major and minor version of the `python3` command to use when testing.
176 # This doesn't need to be exported, but it needs to remain set for
177 # run_python() to use it.
178 _bt_tests_py3_version
=$
("$BT_TESTS_PYTHON_BIN" -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))')
180 # Name of the `python3-config` command to use when testing, if not set
181 if [[ -z ${BT_TESTS_PYTHON_CONFIG_BIN:-} ]]; then
182 BT_TESTS_PYTHON_CONFIG_BIN
=python3-config
185 export BT_TESTS_PYTHON_CONFIG_BIN
187 # Name of the `sed` command to use when testing, if not set
188 if [[ -z ${BT_TESTS_SED_BIN:-} ]]; then
189 BT_TESTS_SED_BIN
="sed"
192 export BT_TESTS_SED_BIN
194 # Name of the `cc` command to use when testing, if not set
195 if [[ -z ${BT_TESTS_CC_BIN:-} ]]; then
199 export BT_TESTS_CC_BIN
201 # Whether or not to enable AddressSanitizer, `0` (disabled) if not set.
203 # This doesn't need to be exported from the point of view of this file,
204 # but the sourced `env.sh` above does export it.
205 if [[ -z ${BT_TESTS_ENABLE_ASAN:-} ]]; then
206 BT_TESTS_ENABLE_ASAN
=0
209 # Directory containing test data
210 BT_TESTS_DATADIR
=$BT_TESTS_SRCDIR/data
212 # Directory containing test CTF traces
213 BT_CTF_TRACES_PATH
=$BT_TESTS_DATADIR/ctf-traces
215 # Source the shell TAP utilities if `SH_TAP` is `1`
216 if [[ ${SH_TAP:-} == 1 ]]; then
217 # shellcheck source=./tap/tap.sh
218 .
"$BT_TESTS_SRCDIR/utils/tap/tap.sh"
221 # Removes the CR characters from the file having the path `$1`.
223 # This is sometimes needed on Windows with text files.
225 # We can't use the `--string-trailing-cr` option of `diff` because
226 # Solaris doesn't have it.
228 "$BT_TESTS_SED_BIN" -i'' -e 's/\r//g' "$1"
231 # Prints `$1` without CR characters.
232 bt_remove_cr_inline
() {
233 "$BT_TESTS_SED_BIN" 's/\r//g' "$1"
236 # Runs the `$BT_TESTS_BT2_BIN` command within an environment which can
237 # import the `bt2` Python package, redirecting the standard output to
238 # the `$1` file and the standard error to the `$2` file.
240 # The remaining arguments are forwarded to the `$BT_TESTS_BT2_BIN`
243 # Returns the exit status of the executed `$BT_TESTS_BT2_BIN`.
245 local -r stdout_file
=$1
246 local -r stderr_file
=$2
252 echo "Running: \`$BT_TESTS_BT2_BIN ${args[*]}\`" >&2
253 run_python_bt2
"$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$stdout_file" 2>"$stderr_file"
256 # Checks the differences between:
258 # • The (expected) contents of the file having the path `$1`.
260 # • The contents of another file having the path `$2`.
262 # Both files are passed through bt_remove_cr_inline() to remove CR
265 # Returns 0 if there's no difference, or not zero otherwise.
267 local -r expected_file
=$1
268 local -r actual_file
=$2
270 diff -u <(bt_remove_cr_inline
"$expected_file") <(bt_remove_cr_inline
"$actual_file") 1>&2
273 # Checks the difference between:
275 # • What the `$BT_TESTS_BT2_BIN` command prints to its standard output
276 # when given the third and following arguments of this function.
278 # • The file having the path `$1`.
280 # as well as the difference between:
282 # • What the `$BT_TESTS_BT2_BIN` command prints to its standard error
283 # when given the third and following arguments of this function.
285 # • The file having the path `$2`.
287 # Returns 0 if there's no difference, or 1 otherwise, also printing said
288 # difference to the standard error.
290 local -r expected_stdout_file
=$1
291 local -r expected_stderr_file
=$2
296 local -r temp_stdout_output_file
=$
(mktemp
-t actual-stdout.XXXXXX
)
297 local -r temp_stderr_output_file
=$
(mktemp
-t actual-stderr.XXXXXX
)
299 bt_cli
"$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}"
300 bt_diff
"$expected_stdout_file" "$temp_stdout_output_file" "${args[@]}"
302 local -r ret_stdout
=$?
304 bt_diff
"$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}"
306 local -r ret_stderr
=$?
308 rm -f "$temp_stdout_output_file" "$temp_stderr_output_file"
309 return $
((ret_stdout || ret_stderr
))
312 # Checks the difference between:
314 # • The content of the file having the path `$1`.
316 # • What the `$BT_TESTS_BT2_BIN` command prints to the standard output
317 # when executed with:
319 # 1. The CTF trace directory `$2`.
320 # 2. The arguments `-c` and `sink.text.details`.
321 # 3. The third and following arguments of this function.
323 # Returns 0 if there's no difference, or 1 otherwise, also printing said
324 # difference to the standard error.
325 bt_diff_details_ctf_single
() {
326 local -r expected_stdout_file
=$1
327 local -r trace_dir
=$2
331 local -r extra_details_args
=("$@")
333 # Compare using the CLI with `sink.text.details`
334 bt_diff_cli
"$expected_stdout_file" /dev
/null
"$trace_dir" \
335 -c sink.text.details
"${extra_details_args[@]+${extra_details_args[@]}}"
338 # Like bt_diff_details_ctf_single(), except that `$1` is the path to a
339 # program which generates the CTF trace to compare to.
341 # The program `$1` receives the path to a temporary, empty directory
342 # where to write the CTF trace as its first argument.
343 bt_diff_details_ctf_gen_single
() {
344 local -r ctf_gen_prog_path
=$1
345 local -r expected_stdout_file
=$2
349 local -r extra_details_args
=("$@")
350 local -r temp_trace_dir
=$
(mktemp
-d)
352 # Run the CTF trace generator program to get a CTF trace
353 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev
/null
; then
354 echo "ERROR: \`$ctf_gen_prog_path $temp_trace_dir\` failed" >&2
355 rm -rf "$temp_trace_dir"
359 # Compare using the CLI with `sink.text.details`
360 bt_diff_details_ctf_single
"$expected_stdout_file" "$temp_trace_dir" \
361 "${extra_details_args[@]+${extra_details_args[@]}}"
365 rm -rf "$temp_trace_dir"
369 # Like `grep`, but using `$BT_TESTS_GREP_BIN`.
371 "$BT_TESTS_GREP_BIN" "$@"
374 # Only if `tap.sh` is sourced because bt_grep_ok() uses ok()
375 if [[ ${SH_TAP:-} == 1 ]]; then
376 # ok() with the test name `$3` on the result of bt_grep() matching
377 # the pattern `$1` within the file `$2`.
381 local -r test_name
=$3
383 bt_grep
--silent "$pattern" "$file"
387 if ! ok
$ret "$test_name"; then
389 echo "Pattern \`$pattern\` doesn't match the contents of \`$file\`:"
400 # Forwards the arguments to `coverage run`.
401 _bt_tests_check_coverage
() {
405 # Executes a command within an environment which can import the testing
406 # Python modules (in `tests/utils/python`).
408 local our_pythonpath
=$BT_TESTS_SRCDIR/utils
/python
410 if [[ $_bt_tests_py3_version =~
3.
[45] ]]; then
411 # Add a local directory containing a `typing.py` to `PYTHONPATH`
412 # for Python 3.4 and Python 3.5 which either don't offer the
413 # `typing` module at all, or offer a partial one.
414 our_pythonpath
=$our_pythonpath:$BT_TESTS_SRCDIR/utils
/python
/typing
417 PYTHONPATH
=$our_pythonpath${PYTHONPATH:+:}${PYTHONPATH:-} "$@"
420 # Executes a command within an environment which can import the testing
421 # Python modules (in `tests/utils/python`) and the `bt2` Python package.
423 local -x BABELTRACE_PLUGIN_PATH
=$BT_TESTS_BABELTRACE_PLUGIN_PATH
424 local -x LIBBABELTRACE2_PLUGIN_PROVIDER_DIR
=$BT_TESTS_PROVIDER_DIR
425 local -x BT_TESTS_DATADIR
=$BT_TESTS_DATADIR
426 local -x BT_CTF_TRACES_PATH
=$BT_CTF_TRACES_PATH
427 local -x BT_PLUGINS_PATH
=$_bt_tests_plugins_path
428 local -x PYTHONPATH
=$BT_TESTS_PYTHONPATH${PYTHONPATH:+:}${PYTHONPATH:-}
429 local -r main_lib_path
=$BT_TESTS_BUILDDIR/..
/src
/lib
/.libs
431 # Set the library search path so that the Python 3 interpreter can
432 # load `libbabeltrace2`.
433 if [[ $BT_TESTS_OS_TYPE == mingw ||
$BT_TESTS_OS_TYPE == cygwin
]]; then
434 local -x PATH
=$main_lib_path${PATH:+:}${PATH:-}
435 elif [[ $BT_TESTS_OS_TYPE == darwin
]]; then
436 local -x DYLD_LIBRARY_PATH
=$main_lib_path${DYLD_LIBRARY_PATH:+:}${DYLD_LIBRARY_PATH:-}
438 local -x LD_LIBRARY_PATH
=$main_lib_path${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH:-}
441 # On Windows, an embedded Python 3 interpreter needs a way to locate
442 # the path to its internal modules: set the `PYTHONHOME` variable to
443 # the prefix from `python3-config`.
444 if [[ $BT_TESTS_OS_TYPE == mingw
]]; then
447 PYTHONHOME
=$
("$BT_TESTS_PYTHON_CONFIG_BIN" --prefix)
450 # If AddressSanitizer is used, we must preload `libasan.so` so that
451 # libasan doesn't complain about not being the first loaded library.
453 # Python and sed (executed as part of the Libtool wrapper) produce
454 # some leaks, so we must unfortunately disable leak detection.
456 # Append it to existing `ASAN_OPTIONS` variable, such that we
457 # override the user's value if it contains `detect_leaks=1`.
458 if [[ ${BT_TESTS_ENABLE_ASAN:-} == 1 ]]; then
459 if "$BT_TESTS_CC_BIN" --version |
head -n 1 | bt_grep
-q '^gcc'; then
460 local -r lib_asan
=$
("$BT_TESTS_CC_BIN" -print-file-name=libasan.so
)
461 local -x LD_PRELOAD
=$lib_asan${LD_PRELOAD:+:}${LD_PRELOAD:-}
464 local -x ASAN_OPTIONS
=${ASAN_OPTIONS:-}${ASAN_OPTIONS:+,}detect_leaks
=0
470 # Runs the Python tests matching the pattern `$2` (optional, `*` if
471 # missing) in the directory `$1` using `testrunner.py`.
473 # This function uses run_python_bt2(), therefore such tests can import
474 # the testing Python modules (in `tests/utils/python`) and the `bt2`
476 run_python_bt2_test
() {
478 local -r test_pattern
=${2:-*}
481 if [[ ${BT_TESTS_COVERAGE:-} == 1 ]]; then
482 python_exec
=_bt_tests_check_coverage
484 python_exec
=$BT_TESTS_PYTHON_BIN
488 "$python_exec" "$BT_TESTS_SRCDIR/utils/python/testrunner.py" \
489 --pattern "$test_pattern" "$test_dir"
493 if [[ ${BT_TESTS_COVERAGE_REPORT:-} == 1 ]]; then
497 if [[ ${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 -r input_file
=$1
510 "$BT_TESTS_PYTHON_BIN" "$BT_TESTS_SRCDIR/utils/python/mctf.py"
511 --base-dir "$base_dir"
515 echo "Running: \`${cmd[*]}\`" >&2
516 run_python
"${cmd[@]}"