Fix: `babeltrace2 convert`: don't consider the `--plugin-path` opt.
[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
MJ
5# Copyright (c) 2019 Michael Jeanson <mjeanson@efficios.com>
6# Copyright (C) 2019 Philippe Proulx <pproulx@efficios.com>
644e0364 7#
644e0364
MJ
8
9# This file is meant to be sourced at the start of shell script-based tests.
10
11
12# Error out when encountering an undefined variable
13set -u
14
9c5dd55a
MJ
15# If "readlink -f" is available, get a resolved absolute path to the
16# tests source dir, otherwise make do with a relative path.
644e0364 17scriptdir="$(dirname "${BASH_SOURCE[0]}")"
9c5dd55a
MJ
18if readlink -f "." >/dev/null 2>&1; then
19 testsdir=$(readlink -f "$scriptdir/..")
20else
21 testsdir="$scriptdir/.."
22fi
644e0364 23
5058d31b
JR
24# The OS on which we are running. See [1] for possible values of 'uname -s'.
25# We do a bit of translation to ease our life down the road for comparison.
26# Export it so that called executables can use it.
27# [1] https://en.wikipedia.org/wiki/Uname#Examples
75e396f6 28if [ -z "${BT_TESTS_OS_TYPE:-}" ]; then
a0baab4a
SM
29 BT_TESTS_OS_TYPE="$(uname -s)"
30 case "$BT_TESTS_OS_TYPE" in
5058d31b 31 MINGW*)
a0baab4a 32 BT_TESTS_OS_TYPE="mingw"
5058d31b
JR
33 ;;
34 Darwin)
a0baab4a 35 BT_TESTS_OS_TYPE="darwin"
5058d31b
JR
36 ;;
37 Linux)
a0baab4a 38 BT_TESTS_OS_TYPE="linux"
5058d31b 39 ;;
df9db467 40 CYGWIN*)
a0baab4a 41 BT_TESTS_OS_TYPE="cygwin"
df9db467 42 ;;
5058d31b 43 *)
a0baab4a 44 BT_TESTS_OS_TYPE="unsupported"
5058d31b
JR
45 ;;
46 esac
47fi
a0baab4a 48export BT_TESTS_OS_TYPE
5058d31b 49
644e0364 50# Allow overriding the source and build directories
75e396f6 51if [ -z "${BT_TESTS_SRCDIR:-}" ]; then
9c5dd55a 52 BT_TESTS_SRCDIR="$testsdir"
644e0364
MJ
53fi
54export BT_TESTS_SRCDIR
55
75e396f6 56if [ -z "${BT_TESTS_BUILDDIR:-}" ]; then
9c5dd55a 57 BT_TESTS_BUILDDIR="$testsdir"
644e0364
MJ
58fi
59export BT_TESTS_BUILDDIR
60
e46cbefe
MJ
61
62# Source the generated environment file if it's present.
63if [ -f "${BT_TESTS_BUILDDIR}/utils/env.sh" ]; then
05ffcfc0 64 # shellcheck disable=SC1091
e46cbefe 65 . "${BT_TESTS_BUILDDIR}/utils/env.sh"
644e0364
MJ
66fi
67
68# Allow overriding the babeltrace2 executables
75e396f6 69if [ -z "${BT_TESTS_BT2_BIN:-}" ]; then
4881a20e 70 BT_TESTS_BT2_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2"
a0baab4a 71 if [ "$BT_TESTS_OS_TYPE" = "mingw" ]; then
27e2c58d
JR
72 BT_TESTS_BT2_BIN="${BT_TESTS_BT2_BIN}.exe"
73 fi
644e0364
MJ
74fi
75export BT_TESTS_BT2_BIN
76
644e0364
MJ
77# TODO: Remove when bindings/python/bt2/test_plugin.py is fixed
78BT_PLUGINS_PATH="${BT_TESTS_BUILDDIR}/../src/plugins"
79
80# Allow overriding the babeltrace2 plugin path
75e396f6 81if [ -z "${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" ]; then
e8cafc6e 82 BT_TESTS_BABELTRACE_PLUGIN_PATH="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text:${BT_PLUGINS_PATH}/lttng-utils"
644e0364 83fi
e46cbefe 84export BT_TESTS_BABELTRACE_PLUGIN_PATH
644e0364 85
75e396f6 86if [ -z "${BT_TESTS_PROVIDER_DIR:-}" ]; then
b14c7bf1
MJ
87 BT_TESTS_PROVIDER_DIR="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs"
88fi
e46cbefe 89export BT_TESTS_PROVIDER_DIR
b14c7bf1 90
644e0364 91# Allow overriding the babeltrace2 executables
75e396f6 92if [ -z "${BT_TESTS_PYTHONPATH:-}" ]; then
644e0364
MJ
93 BT_TESTS_PYTHONPATH="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib"
94fi
e46cbefe 95export BT_TESTS_PYTHONPATH
644e0364
MJ
96
97
98### External Tools ###
75e396f6 99if [ -z "${BT_TESTS_AWK_BIN:-}" ]; then
644e0364
MJ
100 BT_TESTS_AWK_BIN="awk"
101fi
102export BT_TESTS_AWK_BIN
103
75e396f6 104if [ -z "${BT_TESTS_GREP_BIN:-}" ]; then
644e0364
MJ
105 BT_TESTS_GREP_BIN="grep"
106fi
107export BT_TESTS_GREP_BIN
108
75e396f6 109if [ -z "${BT_TESTS_PYTHON_BIN:-}" ]; then
644e0364
MJ
110 BT_TESTS_PYTHON_BIN="python3"
111fi
112export BT_TESTS_PYTHON_BIN
113
7747a39f
PP
114BT_TESTS_PYTHON_VERSION=$($BT_TESTS_PYTHON_BIN -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))')
115
75e396f6 116if [ -z "${BT_TESTS_PYTHON_CONFIG_BIN:-}" ]; then
e23e08c4
MJ
117 BT_TESTS_PYTHON_CONFIG_BIN="python3-config"
118fi
242ddcb7 119export BT_TESTS_PYTHON_CONFIG_BIN
e23e08c4 120
75e396f6 121if [ -z "${BT_TESTS_SED_BIN:-}" ]; then
644e0364
MJ
122 BT_TESTS_SED_BIN="sed"
123fi
124export BT_TESTS_SED_BIN
125
75e396f6 126if [ -z "${BT_TESTS_CC_BIN:-}" ]; then
0b0893d4
SM
127 BT_TESTS_CC_BIN="cc"
128fi
129export BT_TESTS_CC_BIN
130
131
132### Optional features ###
133
75e396f6 134if [ -z "${BT_TESTS_ENABLE_ASAN:-}" ]; then
0b0893d4
SM
135 BT_TESTS_ENABLE_ASAN="0"
136fi
137export BT_TESTS_ENABLE_ASAN
138
644e0364
MJ
139
140# Data files path
141BT_TESTS_DATADIR="${BT_TESTS_SRCDIR}/data"
142BT_CTF_TRACES_PATH="${BT_TESTS_DATADIR}/ctf-traces"
644e0364 143
e46cbefe
MJ
144# By default, it will not source tap.sh. If you want to output tap directly
145# from the test script, define the 'SH_TAP' variable to '1' before sourcing
146# this script.
75e396f6 147if [ "${SH_TAP:-}" = 1 ]; then
e46cbefe
MJ
148 # shellcheck source=./tap/tap.sh
149 . "${BT_TESTS_SRCDIR}/utils/tap/tap.sh"
150fi
151
152
90a8a0f2
SM
153# Remove CR characters in file "$1".
154
155bt_remove_cr() {
717a01a7 156 "$BT_TESTS_SED_BIN" -i'' -e 's/\r//g' "$1"
90a8a0f2
SM
157}
158
22703f66
SM
159# Run the Babeltrace CLI, redirecting stdout and stderr to specified files.
160#
161# $1: file to redirect stdout to
162# $2: file to redirect stderr to
163# remaining args: arguments to pass to the CLI
164#
165# Return the exit code of the CLI.
166
167bt_cli() {
168 local stdout_file="$1"
169 local stderr_file="$2"
170 shift 2
171 local args=("$@")
172
ea458b95 173 echo "Running: $BT_TESTS_BT2_BIN ${args[*]}" >&2
22703f66
SM
174 run_python_bt2 "$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$stdout_file" 2>"$stderr_file"
175}
644e0364
MJ
176
177### Diff Functions ###
178
a70b6702
SM
179# Check the differences between two files (typically some expected output vs
180# some actual output). If there are differences, print the diff to stderr.
ff89ed28 181#
a70b6702
SM
182# $1: file 1 (expected)
183# $2: file 2 (actual)
ff89ed28 184#
ea458b95 185# Return 0 if there's no difference, and non-zero if there are.
ff89ed28 186#
a70b6702
SM
187# Note that this function modifies the actual output file ($2) _in-place_ to
188# remove any \r character.
189
ff89ed28 190bt_diff() {
a70b6702
SM
191 local expected_file="$1"
192 local actual_file="$2"
ff89ed28 193 local ret=0
ff89ed28
MJ
194
195 # Strip any \r present due to Windows (\n -> \r\n).
196 # "diff --string-trailing-cr" is not used since it is not present on
197 # Solaris.
90a8a0f2 198 bt_remove_cr "$actual_file"
ff89ed28 199
ea458b95 200 diff -u "$expected_file" "$actual_file" 1>&2
ff89ed28 201
ea458b95 202 return $?
ff89ed28
MJ
203}
204
58db335e
FD
205# Checks the difference between:
206#
207# 1. What the CLI outputs on its standard output when given the arguments
208# "$@" (excluding the first two arguments).
209# 2. The file with path "$1".
210#
211# And the difference between:
212#
213# 1. What the CLI outputs on its standard error when given the arguments
214# "$@" (excluding the first two arguments).
215# 2. The file with path "$2".
644e0364
MJ
216#
217# Returns 0 if there's no difference, and 1 if there is, also printing
218# said difference to the standard error.
219bt_diff_cli() {
58db335e
FD
220 local expected_stdout_file="$1"
221 local expected_stderr_file="$2"
222 shift 2
53cc240b
MJ
223 local args=("$@")
224
58db335e
FD
225 local temp_stdout_output_file
226 local temp_stderr_output_file
644e0364 227 local ret=0
a70b6702
SM
228 local ret_stdout
229 local ret_stderr
644e0364 230
7132b838
PP
231 temp_stdout_output_file="$(mktemp -t actual-stdout.XXXXXX)"
232 temp_stderr_output_file="$(mktemp -t actual-stderr.XXXXXX)"
644e0364 233
58db335e 234 # Run the CLI to get a detailed file.
22703f66 235 bt_cli "$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}"
58db335e 236
a70b6702
SM
237 bt_diff "$expected_stdout_file" "$temp_stdout_output_file" "${args[@]}"
238 ret_stdout=$?
239 bt_diff "$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}"
240 ret_stderr=$?
241
242 if ((ret_stdout != 0 || ret_stderr != 0)); then
243 ret=1
244 fi
58db335e 245
ff89ed28 246 rm -f "$temp_stdout_output_file" "$temp_stderr_output_file"
644e0364 247
ff89ed28
MJ
248 return $ret
249}
250
53cc240b
MJ
251# Checks the difference between the content of the file with path "$1"
252# and the output of the CLI when called on the directory path "$2" with
253# the arguments '-c sink.text.details' and the rest of the arguments to
254# this function.
644e0364
MJ
255#
256# Returns 0 if there's no difference, and 1 if there is, also printing
257# said difference to the standard error.
258bt_diff_details_ctf_single() {
58db335e 259 local expected_stdout_file="$1"
53cc240b
MJ
260 local trace_dir="$2"
261 shift 2
262 local extra_details_args=("$@")
58db335e 263 expected_stderr_file="/dev/null"
644e0364
MJ
264
265 # Compare using the CLI with `sink.text.details`
8b729209
PP
266 bt_diff_cli "$expected_stdout_file" "$expected_stderr_file" "$trace_dir" \
267 "-c" "sink.text.details" "${extra_details_args[@]+${extra_details_args[@]}}"
644e0364
MJ
268}
269
270# Calls bt_diff_details_ctf_single(), except that "$1" is the path to a
271# program which generates the CTF trace to compare to. The program "$1"
272# receives the path to a temporary, empty directory where to write the
273# CTF trace as its first argument.
274bt_diff_details_ctf_gen_single() {
275 local ctf_gen_prog_path="$1"
58db335e 276 local expected_stdout_file="$2"
53cc240b
MJ
277 shift 2
278 local extra_details_args=("$@")
644e0364
MJ
279
280 local temp_trace_dir
281 local ret
282
283 temp_trace_dir="$(mktemp -d)"
284
285 # Run the CTF trace generator program to get a CTF trace
286 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then
287 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
288 rm -rf "$temp_trace_dir"
289 return 1
290 fi
291
292 # Compare using the CLI with `sink.text.details`
8b729209
PP
293 bt_diff_details_ctf_single "$expected_stdout_file" "$temp_trace_dir" \
294 "${extra_details_args[@]+${extra_details_args[@]}}"
644e0364
MJ
295 ret=$?
296 rm -rf "$temp_trace_dir"
297 return $ret
298}
299
407d3b10
SM
300# Run the grep binary configured for the tests.
301bt_grep() {
302 "$BT_TESTS_GREP_BIN" "$@"
303}
644e0364 304
db01f759
SM
305# ok() with the test name `$3` on the result of bt_grep() matching the
306# pattern `$1` within the file `$2`.
307bt_grep_ok() {
308 local pattern=$1
309 local file=$2
310 local test_name=$3
311
312 bt_grep --silent "$pattern" "$file"
313
314 local ret=$?
315
316 if ! ok $ret "$test_name"; then
317 {
318 echo "Pattern \`$pattern\` doesn't match the contents of \`$file\`:"
319 echo '--- 8< ---'
320 cat "$file"
321 echo '--- >8 ---'
322 } >&2
323 fi
324
325 return $ret
326}
327
644e0364
MJ
328### Functions ###
329
330check_coverage() {
331 coverage run "$@"
332}
333
1e14ec65
PP
334# Execute a shell command in the appropriate environment to access the Python
335# test utility modules in `tests/utils/python`.
336run_python() {
7747a39f
PP
337 local our_pythonpath="${BT_TESTS_SRCDIR}/utils/python"
338
5f62b715 339 if [[ $BT_TESTS_PYTHON_VERSION =~ 3.[45] ]]; then
7747a39f
PP
340 # Add a local directory containing a `typing.py` to `PYTHONPATH` for
341 # Python 3.4 which doesn't offer the `typing` module.
342 our_pythonpath="$our_pythonpath:${BT_TESTS_SRCDIR}/utils/python/typing"
343 fi
344
345 PYTHONPATH="${our_pythonpath}${PYTHONPATH:+:}${PYTHONPATH:-}" "$@"
1e14ec65
PP
346}
347
644e0364
MJ
348# Execute a shell command in the appropriate environment to have access to the
349# bt2 Python bindings.
350run_python_bt2() {
0b0893d4 351 local lib_asan
1e14ec65
PP
352 local -x "BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1"
353 local -x "BABELTRACE_PLUGIN_PATH=${BT_TESTS_BABELTRACE_PLUGIN_PATH}"
354 local -x "LIBBABELTRACE2_PLUGIN_PROVIDER_DIR=${BT_TESTS_PROVIDER_DIR}"
355 local -x "BT_TESTS_DATADIR=${BT_TESTS_DATADIR}"
356 local -x "BT_CTF_TRACES_PATH=${BT_CTF_TRACES_PATH}"
357 local -x "BT_PLUGINS_PATH=${BT_PLUGINS_PATH}"
358 local -x "PYTHONPATH=${BT_TESTS_PYTHONPATH}${PYTHONPATH:+:}${PYTHONPATH:-}"
644e0364 359
0408934a
SM
360 local main_lib_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs"
361
644e0364 362 # Set the library search path so the python interpreter can load libbabeltrace2
a0baab4a 363 if [ "$BT_TESTS_OS_TYPE" = "mingw" ] || [ "$BT_TESTS_OS_TYPE" = "cygwin" ]; then
1e14ec65 364 local -x PATH="${main_lib_path}${PATH:+:}${PATH:-}"
a0baab4a 365 elif [ "$BT_TESTS_OS_TYPE" = "darwin" ]; then
1e14ec65 366 local -x DYLD_LIBRARY_PATH="${main_lib_path}${DYLD_LIBRARY_PATH:+:}${DYLD_LIBRARY_PATH:-}"
644e0364 367 else
1e14ec65 368 local -x LD_LIBRARY_PATH="${main_lib_path}${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH:-}"
e23e08c4
MJ
369 fi
370
371 # On Windows, an embedded Python interpreter needs a way to locate the path
1e14ec65 372 # to its internal modules, set the prefix from python-config to the
e23e08c4 373 # PYTHONHOME variable.
a0baab4a 374 if [ "$BT_TESTS_OS_TYPE" = "mingw" ]; then
1e14ec65
PP
375 local -x PYTHONHOME
376
377 PYTHONHOME=$($BT_TESTS_PYTHON_CONFIG_BIN --prefix)
644e0364
MJ
378 fi
379
0b0893d4
SM
380 # If AddressSanitizer is used, we must preload libasan.so so that
381 # libasan doesn't complain about not being the first loaded library.
382 #
383 # Python and sed (executed as part of the libtool wrapper) produce some
384 # leaks, so we must unfortunately disable leak detection. Append it to
385 # existing ASAN_OPTIONS, such that we override the user's value if it
386 # contains detect_leaks=1.
75e396f6 387 if [ "${BT_TESTS_ENABLE_ASAN:-}" = "1" ]; then
407d3b10 388 if ${BT_TESTS_CC_BIN} --version | head -n 1 | bt_grep -q '^gcc'; then
1e14ec65
PP
389 lib_asan="$(${BT_TESTS_CC_BIN} -print-file-name=libasan.so)"
390 local -x LD_PRELOAD="${lib_asan}${LD_PRELOAD:+:}${LD_PRELOAD:-}"
2d12d310 391 fi
0b0893d4 392
1e14ec65 393 local -x "ASAN_OPTIONS=${ASAN_OPTIONS:-}${ASAN_OPTIONS:+,}detect_leaks=0"
0b0893d4
SM
394 fi
395
1e14ec65 396 run_python "$@"
644e0364
MJ
397}
398
399# Set the environment and run python tests in the directory.
400#
401# $1 : The directory containing the python test scripts
402# $2 : The pattern to match python test script names (optional)
644e0364
MJ
403run_python_bt2_test() {
404 local test_dir="$1"
a818a617 405 local test_pattern="${2:-'*'}" # optional, if none default to "*"
644e0364
MJ
406
407 local ret
408 local test_runner_args=()
409
410 test_runner_args+=("$test_dir")
75e396f6 411 if [ -n "${test_pattern}" ]; then
644e0364
MJ
412 test_runner_args+=("${test_pattern}")
413 fi
414
75e396f6 415 if test "${BT_TESTS_COVERAGE:-}" = "1"; then
644e0364
MJ
416 python_exec="check_coverage"
417 else
418 python_exec="${BT_TESTS_PYTHON_BIN}"
419 fi
420
421 run_python_bt2 \
422 "${python_exec}" \
423 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
a818a617
FD
424 --pattern "$test_pattern" \
425 "$test_dir" \
426
644e0364
MJ
427 ret=$?
428
75e396f6 429 if test "${BT_TESTS_COVERAGE_REPORT:-}" = "1"; then
644e0364
MJ
430 coverage report -m
431 fi
432
75e396f6 433 if test "${BT_TESTS_COVERAGE_HTML:-}" = "1"; then
644e0364
MJ
434 coverage html
435 fi
436
437 return $ret
438}
89ec984e
SM
439
440# Generate a CTF trace using `mctf.py`.
441#
442# $1: Input filename
443# $2: Base directory path for output files
444gen_mctf_trace() {
445 local input_file="$1"
446 local base_dir="$2"
447
448 diag "Running: ${BT_TESTS_PYTHON_BIN} ${BT_TESTS_SRCDIR}/utils/python/mctf.py --base-dir ${base_dir} ${input_file}"
ec48dce5 449 run_python "${BT_TESTS_PYTHON_BIN}" "${BT_TESTS_SRCDIR}/utils/python/mctf.py" \
89ec984e
SM
450 --base-dir "${base_dir}" "${input_file}"
451}
This page took 0.079455 seconds and 4 git commands to generate.