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