tests/utils/utils.sh: bt_diff_details_ctf_single(): use `local -r` when possible
[babeltrace.git] / tests / utils / utils.sh
... / ...
CommitLineData
1#!/bin/bash
2#
3# SPDX-License-Identifier: GPL-2.0-only
4#
5# Copyright (c) 2019 Michael Jeanson <mjeanson@efficios.com>
6# Copyright (C) 2019-2023 Philippe Proulx <pproulx@efficios.com>
7
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).
24
25# An unbound variable is an error
26set -u
27
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.
43if [ -z "${BT_TESTS_OS_TYPE:-}" ]; then
44 BT_TESTS_OS_TYPE="$(uname -s)"
45 case "$BT_TESTS_OS_TYPE" in
46 MINGW*)
47 BT_TESTS_OS_TYPE="mingw"
48 ;;
49 Darwin)
50 BT_TESTS_OS_TYPE="darwin"
51 ;;
52 Linux)
53 BT_TESTS_OS_TYPE="linux"
54 ;;
55 CYGWIN*)
56 BT_TESTS_OS_TYPE="cygwin"
57 ;;
58 *)
59 BT_TESTS_OS_TYPE="unsupported"
60 ;;
61 esac
62fi
63export BT_TESTS_OS_TYPE
64
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.
73 local -r scriptdir="$(dirname "${BASH_SOURCE[0]}")"
74 local testsdir
75
76 if readlink -f "." >/dev/null 2>&1; then
77 testsdir=$(readlink -f "$scriptdir/..")
78 else
79 testsdir="$scriptdir/.."
80 fi
81
82 # Base source directory of tests
83 if [ -z "${BT_TESTS_SRCDIR:-}" ]; then
84 BT_TESTS_SRCDIR="$testsdir"
85 fi
86 export BT_TESTS_SRCDIR
87
88 # Base build directory of tests
89 if [ -z "${BT_TESTS_BUILDDIR:-}" ]; then
90 BT_TESTS_BUILDDIR="$testsdir"
91 fi
92 export BT_TESTS_BUILDDIR
93}
94
95_set_vars_srcdir_builddir
96unset -f _set_vars_srcdir_builddir
97
98# Sources the generated environment file (`env.sh`) if it exists.
99_source_env_sh() {
100 local -r env_sh_path="$BT_TESTS_BUILDDIR/utils/env.sh"
101
102 if [ -f "${env_sh_path}" ]; then
103 # shellcheck disable=SC1090,SC1091
104 . "${env_sh_path}"
105 fi
106}
107
108_source_env_sh
109unset -f _source_env_sh
110
111# Path to the `babeltrace2` command, if not set
112if [ -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="${BT_TESTS_BT2_BIN}.exe"
116 fi
117fi
118export BT_TESTS_BT2_BIN
119
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.
124_bt_tests_plugins_path="${BT_TESTS_BUILDDIR}/../src/plugins"
125
126# Colon-separated list of project plugin paths, if not set
127if [ -z "${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" ]; then
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"
129fi
130export BT_TESTS_BABELTRACE_PLUGIN_PATH
131
132# Directory containing the Python plugin provider library, if not set
133if [ -z "${BT_TESTS_PROVIDER_DIR:-}" ]; then
134 BT_TESTS_PROVIDER_DIR="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs"
135fi
136export BT_TESTS_PROVIDER_DIR
137
138# Directory containing the built `bt2` Python package, if not set
139if [ -z "${BT_TESTS_PYTHONPATH:-}" ]; then
140 BT_TESTS_PYTHONPATH="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib"
141fi
142export BT_TESTS_PYTHONPATH
143
144# Name of the `awk` command to use when testing, if not set
145if [ -z "${BT_TESTS_AWK_BIN:-}" ]; then
146 BT_TESTS_AWK_BIN="awk"
147fi
148export BT_TESTS_AWK_BIN
149
150# Name of the `grep` command to use when testing, if not set
151if [ -z "${BT_TESTS_GREP_BIN:-}" ]; then
152 BT_TESTS_GREP_BIN="grep"
153fi
154export BT_TESTS_GREP_BIN
155
156# Name of the `python3` command to use when testing, if not set
157if [ -z "${BT_TESTS_PYTHON_BIN:-}" ]; then
158 BT_TESTS_PYTHON_BIN="python3"
159fi
160export BT_TESTS_PYTHON_BIN
161
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.
166_bt_tests_py3_version=$("$BT_TESTS_PYTHON_BIN" -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))')
167
168# Name of the `python3-config` command to use when testing, if not set
169if [ -z "${BT_TESTS_PYTHON_CONFIG_BIN:-}" ]; then
170 BT_TESTS_PYTHON_CONFIG_BIN="python3-config"
171fi
172export BT_TESTS_PYTHON_CONFIG_BIN
173
174# Name of the `sed` command to use when testing, if not set
175if [ -z "${BT_TESTS_SED_BIN:-}" ]; then
176 BT_TESTS_SED_BIN="sed"
177fi
178export BT_TESTS_SED_BIN
179
180# Name of the `cc` command to use when testing, if not set
181if [ -z "${BT_TESTS_CC_BIN:-}" ]; then
182 BT_TESTS_CC_BIN="cc"
183fi
184export BT_TESTS_CC_BIN
185
186# Whether or not to enable AddressSanitizer, `0` (disabled) if not set.
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.
190if [ -z "${BT_TESTS_ENABLE_ASAN:-}" ]; then
191 BT_TESTS_ENABLE_ASAN="0"
192fi
193
194# Directory containing test data
195BT_TESTS_DATADIR="${BT_TESTS_SRCDIR}/data"
196
197# Directory containing test CTF traces
198BT_CTF_TRACES_PATH="${BT_TESTS_DATADIR}/ctf-traces"
199
200# Source the shell TAP utilities if `SH_TAP` is `1`
201if [ "${SH_TAP:-}" = 1 ]; then
202 # shellcheck source=./tap/tap.sh
203 . "${BT_TESTS_SRCDIR}/utils/tap/tap.sh"
204fi
205
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.
212bt_remove_cr() {
213 "$BT_TESTS_SED_BIN" -i'' -e 's/\r//g' "$1"
214}
215
216# Prints `$1` without CR characters.
217bt_remove_cr_inline() {
218 "$BT_TESTS_SED_BIN" 's/\r//g' "$1"
219}
220
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.
224#
225# The remaining arguments are forwarded to the `$BT_TESTS_BT2_BIN`
226# command.
227#
228# Returns the exit status of the executed `$BT_TESTS_BT2_BIN`.
229bt_cli() {
230 local -r stdout_file="$1"
231 local -r stderr_file="$2"
232 shift 2
233 local -r args=("$@")
234
235 echo "Running: $BT_TESTS_BT2_BIN ${args[*]}" >&2
236 run_python_bt2 "$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$stdout_file" 2>"$stderr_file"
237}
238
239# Checks the differences between:
240#
241# • The (expected) contents of the file having the path `$1`.
242#
243# • The contents of another file having the path `$2`.
244#
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.
249bt_diff() {
250 local -r expected_file="$1"
251 local -r actual_file="$2"
252 local ret=0
253
254 diff -u <(bt_remove_cr_inline "$expected_file") <(bt_remove_cr_inline "$actual_file") 1>&2
255
256 return $?
257}
258
259# Checks the difference between:
260#
261# • What the `$BT_TESTS_BT2_BIN` command prints to its standard output
262# when given the third and following arguments of this function.
263#
264# • The file having the path `$1`.
265#
266# as well as the difference between:
267#
268# • What the `$BT_TESTS_BT2_BIN` command prints to its standard error
269# when given the third and following arguments of this function.
270#
271# • The file having the path `$2`.
272#
273# Returns 0 if there's no difference, or 1 otherwise, also printing said
274# difference to the standard error.
275bt_diff_cli() {
276 local -r expected_stdout_file="$1"
277 local -r expected_stderr_file="$2"
278 shift 2
279 local -r args=("$@")
280
281 local -r temp_stdout_output_file="$(mktemp -t actual-stdout.XXXXXX)"
282 local -r temp_stderr_output_file="$(mktemp -t actual-stderr.XXXXXX)"
283 local ret=0
284
285 bt_cli "$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}"
286
287 bt_diff "$expected_stdout_file" "$temp_stdout_output_file" "${args[@]}"
288 local -r ret_stdout=$?
289 bt_diff "$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}"
290 local -r ret_stderr=$?
291
292 if ((ret_stdout != 0 || ret_stderr != 0)); then
293 ret=1
294 fi
295
296 rm -f "$temp_stdout_output_file" "$temp_stderr_output_file"
297
298 return $ret
299}
300
301# Checks the difference between:
302#
303# • The content of the file having the path `$1`.
304#
305# • What the `$BT_TESTS_BT2_BIN` command prints to the standard output
306# when executed with:
307#
308# 1. The CTF trace directory `$2`.
309# 2. The arguments `-c` and `sink.text.details`.
310# 3. The third and following arguments of this function.
311#
312# Returns 0 if there's no difference, or 1 otherwise, also printing said
313# difference to the standard error.
314bt_diff_details_ctf_single() {
315 local -r expected_stdout_file="$1"
316 local -r trace_dir="$2"
317 shift 2
318 local -r extra_details_args=("$@")
319 expected_stderr_file="/dev/null"
320
321 # Compare using the CLI with `sink.text.details`
322 bt_diff_cli "$expected_stdout_file" "$expected_stderr_file" "$trace_dir" \
323 "-c" "sink.text.details" "${extra_details_args[@]+${extra_details_args[@]}}"
324}
325
326# Like bt_diff_details_ctf_single(), except that `$1` is the path to a
327# program which generates the CTF trace to compare to.
328#
329# The program `$1` receives the path to a temporary, empty directory
330# where to write the CTF trace as its first argument.
331bt_diff_details_ctf_gen_single() {
332 local ctf_gen_prog_path="$1"
333 local expected_stdout_file="$2"
334 shift 2
335 local extra_details_args=("$@")
336
337 local temp_trace_dir
338 local ret
339
340 temp_trace_dir="$(mktemp -d)"
341
342 # Run the CTF trace generator program to get a CTF trace
343 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then
344 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
345 rm -rf "$temp_trace_dir"
346 return 1
347 fi
348
349 # Compare using the CLI with `sink.text.details`
350 bt_diff_details_ctf_single "$expected_stdout_file" "$temp_trace_dir" \
351 "${extra_details_args[@]+${extra_details_args[@]}}"
352 ret=$?
353 rm -rf "$temp_trace_dir"
354 return $ret
355}
356
357# Like `grep`, but using `$BT_TESTS_GREP_BIN`.
358bt_grep() {
359 "$BT_TESTS_GREP_BIN" "$@"
360}
361
362# ok() with the test name `$3` on the result of bt_grep() matching the
363# pattern `$1` within the file `$2`.
364bt_grep_ok() {
365 local pattern=$1
366 local file=$2
367 local test_name=$3
368
369 bt_grep --silent "$pattern" "$file"
370
371 local ret=$?
372
373 if ! ok $ret "$test_name"; then
374 {
375 echo "Pattern \`$pattern\` doesn't match the contents of \`$file\`:"
376 echo '--- 8< ---'
377 cat "$file"
378 echo '--- >8 ---'
379 } >&2
380 fi
381
382 return $ret
383}
384
385# Forwards the arguments to `coverage run`.
386check_coverage() {
387 coverage run "$@"
388}
389
390# Executes a command within an environment which can import the testing
391# Python modules (in `tests/utils/python`).
392run_python() {
393 local our_pythonpath="${BT_TESTS_SRCDIR}/utils/python"
394
395 if [[ $_bt_tests_py3_version =~ 3.[45] ]]; then
396 # Add a local directory containing a `typing.py` to `PYTHONPATH`
397 # for Python 3.4 and Python 3.5 which either don't offer the
398 # `typing` module at all, or offer a partial one.
399 our_pythonpath="$our_pythonpath:${BT_TESTS_SRCDIR}/utils/python/typing"
400 fi
401
402 PYTHONPATH="${our_pythonpath}${PYTHONPATH:+:}${PYTHONPATH:-}" "$@"
403}
404
405# Executes a command within an environment which can import the testing
406# Python modules (in `tests/utils/python`) and the `bt2` Python package.
407run_python_bt2() {
408 local lib_asan
409 local -x "BABELTRACE_PLUGIN_PATH=${BT_TESTS_BABELTRACE_PLUGIN_PATH}"
410 local -x "LIBBABELTRACE2_PLUGIN_PROVIDER_DIR=${BT_TESTS_PROVIDER_DIR}"
411 local -x "BT_TESTS_DATADIR=${BT_TESTS_DATADIR}"
412 local -x "BT_CTF_TRACES_PATH=${BT_CTF_TRACES_PATH}"
413 local -x "BT_PLUGINS_PATH=${_bt_tests_plugins_path}"
414 local -x "PYTHONPATH=${BT_TESTS_PYTHONPATH}${PYTHONPATH:+:}${PYTHONPATH:-}"
415
416 local main_lib_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs"
417
418 # Set the library search path so that the Python 3 interpreter can
419 # load `libbabeltrace2`.
420 if [ "$BT_TESTS_OS_TYPE" = "mingw" ] || [ "$BT_TESTS_OS_TYPE" = "cygwin" ]; then
421 local -x PATH="${main_lib_path}${PATH:+:}${PATH:-}"
422 elif [ "$BT_TESTS_OS_TYPE" = "darwin" ]; then
423 local -x DYLD_LIBRARY_PATH="${main_lib_path}${DYLD_LIBRARY_PATH:+:}${DYLD_LIBRARY_PATH:-}"
424 else
425 local -x LD_LIBRARY_PATH="${main_lib_path}${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH:-}"
426 fi
427
428 # On Windows, an embedded Python 3 interpreter needs a way to locate
429 # the path to its internal modules: set the `PYTHONHOME` variable to
430 # the prefix from `python3-config`.
431 if [ "$BT_TESTS_OS_TYPE" = "mingw" ]; then
432 local -x PYTHONHOME
433
434 PYTHONHOME=$("$BT_TESTS_PYTHON_CONFIG_BIN" --prefix)
435 fi
436
437 # If AddressSanitizer is used, we must preload `libasan.so` so that
438 # libasan doesn't complain about not being the first loaded library.
439 #
440 # Python and sed (executed as part of the Libtool wrapper) produce
441 # some leaks, so we must unfortunately disable leak detection.
442 #
443 # Append it to existing `ASAN_OPTIONS` variable, such that we
444 # override the user's value if it contains `detect_leaks=1`.
445 if [ "${BT_TESTS_ENABLE_ASAN:-}" = "1" ]; then
446 if "${BT_TESTS_CC_BIN}" --version | head -n 1 | bt_grep -q '^gcc'; then
447 lib_asan="$("${BT_TESTS_CC_BIN}" -print-file-name=libasan.so)"
448 local -x LD_PRELOAD="${lib_asan}${LD_PRELOAD:+:}${LD_PRELOAD:-}"
449 fi
450
451 local -x "ASAN_OPTIONS=${ASAN_OPTIONS:-}${ASAN_OPTIONS:+,}detect_leaks=0"
452 fi
453
454 run_python "$@"
455}
456
457# Runs the Python tests matching the pattern `$2` (optional, `*` if
458# missing) in the directory `$1` using `testrunner.py`.
459#
460# This function uses run_python_bt2(), therefore such tests can import
461# the testing Python modules (in `tests/utils/python`) and the `bt2`
462# Python package.
463run_python_bt2_test() {
464 local test_dir="$1"
465 local test_pattern="${2:-'*'}"
466
467 local ret
468 local test_runner_args=()
469
470 test_runner_args+=("$test_dir")
471 if [ -n "${test_pattern}" ]; then
472 test_runner_args+=("${test_pattern}")
473 fi
474
475 if test "${BT_TESTS_COVERAGE:-}" = "1"; then
476 python_exec="check_coverage"
477 else
478 python_exec="${BT_TESTS_PYTHON_BIN}"
479 fi
480
481 run_python_bt2 \
482 "${python_exec}" \
483 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
484 --pattern "$test_pattern" \
485 "$test_dir" \
486
487 ret=$?
488
489 if test "${BT_TESTS_COVERAGE_REPORT:-}" = "1"; then
490 coverage report -m
491 fi
492
493 if test "${BT_TESTS_COVERAGE_HTML:-}" = "1"; then
494 coverage html
495 fi
496
497 return $ret
498}
499
500# Generates a CTF trace into the directory `$2` from the moultipart
501# document `$1` using `mctf.py`.
502gen_mctf_trace() {
503 local input_file="$1"
504 local base_dir="$2"
505
506 diag "Running: ${BT_TESTS_PYTHON_BIN} ${BT_TESTS_SRCDIR}/utils/python/mctf.py --base-dir ${base_dir} ${input_file}"
507 run_python "${BT_TESTS_PYTHON_BIN}" "${BT_TESTS_SRCDIR}/utils/python/mctf.py" \
508 --base-dir "${base_dir}" "${input_file}"
509}
This page took 0.024754 seconds and 4 git commands to generate.