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