src.ctf.lttng-live: remove some goto error-handling
[babeltrace.git] / tests / utils / utils.sh
CommitLineData
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
26set -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
55if [[ -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
75fi
7121d33d 76
a0baab4a 77export 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
104unset -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
117unset -f _source_env_sh
644e0364 118
07ff3b19 119# Path to the `babeltrace2` command, if not set
c0d75ae3
PP
120if [[ -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 126fi
7121d33d 127
644e0364
MJ
128export 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()
171unset -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
177if [[ -z ${BT_TESTS_ENABLE_ASAN:-} ]]; then
178 BT_TESTS_ENABLE_ASAN=0
0b0893d4 179fi
0b0893d4 180
07ff3b19 181# Directory containing test data
ad42b27e 182BT_TESTS_DATADIR=$BT_TESTS_SRCDIR/data
07ff3b19
PP
183
184# Directory containing test CTF traces
ad42b27e 185BT_CTF_TRACES_PATH=$BT_TESTS_DATADIR/ctf-traces
644e0364 186
07ff3b19 187# Source the shell TAP utilities if `SH_TAP` is `1`
c0d75ae3 188if [[ ${SH_TAP:-} == 1 ]]; then
e46cbefe 189 # shellcheck source=./tap/tap.sh
ad42b27e 190 . "$BT_TESTS_SRCDIR/utils/tap/tap.sh"
e46cbefe
MJ
191fi
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 199bt_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 204bt_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 216bt_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 238bt_diff() {
c0d75ae3
PP
239 local -r expected_file=$1
240 local -r actual_file=$2
ff89ed28 241
10ba5f69 242 diff -u <(bt_remove_cr_inline "$expected_file") <(bt_remove_cr_inline "$actual_file") 1>&2
ff89ed28
MJ
243}
244
58db335e
FD
245# Checks the difference between:
246#
07ff3b19
PP
247# • What the `$BT_TESTS_BT2_BIN` command prints to its standard output
248# when given the third and following arguments of this function.
249#
250# • The file having the path `$1`.
251#
252# as well as the difference between:
58db335e 253#
07ff3b19
PP
254# • What the `$BT_TESTS_BT2_BIN` command prints to its standard error
255# when given the third and following arguments of this function.
58db335e 256#
07ff3b19 257# • The file having the path `$2`.
644e0364 258#
07ff3b19
PP
259# Returns 0 if there's no difference, or 1 otherwise, also printing said
260# difference to the standard error.
644e0364 261bt_diff_cli() {
c0d75ae3
PP
262 local -r expected_stdout_file=$1
263 local -r expected_stderr_file=$2
7121d33d 264
58db335e 265 shift 2
53cc240b 266
7121d33d 267 local -r args=("$@")
c0d75ae3
PP
268 local -r temp_stdout_output_file=$(mktemp -t actual-stdout.XXXXXX)
269 local -r temp_stderr_output_file=$(mktemp -t actual-stderr.XXXXXX)
644e0364 270
22703f66 271 bt_cli "$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}"
a70b6702 272 bt_diff "$expected_stdout_file" "$temp_stdout_output_file" "${args[@]}"
7121d33d 273
af34d92b 274 local -r ret_stdout=$?
7121d33d 275
a70b6702 276 bt_diff "$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}"
7121d33d 277
af34d92b 278 local -r ret_stderr=$?
a70b6702 279
ff89ed28 280 rm -f "$temp_stdout_output_file" "$temp_stderr_output_file"
a5102ddb 281 return $((ret_stdout || ret_stderr))
ff89ed28
MJ
282}
283
07ff3b19
PP
284# Checks the difference between:
285#
286# • The content of the file having the path `$1`.
287#
288# • What the `$BT_TESTS_BT2_BIN` command prints to the standard output
289# when executed with:
290#
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.
644e0364 294#
07ff3b19
PP
295# Returns 0 if there's no difference, or 1 otherwise, also printing said
296# difference to the standard error.
644e0364 297bt_diff_details_ctf_single() {
c0d75ae3
PP
298 local -r expected_stdout_file=$1
299 local -r trace_dir=$2
7121d33d 300
53cc240b 301 shift 2
7121d33d 302
30723e1c 303 local -r extra_details_args=("$@")
644e0364
MJ
304
305 # Compare using the CLI with `sink.text.details`
568f4bd1 306 bt_diff_cli "$expected_stdout_file" /dev/null "$trace_dir" \
c0d75ae3 307 -c sink.text.details "${extra_details_args[@]+${extra_details_args[@]}}"
644e0364
MJ
308}
309
07ff3b19
PP
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.
312#
313# The program `$1` receives the path to a temporary, empty directory
314# where to write the CTF trace as its first argument.
644e0364 315bt_diff_details_ctf_gen_single() {
c0d75ae3
PP
316 local -r ctf_gen_prog_path=$1
317 local -r expected_stdout_file=$2
7121d33d 318
53cc240b 319 shift 2
644e0364 320
9a8ce5a3 321 local -r gen_extra_details_args=("$@")
c0d75ae3 322 local -r temp_trace_dir=$(mktemp -d)
644e0364
MJ
323
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
4a5ed599 326 echo "ERROR: \`$ctf_gen_prog_path $temp_trace_dir\` failed" >&2
644e0364
MJ
327 rm -rf "$temp_trace_dir"
328 return 1
329 fi
330
331 # Compare using the CLI with `sink.text.details`
8b729209 332 bt_diff_details_ctf_single "$expected_stdout_file" "$temp_trace_dir" \
9a8ce5a3 333 "${gen_extra_details_args[@]+${gen_extra_details_args[@]}}"
7121d33d 334
69b5f89d 335 local -r ret=$?
7121d33d 336
644e0364
MJ
337 rm -rf "$temp_trace_dir"
338 return $ret
339}
340
07ff3b19 341# Like `grep`, but using `$BT_TESTS_GREP_BIN`.
407d3b10
SM
342bt_grep() {
343 "$BT_TESTS_GREP_BIN" "$@"
344}
644e0364 345
53be3c12
PP
346# Only if `tap.sh` is sourced because bt_grep_ok() uses ok()
347if [[ ${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`.
350 bt_grep_ok() {
351 local -r pattern=$1
352 local -r file=$2
353 local -r test_name=$3
354
355 bt_grep --silent "$pattern" "$file"
356
357 local -r ret=$?
358
359 if ! ok $ret "$test_name"; then
360 {
361 echo "Pattern \`$pattern\` doesn't match the contents of \`$file\`:"
362 echo '--- 8< ---'
363 cat "$file"
364 echo '--- >8 ---'
365 } >&2
366 fi
367
368 return $ret
369 }
370fi
db01f759 371
07ff3b19 372# Forwards the arguments to `coverage run`.
4958be54 373_bt_tests_check_coverage() {
644e0364
MJ
374 coverage run "$@"
375}
376
07ff3b19
PP
377# Executes a command within an environment which can import the testing
378# Python modules (in `tests/utils/python`).
fca73c71 379bt_run_in_py_utils_env() {
ad42b27e 380 local our_pythonpath=$BT_TESTS_SRCDIR/utils/python
7747a39f 381
315323f4 382 if [[ $_bt_tests_py3_version =~ 3.[45] ]]; then
07ff3b19
PP
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.
ad42b27e 386 our_pythonpath=$our_pythonpath:$BT_TESTS_SRCDIR/utils/python/typing
7747a39f
PP
387 fi
388
ad42b27e 389 PYTHONPATH=$our_pythonpath${PYTHONPATH:+:}${PYTHONPATH:-} "$@"
1e14ec65
PP
390}
391
07ff3b19
PP
392# Executes a command within an environment which can import the testing
393# Python modules (in `tests/utils/python`) and the `bt2` Python package.
ae7f2cf2 394bt_run_in_py_env() {
ad42b27e
PP
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:-}
ad42b27e 401 local -r main_lib_path=$BT_TESTS_BUILDDIR/../src/lib/.libs
0408934a 402
07ff3b19
PP
403 # Set the library search path so that the Python 3 interpreter can
404 # load `libbabeltrace2`.
c0d75ae3 405 if [[ $BT_TESTS_OS_TYPE == mingw || $BT_TESTS_OS_TYPE == cygwin ]]; then
ad42b27e 406 local -x PATH=$main_lib_path${PATH:+:}${PATH:-}
c0d75ae3 407 elif [[ $BT_TESTS_OS_TYPE == darwin ]]; then
ad42b27e 408 local -x DYLD_LIBRARY_PATH=$main_lib_path${DYLD_LIBRARY_PATH:+:}${DYLD_LIBRARY_PATH:-}
644e0364 409 else
ad42b27e 410 local -x LD_LIBRARY_PATH=$main_lib_path${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH:-}
e23e08c4
MJ
411 fi
412
07ff3b19
PP
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`.
c0d75ae3 416 if [[ $BT_TESTS_OS_TYPE == mingw ]]; then
1e14ec65
PP
417 local -x PYTHONHOME
418
68533c73 419 PYTHONHOME=$($BT_TESTS_PYTHON_CONFIG_BIN --prefix)
644e0364
MJ
420 fi
421
07ff3b19 422 # If AddressSanitizer is used, we must preload `libasan.so` so that
0b0893d4
SM
423 # libasan doesn't complain about not being the first loaded library.
424 #
07ff3b19
PP
425 # Python and sed (executed as part of the Libtool wrapper) produce
426 # some leaks, so we must unfortunately disable leak detection.
427 #
428 # Append it to existing `ASAN_OPTIONS` variable, such that we
429 # override the user's value if it contains `detect_leaks=1`.
c0d75ae3 430 if [[ ${BT_TESTS_ENABLE_ASAN:-} == 1 ]]; then
68533c73
SM
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)
ad42b27e 433 local -x LD_PRELOAD=$lib_asan${LD_PRELOAD:+:}${LD_PRELOAD:-}
2d12d310 434 fi
0b0893d4 435
c0d75ae3 436 local -x ASAN_OPTIONS=${ASAN_OPTIONS:-}${ASAN_OPTIONS:+,}detect_leaks=0
0b0893d4
SM
437 fi
438
fca73c71 439 bt_run_in_py_utils_env "$@"
644e0364
MJ
440}
441
07ff3b19
PP
442# Runs the Python tests matching the pattern `$2` (optional, `*` if
443# missing) in the directory `$1` using `testrunner.py`.
644e0364 444#
ae7f2cf2 445# This function uses bt_run_in_py_env(), therefore such tests can import
07ff3b19
PP
446# the testing Python modules (in `tests/utils/python`) and the `bt2`
447# Python package.
c3ca8136 448bt_run_py_test() {
c0d75ae3
PP
449 local -r test_dir=$1
450 local -r test_pattern=${2:-*}
7c577b1d
PP
451 local python_exec
452
c0d75ae3
PP
453 if [[ ${BT_TESTS_COVERAGE:-} == 1 ]]; then
454 python_exec=_bt_tests_check_coverage
644e0364 455 else
ad42b27e 456 python_exec=$BT_TESTS_PYTHON_BIN
644e0364
MJ
457 fi
458
ae7f2cf2 459 bt_run_in_py_env \
7121d33d
PP
460 "$python_exec" "$BT_TESTS_SRCDIR/utils/python/testrunner.py" \
461 --pattern "$test_pattern" "$test_dir"
a818a617 462
04c029a2 463 local -r ret=$?
644e0364 464
c0d75ae3 465 if [[ ${BT_TESTS_COVERAGE_REPORT:-} == 1 ]]; then
644e0364
MJ
466 coverage report -m
467 fi
468
c0d75ae3 469 if [[ ${BT_TESTS_COVERAGE_HTML:-} == 1 ]]; then
644e0364
MJ
470 coverage html
471 fi
472
473 return $ret
474}
89ec984e 475
07ff3b19
PP
476# Generates a CTF trace into the directory `$2` from the moultipart
477# document `$1` using `mctf.py`.
2fb231c0 478bt_gen_mctf_trace() {
c0d75ae3
PP
479 local -r input_file=$1
480 local -r base_dir=$2
ce8c884d
PP
481 local -r cmd=(
482 "$BT_TESTS_PYTHON_BIN" "$BT_TESTS_SRCDIR/utils/python/mctf.py"
483 --base-dir "$base_dir"
484 "$input_file"
485 )
486
487 echo "Running: \`${cmd[*]}\`" >&2
fca73c71 488 bt_run_in_py_utils_env "${cmd[@]}"
89ec984e 489}
bfa3d907
SM
490
491# Call `diag` with the contents of file `$1`.
492
493diag_file() {
494 diag "$(cat "$1")"
495}
This page took 0.089832 seconds and 4 git commands to generate.