Commit | Line | Data |
---|---|---|
644e0364 | 1 | #!/bin/bash |
644e0364 | 2 | # |
0235b0db | 3 | # SPDX-License-Identifier: GPL-2.0-only |
644e0364 | 4 | # |
0235b0db | 5 | # Copyright (c) 2019 Michael Jeanson <mjeanson@efficios.com> |
07ff3b19 | 6 | # Copyright (C) 2019-2023 Philippe Proulx <pproulx@efficios.com> |
644e0364 | 7 | |
07ff3b19 PP |
8 | # Source this file at the beginning of a shell script test to access |
9 | # useful testing variables and functions: | |
10 | # | |
11 | # SH_TAP=1 | |
12 | # | |
13 | # if [[ -n ${BT_TESTS_SRCDIR:-} ]]; then | |
14 | # UTILSSH=$BT_TESTS_SRCDIR/utils/utils.sh | |
15 | # else | |
16 | # UTILSSH=$(dirname "$0")/../utils/utils.sh | |
17 | # fi | |
18 | # | |
19 | # # shellcheck source=../utils/utils.sh | |
20 | # source "$UTILSSH" | |
21 | # | |
22 | # Make sure the relative path to `utils.sh` (this file) above is | |
23 | # correct (twice). | |
644e0364 | 24 | |
07ff3b19 | 25 | # An unbound variable is an error |
644e0364 MJ |
26 | set -u |
27 | ||
82057d0c PP |
28 | # Sets the variable named `$1` to `$2` if it's not set, and exports it. |
29 | _bt_tests_set_var_def() { | |
30 | local -r varname=$1 | |
31 | local -r val=$2 | |
32 | ||
33 | if [[ -z ${!varname:-} ]]; then | |
34 | eval "$varname='$val'" | |
35 | fi | |
36 | ||
37 | export "${varname?}" | |
38 | } | |
39 | ||
07ff3b19 PP |
40 | # Name of the OS on which we're running, if not set. |
41 | # | |
42 | # One of: | |
43 | # | |
44 | # `mingw`: MinGW (Windows) | |
45 | # `darwin`: macOS | |
46 | # `linux`: Linux | |
47 | # `cygwin`: Cygwin (Windows) | |
48 | # `unsupported`: Anything else | |
49 | # | |
50 | # See <https://en.wikipedia.org/wiki/Uname#Examples> for possible values | |
51 | # of `uname -s`. | |
52 | # | |
53 | # Do some translation to ease our life down the road for comparison. | |
54 | # Export it so that executed commands can use it. | |
c0d75ae3 PP |
55 | if [[ -z ${BT_TESTS_OS_TYPE:-} ]]; then |
56 | BT_TESTS_OS_TYPE=$(uname -s) | |
7121d33d | 57 | |
c0d75ae3 | 58 | case $BT_TESTS_OS_TYPE in |
5058d31b | 59 | MINGW*) |
c0d75ae3 | 60 | BT_TESTS_OS_TYPE=mingw |
5058d31b JR |
61 | ;; |
62 | Darwin) | |
c0d75ae3 | 63 | BT_TESTS_OS_TYPE=darwin |
5058d31b JR |
64 | ;; |
65 | Linux) | |
c0d75ae3 | 66 | BT_TESTS_OS_TYPE=linux |
5058d31b | 67 | ;; |
df9db467 | 68 | CYGWIN*) |
c0d75ae3 | 69 | BT_TESTS_OS_TYPE=cygwin |
df9db467 | 70 | ;; |
5058d31b | 71 | *) |
c0d75ae3 | 72 | BT_TESTS_OS_TYPE=unsupported |
5058d31b JR |
73 | ;; |
74 | esac | |
75 | fi | |
7121d33d | 76 | |
a0baab4a | 77 | export BT_TESTS_OS_TYPE |
5058d31b | 78 | |
7030f9fe PP |
79 | # Sets and exports, if not set: |
80 | # | |
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 | |
86 | # path. | |
c0d75ae3 | 87 | local -r scriptdir=$(dirname "${BASH_SOURCE[0]}") |
7030f9fe | 88 | local testsdir |
644e0364 | 89 | |
c0d75ae3 | 90 | if readlink -f . &> /dev/null; then |
7030f9fe PP |
91 | testsdir=$(readlink -f "$scriptdir/..") |
92 | else | |
c0d75ae3 | 93 | testsdir=$scriptdir/.. |
7030f9fe PP |
94 | fi |
95 | ||
96 | # Base source directory of tests | |
82057d0c | 97 | _bt_tests_set_var_def BT_TESTS_SRCDIR "$testsdir" |
7030f9fe PP |
98 | |
99 | # Base build directory of tests | |
82057d0c | 100 | _bt_tests_set_var_def BT_TESTS_BUILDDIR "$testsdir" |
7030f9fe | 101 | } |
644e0364 | 102 | |
7030f9fe PP |
103 | _set_vars_srcdir_builddir |
104 | unset -f _set_vars_srcdir_builddir | |
e46cbefe | 105 | |
4a162893 PP |
106 | # Sources the generated environment file (`env.sh`) if it exists. |
107 | _source_env_sh() { | |
c0d75ae3 | 108 | local -r env_sh_path=$BT_TESTS_BUILDDIR/utils/env.sh |
4a162893 | 109 | |
ad42b27e | 110 | if [[ -f $env_sh_path ]]; then |
4a162893 | 111 | # shellcheck disable=SC1090,SC1091 |
ad42b27e | 112 | . "$env_sh_path" |
4a162893 PP |
113 | fi |
114 | } | |
115 | ||
116 | _source_env_sh | |
117 | unset -f _source_env_sh | |
644e0364 | 118 | |
07ff3b19 | 119 | # Path to the `babeltrace2` command, if not set |
c0d75ae3 PP |
120 | if [[ -z ${BT_TESTS_BT2_BIN:-} ]]; then |
121 | BT_TESTS_BT2_BIN=$BT_TESTS_BUILDDIR/../src/cli/babeltrace2 | |
7121d33d | 122 | |
c0d75ae3 PP |
123 | if [[ $BT_TESTS_OS_TYPE == mingw ]]; then |
124 | BT_TESTS_BT2_BIN+=.exe | |
27e2c58d | 125 | fi |
644e0364 | 126 | fi |
7121d33d | 127 | |
644e0364 MJ |
128 | export BT_TESTS_BT2_BIN |
129 | ||
07ff3b19 | 130 | # This doesn't need to be exported, but it needs to remain set for |
ae7f2cf2 | 131 | # bt_run_in_py_env() to use it. |
07ff3b19 PP |
132 | # |
133 | # TODO: Remove when `tests/bindings/python/bt2/test_plugin.py` is fixed. | |
ad42b27e | 134 | _bt_tests_plugins_path=$BT_TESTS_BUILDDIR/../src/plugins |
644e0364 | 135 | |
07ff3b19 | 136 | # Colon-separated list of project plugin paths, if not set |
82057d0c PP |
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" | |
644e0364 | 139 | |
07ff3b19 | 140 | # Directory containing the Python plugin provider library, if not set |
82057d0c | 141 | _bt_tests_set_var_def BT_TESTS_PROVIDER_DIR "$BT_TESTS_BUILDDIR/../src/python-plugin-provider/.libs" |
b14c7bf1 | 142 | |
07ff3b19 | 143 | # Directory containing the built `bt2` Python package, if not set |
82057d0c | 144 | _bt_tests_set_var_def BT_TESTS_PYTHONPATH "$BT_TESTS_BUILDDIR/../src/bindings/python/bt2/build/build_lib" |
644e0364 | 145 | |
07ff3b19 | 146 | # Name of the `awk` command to use when testing, if not set |
82057d0c | 147 | _bt_tests_set_var_def BT_TESTS_AWK_BIN awk |
644e0364 | 148 | |
07ff3b19 | 149 | # Name of the `grep` command to use when testing, if not set |
82057d0c | 150 | _bt_tests_set_var_def BT_TESTS_GREP_BIN grep |
644e0364 | 151 | |
07ff3b19 | 152 | # Name of the `python3` command to use when testing, if not set |
82057d0c | 153 | _bt_tests_set_var_def BT_TESTS_PYTHON_BIN python3 |
644e0364 | 154 | |
07ff3b19 PP |
155 | # Major and minor version of the `python3` command to use when testing. |
156 | # | |
157 | # This doesn't need to be exported, but it needs to remain set for | |
fca73c71 | 158 | # bt_run_in_py_utils_env() to use it. |
68533c73 | 159 | _bt_tests_py3_version=$($BT_TESTS_PYTHON_BIN -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))') |
7747a39f | 160 | |
07ff3b19 | 161 | # Name of the `python3-config` command to use when testing, if not set |
82057d0c | 162 | _bt_tests_set_var_def BT_TESTS_PYTHON_CONFIG_BIN python3-config |
e23e08c4 | 163 | |
07ff3b19 | 164 | # Name of the `sed` command to use when testing, if not set |
82057d0c | 165 | _bt_tests_set_var_def BT_TESTS_SED_BIN sed |
644e0364 | 166 | |
07ff3b19 | 167 | # Name of the `cc` command to use when testing, if not set |
82057d0c | 168 | _bt_tests_set_var_def BT_TESTS_CC_BIN cc |
7121d33d | 169 | |
82057d0c PP |
170 | # Done with _bt_tests_set_var_def() |
171 | unset -f _bt_tests_set_var_def | |
0b0893d4 | 172 | |
07ff3b19 | 173 | # Whether or not to enable AddressSanitizer, `0` (disabled) if not set. |
ea81414f PP |
174 | # |
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. | |
c0d75ae3 PP |
177 | if [[ -z ${BT_TESTS_ENABLE_ASAN:-} ]]; then |
178 | BT_TESTS_ENABLE_ASAN=0 | |
0b0893d4 | 179 | fi |
0b0893d4 | 180 | |
07ff3b19 | 181 | # Directory containing test data |
ad42b27e | 182 | BT_TESTS_DATADIR=$BT_TESTS_SRCDIR/data |
07ff3b19 PP |
183 | |
184 | # Directory containing test CTF traces | |
ad42b27e | 185 | BT_CTF_TRACES_PATH=$BT_TESTS_DATADIR/ctf-traces |
644e0364 | 186 | |
07ff3b19 | 187 | # Source the shell TAP utilities if `SH_TAP` is `1` |
c0d75ae3 | 188 | if [[ ${SH_TAP:-} == 1 ]]; then |
e46cbefe | 189 | # shellcheck source=./tap/tap.sh |
ad42b27e | 190 | . "$BT_TESTS_SRCDIR/utils/tap/tap.sh" |
e46cbefe MJ |
191 | fi |
192 | ||
07ff3b19 PP |
193 | # Removes the CR characters from the file having the path `$1`. |
194 | # | |
195 | # This is sometimes needed on Windows with text files. | |
196 | # | |
197 | # We can't use the `--string-trailing-cr` option of `diff` because | |
198 | # Solaris doesn't have it. | |
90a8a0f2 | 199 | bt_remove_cr() { |
717a01a7 | 200 | "$BT_TESTS_SED_BIN" -i'' -e 's/\r//g' "$1" |
90a8a0f2 SM |
201 | } |
202 | ||
07ff3b19 | 203 | # Prints `$1` without CR characters. |
10ba5f69 | 204 | bt_remove_cr_inline() { |
07ff3b19 | 205 | "$BT_TESTS_SED_BIN" 's/\r//g' "$1" |
10ba5f69 OD |
206 | } |
207 | ||
07ff3b19 PP |
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. | |
22703f66 | 211 | # |
07ff3b19 PP |
212 | # The remaining arguments are forwarded to the `$BT_TESTS_BT2_BIN` |
213 | # command. | |
22703f66 | 214 | # |
07ff3b19 | 215 | # Returns the exit status of the executed `$BT_TESTS_BT2_BIN`. |
22703f66 | 216 | bt_cli() { |
c0d75ae3 PP |
217 | local -r stdout_file=$1 |
218 | local -r stderr_file=$2 | |
7121d33d | 219 | |
22703f66 | 220 | shift 2 |
7121d33d | 221 | |
9a8ce5a3 | 222 | local -a bt_cli_args=("$@") |
22703f66 | 223 | |
9a8ce5a3 MJ |
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" | |
22703f66 | 226 | } |
644e0364 | 227 | |
07ff3b19 | 228 | # Checks the differences between: |
ff89ed28 | 229 | # |
07ff3b19 | 230 | # • The (expected) contents of the file having the path `$1`. |
ff89ed28 | 231 | # |
07ff3b19 | 232 | # • The contents of another file having the path `$2`. |
ff89ed28 | 233 | # |
07ff3b19 PP |
234 | # Both files are passed through bt_remove_cr_inline() to remove CR |
235 | # characters. | |
236 | # | |
237 | # Returns 0 if there's no difference, or not zero otherwise. | |
ff89ed28 | 238 | bt_diff() { |
c0d75ae3 PP |
239 | local -r expected_file=$1 |
240 | local -r actual_file=$2 | |
ff89ed28 | 241 | |
2e0d6c25 PP |
242 | if [[ ! -e $expected_file ]]; then |
243 | echo "ERROR: expected file \`$expected_file\` doesn't exist" >&2 | |
244 | return 1 | |
245 | fi | |
246 | ||
247 | if [[ ! -e $actual_file ]]; then | |
248 | echo "ERROR: actual file \`$actual_file\` doesn't exist" >&2 | |
249 | return 1 | |
250 | fi | |
251 | ||
10ba5f69 | 252 | diff -u <(bt_remove_cr_inline "$expected_file") <(bt_remove_cr_inline "$actual_file") 1>&2 |
ff89ed28 MJ |
253 | } |
254 | ||
58db335e FD |
255 | # Checks the difference between: |
256 | # | |
07ff3b19 PP |
257 | # • What the `$BT_TESTS_BT2_BIN` command prints to its standard output |
258 | # when given the third and following arguments of this function. | |
259 | # | |
260 | # • The file having the path `$1`. | |
261 | # | |
262 | # as well as the difference between: | |
58db335e | 263 | # |
07ff3b19 PP |
264 | # • What the `$BT_TESTS_BT2_BIN` command prints to its standard error |
265 | # when given the third and following arguments of this function. | |
58db335e | 266 | # |
07ff3b19 | 267 | # • The file having the path `$2`. |
644e0364 | 268 | # |
07ff3b19 PP |
269 | # Returns 0 if there's no difference, or 1 otherwise, also printing said |
270 | # difference to the standard error. | |
644e0364 | 271 | bt_diff_cli() { |
c0d75ae3 PP |
272 | local -r expected_stdout_file=$1 |
273 | local -r expected_stderr_file=$2 | |
7121d33d | 274 | |
58db335e | 275 | shift 2 |
53cc240b | 276 | |
7121d33d | 277 | local -r args=("$@") |
c0d75ae3 PP |
278 | local -r temp_stdout_output_file=$(mktemp -t actual-stdout.XXXXXX) |
279 | local -r temp_stderr_output_file=$(mktemp -t actual-stderr.XXXXXX) | |
644e0364 | 280 | |
22703f66 | 281 | bt_cli "$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}" |
a70b6702 | 282 | bt_diff "$expected_stdout_file" "$temp_stdout_output_file" "${args[@]}" |
7121d33d | 283 | |
af34d92b | 284 | local -r ret_stdout=$? |
7121d33d | 285 | |
a70b6702 | 286 | bt_diff "$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}" |
7121d33d | 287 | |
af34d92b | 288 | local -r ret_stderr=$? |
a70b6702 | 289 | |
ff89ed28 | 290 | rm -f "$temp_stdout_output_file" "$temp_stderr_output_file" |
a5102ddb | 291 | return $((ret_stdout || ret_stderr)) |
ff89ed28 MJ |
292 | } |
293 | ||
07ff3b19 PP |
294 | # Checks the difference between: |
295 | # | |
296 | # • The content of the file having the path `$1`. | |
297 | # | |
298 | # • What the `$BT_TESTS_BT2_BIN` command prints to the standard output | |
299 | # when executed with: | |
300 | # | |
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. | |
644e0364 | 304 | # |
07ff3b19 PP |
305 | # Returns 0 if there's no difference, or 1 otherwise, also printing said |
306 | # difference to the standard error. | |
644e0364 | 307 | bt_diff_details_ctf_single() { |
c0d75ae3 PP |
308 | local -r expected_stdout_file=$1 |
309 | local -r trace_dir=$2 | |
7121d33d | 310 | |
53cc240b | 311 | shift 2 |
7121d33d | 312 | |
30723e1c | 313 | local -r extra_details_args=("$@") |
644e0364 MJ |
314 | |
315 | # Compare using the CLI with `sink.text.details` | |
568f4bd1 | 316 | bt_diff_cli "$expected_stdout_file" /dev/null "$trace_dir" \ |
c0d75ae3 | 317 | -c sink.text.details "${extra_details_args[@]+${extra_details_args[@]}}" |
644e0364 MJ |
318 | } |
319 | ||
07ff3b19 PP |
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. | |
322 | # | |
323 | # The program `$1` receives the path to a temporary, empty directory | |
324 | # where to write the CTF trace as its first argument. | |
644e0364 | 325 | bt_diff_details_ctf_gen_single() { |
c0d75ae3 PP |
326 | local -r ctf_gen_prog_path=$1 |
327 | local -r expected_stdout_file=$2 | |
7121d33d | 328 | |
53cc240b | 329 | shift 2 |
644e0364 | 330 | |
9a8ce5a3 | 331 | local -r gen_extra_details_args=("$@") |
c0d75ae3 | 332 | local -r temp_trace_dir=$(mktemp -d) |
644e0364 MJ |
333 | |
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 | |
4a5ed599 | 336 | echo "ERROR: \`$ctf_gen_prog_path $temp_trace_dir\` failed" >&2 |
644e0364 MJ |
337 | rm -rf "$temp_trace_dir" |
338 | return 1 | |
339 | fi | |
340 | ||
341 | # Compare using the CLI with `sink.text.details` | |
8b729209 | 342 | bt_diff_details_ctf_single "$expected_stdout_file" "$temp_trace_dir" \ |
9a8ce5a3 | 343 | "${gen_extra_details_args[@]+${gen_extra_details_args[@]}}" |
7121d33d | 344 | |
69b5f89d | 345 | local -r ret=$? |
7121d33d | 346 | |
644e0364 MJ |
347 | rm -rf "$temp_trace_dir" |
348 | return $ret | |
349 | } | |
350 | ||
07ff3b19 | 351 | # Like `grep`, but using `$BT_TESTS_GREP_BIN`. |
407d3b10 SM |
352 | bt_grep() { |
353 | "$BT_TESTS_GREP_BIN" "$@" | |
354 | } | |
644e0364 | 355 | |
53be3c12 PP |
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`. | |
360 | bt_grep_ok() { | |
361 | local -r pattern=$1 | |
362 | local -r file=$2 | |
363 | local -r test_name=$3 | |
364 | ||
365 | bt_grep --silent "$pattern" "$file" | |
366 | ||
367 | local -r ret=$? | |
368 | ||
369 | if ! ok $ret "$test_name"; then | |
370 | { | |
371 | echo "Pattern \`$pattern\` doesn't match the contents of \`$file\`:" | |
372 | echo '--- 8< ---' | |
373 | cat "$file" | |
374 | echo '--- >8 ---' | |
375 | } >&2 | |
376 | fi | |
377 | ||
378 | return $ret | |
379 | } | |
380 | fi | |
db01f759 | 381 | |
07ff3b19 | 382 | # Forwards the arguments to `coverage run`. |
4958be54 | 383 | _bt_tests_check_coverage() { |
644e0364 MJ |
384 | coverage run "$@" |
385 | } | |
386 | ||
07ff3b19 PP |
387 | # Executes a command within an environment which can import the testing |
388 | # Python modules (in `tests/utils/python`). | |
fca73c71 | 389 | bt_run_in_py_utils_env() { |
ad42b27e | 390 | local our_pythonpath=$BT_TESTS_SRCDIR/utils/python |
7747a39f | 391 | |
315323f4 | 392 | if [[ $_bt_tests_py3_version =~ 3.[45] ]]; then |
07ff3b19 PP |
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. | |
ad42b27e | 396 | our_pythonpath=$our_pythonpath:$BT_TESTS_SRCDIR/utils/python/typing |
7747a39f PP |
397 | fi |
398 | ||
ad42b27e | 399 | PYTHONPATH=$our_pythonpath${PYTHONPATH:+:}${PYTHONPATH:-} "$@" |
1e14ec65 PP |
400 | } |
401 | ||
07ff3b19 PP |
402 | # Executes a command within an environment which can import the testing |
403 | # Python modules (in `tests/utils/python`) and the `bt2` Python package. | |
ae7f2cf2 | 404 | bt_run_in_py_env() { |
ad42b27e PP |
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:-} | |
ad42b27e | 411 | local -r main_lib_path=$BT_TESTS_BUILDDIR/../src/lib/.libs |
0408934a | 412 | |
07ff3b19 PP |
413 | # Set the library search path so that the Python 3 interpreter can |
414 | # load `libbabeltrace2`. | |
c0d75ae3 | 415 | if [[ $BT_TESTS_OS_TYPE == mingw || $BT_TESTS_OS_TYPE == cygwin ]]; then |
ad42b27e | 416 | local -x PATH=$main_lib_path${PATH:+:}${PATH:-} |
c0d75ae3 | 417 | elif [[ $BT_TESTS_OS_TYPE == darwin ]]; then |
ad42b27e | 418 | local -x DYLD_LIBRARY_PATH=$main_lib_path${DYLD_LIBRARY_PATH:+:}${DYLD_LIBRARY_PATH:-} |
644e0364 | 419 | else |
ad42b27e | 420 | local -x LD_LIBRARY_PATH=$main_lib_path${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH:-} |
e23e08c4 MJ |
421 | fi |
422 | ||
07ff3b19 PP |
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`. | |
c0d75ae3 | 426 | if [[ $BT_TESTS_OS_TYPE == mingw ]]; then |
1e14ec65 PP |
427 | local -x PYTHONHOME |
428 | ||
68533c73 | 429 | PYTHONHOME=$($BT_TESTS_PYTHON_CONFIG_BIN --prefix) |
644e0364 MJ |
430 | fi |
431 | ||
07ff3b19 | 432 | # If AddressSanitizer is used, we must preload `libasan.so` so that |
0b0893d4 SM |
433 | # libasan doesn't complain about not being the first loaded library. |
434 | # | |
07ff3b19 PP |
435 | # Python and sed (executed as part of the Libtool wrapper) produce |
436 | # some leaks, so we must unfortunately disable leak detection. | |
437 | # | |
438 | # Append it to existing `ASAN_OPTIONS` variable, such that we | |
439 | # override the user's value if it contains `detect_leaks=1`. | |
c0d75ae3 | 440 | if [[ ${BT_TESTS_ENABLE_ASAN:-} == 1 ]]; then |
68533c73 SM |
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) | |
1dcaf4b7 SM |
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:-} | |
2d12d310 | 445 | fi |
0b0893d4 | 446 | |
c0d75ae3 | 447 | local -x ASAN_OPTIONS=${ASAN_OPTIONS:-}${ASAN_OPTIONS:+,}detect_leaks=0 |
0b0893d4 SM |
448 | fi |
449 | ||
fca73c71 | 450 | bt_run_in_py_utils_env "$@" |
644e0364 MJ |
451 | } |
452 | ||
07ff3b19 PP |
453 | # Runs the Python tests matching the pattern `$2` (optional, `*` if |
454 | # missing) in the directory `$1` using `testrunner.py`. | |
644e0364 | 455 | # |
ae7f2cf2 | 456 | # This function uses bt_run_in_py_env(), therefore such tests can import |
07ff3b19 PP |
457 | # the testing Python modules (in `tests/utils/python`) and the `bt2` |
458 | # Python package. | |
c3ca8136 | 459 | bt_run_py_test() { |
c0d75ae3 PP |
460 | local -r test_dir=$1 |
461 | local -r test_pattern=${2:-*} | |
7c577b1d PP |
462 | local python_exec |
463 | ||
c0d75ae3 PP |
464 | if [[ ${BT_TESTS_COVERAGE:-} == 1 ]]; then |
465 | python_exec=_bt_tests_check_coverage | |
644e0364 | 466 | else |
ad42b27e | 467 | python_exec=$BT_TESTS_PYTHON_BIN |
644e0364 MJ |
468 | fi |
469 | ||
ae7f2cf2 | 470 | bt_run_in_py_env \ |
7121d33d PP |
471 | "$python_exec" "$BT_TESTS_SRCDIR/utils/python/testrunner.py" \ |
472 | --pattern "$test_pattern" "$test_dir" | |
a818a617 | 473 | |
04c029a2 | 474 | local -r ret=$? |
644e0364 | 475 | |
c0d75ae3 | 476 | if [[ ${BT_TESTS_COVERAGE_REPORT:-} == 1 ]]; then |
644e0364 MJ |
477 | coverage report -m |
478 | fi | |
479 | ||
c0d75ae3 | 480 | if [[ ${BT_TESTS_COVERAGE_HTML:-} == 1 ]]; then |
644e0364 MJ |
481 | coverage html |
482 | fi | |
483 | ||
484 | return $ret | |
485 | } | |
89ec984e | 486 | |
07ff3b19 PP |
487 | # Generates a CTF trace into the directory `$2` from the moultipart |
488 | # document `$1` using `mctf.py`. | |
2fb231c0 | 489 | bt_gen_mctf_trace() { |
c0d75ae3 PP |
490 | local -r input_file=$1 |
491 | local -r base_dir=$2 | |
ce8c884d PP |
492 | local -r cmd=( |
493 | "$BT_TESTS_PYTHON_BIN" "$BT_TESTS_SRCDIR/utils/python/mctf.py" | |
494 | --base-dir "$base_dir" | |
495 | "$input_file" | |
496 | ) | |
497 | ||
498 | echo "Running: \`${cmd[*]}\`" >&2 | |
fca73c71 | 499 | bt_run_in_py_utils_env "${cmd[@]}" |
89ec984e | 500 | } |
bfa3d907 SM |
501 | |
502 | # Call `diag` with the contents of file `$1`. | |
503 | ||
504 | diag_file() { | |
505 | diag "$(cat "$1")" | |
506 | } |