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