Move to kernel style SPDX license identifiers
[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
28if [ "x${BT_OS_TYPE:-}" = "x" ]; then
29 BT_OS_TYPE="$(uname -s)"
30 case "$BT_OS_TYPE" in
31 MINGW*)
32 BT_OS_TYPE="mingw"
33 ;;
34 Darwin)
35 BT_OS_TYPE="darwin"
36 ;;
37 Linux)
38 BT_OS_TYPE="linux"
39 ;;
df9db467
JR
40 CYGWIN*)
41 BT_OS_TYPE="cygwin"
42 ;;
5058d31b
JR
43 *)
44 BT_OS_TYPE="unsupported"
45 ;;
46 esac
47fi
48export BT_OS_TYPE
49
644e0364
MJ
50# Allow overriding the source and build directories
51if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then
9c5dd55a 52 BT_TESTS_SRCDIR="$testsdir"
644e0364
MJ
53fi
54export BT_TESTS_SRCDIR
55
56if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then
9c5dd55a 57 BT_TESTS_BUILDDIR="$testsdir"
644e0364
MJ
58fi
59export BT_TESTS_BUILDDIR
60
61# By default, it will not source tap.sh. If you to tap output directly from
62# the test script, define the 'SH_TAP' variable to '1' before sourcing this
63# script.
64if [ "x${SH_TAP:-}" = x1 ]; then
1d970e04 65 # shellcheck source=./tap/tap.sh
644e0364
MJ
66 . "${BT_TESTS_SRCDIR}/utils/tap/tap.sh"
67fi
68
69# Allow overriding the babeltrace2 executables
70if [ "x${BT_TESTS_BT2_BIN:-}" = "x" ]; then
4881a20e 71 BT_TESTS_BT2_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2"
5058d31b 72 if [ "$BT_OS_TYPE" = "mingw" ]; then
27e2c58d
JR
73 BT_TESTS_BT2_BIN="${BT_TESTS_BT2_BIN}.exe"
74 fi
644e0364
MJ
75fi
76export BT_TESTS_BT2_BIN
77
644e0364
MJ
78# TODO: Remove when bindings/python/bt2/test_plugin.py is fixed
79BT_PLUGINS_PATH="${BT_TESTS_BUILDDIR}/../src/plugins"
80
81# Allow overriding the babeltrace2 plugin path
82if [ "x${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" = "x" ]; then
e8cafc6e 83 BT_TESTS_BABELTRACE_PLUGIN_PATH="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text:${BT_PLUGINS_PATH}/lttng-utils"
644e0364
MJ
84fi
85
b14c7bf1
MJ
86if [ "x${BT_TESTS_PROVIDER_DIR:-}" = "x" ]; then
87 BT_TESTS_PROVIDER_DIR="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs"
88fi
89
644e0364
MJ
90# Allow overriding the babeltrace2 executables
91if [ "x${BT_TESTS_PYTHONPATH:-}" = "x" ]; then
92 BT_TESTS_PYTHONPATH="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib"
93fi
94
95
96### External Tools ###
97if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then
98 BT_TESTS_AWK_BIN="awk"
99fi
100export BT_TESTS_AWK_BIN
101
102if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then
103 BT_TESTS_GREP_BIN="grep"
104fi
105export BT_TESTS_GREP_BIN
106
107if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then
108 BT_TESTS_PYTHON_BIN="python3"
109fi
110export BT_TESTS_PYTHON_BIN
111
e23e08c4
MJ
112if [ "x${BT_TESTS_PYTHON_CONFIG_BIN:-}" = "x" ]; then
113 BT_TESTS_PYTHON_CONFIG_BIN="python3-config"
114fi
242ddcb7 115export BT_TESTS_PYTHON_CONFIG_BIN
e23e08c4 116
644e0364
MJ
117if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then
118 BT_TESTS_SED_BIN="sed"
119fi
120export BT_TESTS_SED_BIN
121
122
123# Data files path
124BT_TESTS_DATADIR="${BT_TESTS_SRCDIR}/data"
125BT_CTF_TRACES_PATH="${BT_TESTS_DATADIR}/ctf-traces"
644e0364 126
90a8a0f2
SM
127# Remove CR characters in file "$1".
128
129bt_remove_cr() {
130 "$BT_TESTS_SED_BIN" -i 's/\r//g' "$1"
131}
132
22703f66
SM
133# Run the Babeltrace CLI, redirecting stdout and stderr to specified files.
134#
135# $1: file to redirect stdout to
136# $2: file to redirect stderr to
137# remaining args: arguments to pass to the CLI
138#
139# Return the exit code of the CLI.
140
141bt_cli() {
142 local stdout_file="$1"
143 local stderr_file="$2"
144 shift 2
145 local args=("$@")
146
ea458b95 147 echo "Running: $BT_TESTS_BT2_BIN ${args[*]}" >&2
22703f66
SM
148 run_python_bt2 "$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$stdout_file" 2>"$stderr_file"
149}
644e0364
MJ
150
151### Diff Functions ###
152
a70b6702
SM
153# Check the differences between two files (typically some expected output vs
154# some actual output). If there are differences, print the diff to stderr.
ff89ed28 155#
a70b6702
SM
156# $1: file 1 (expected)
157# $2: file 2 (actual)
ff89ed28 158#
ea458b95 159# Return 0 if there's no difference, and non-zero if there are.
ff89ed28 160#
a70b6702
SM
161# Note that this function modifies the actual output file ($2) _in-place_ to
162# remove any \r character.
163
ff89ed28 164bt_diff() {
a70b6702
SM
165 local expected_file="$1"
166 local actual_file="$2"
ff89ed28 167 local ret=0
ff89ed28
MJ
168
169 # Strip any \r present due to Windows (\n -> \r\n).
170 # "diff --string-trailing-cr" is not used since it is not present on
171 # Solaris.
90a8a0f2 172 bt_remove_cr "$actual_file"
ff89ed28 173
ea458b95 174 diff -u "$expected_file" "$actual_file" 1>&2
ff89ed28 175
ea458b95 176 return $?
ff89ed28
MJ
177}
178
58db335e
FD
179# Checks the difference between:
180#
181# 1. What the CLI outputs on its standard output when given the arguments
182# "$@" (excluding the first two arguments).
183# 2. The file with path "$1".
184#
185# And the difference between:
186#
187# 1. What the CLI outputs on its standard error when given the arguments
188# "$@" (excluding the first two arguments).
189# 2. The file with path "$2".
644e0364
MJ
190#
191# Returns 0 if there's no difference, and 1 if there is, also printing
192# said difference to the standard error.
193bt_diff_cli() {
58db335e
FD
194 local expected_stdout_file="$1"
195 local expected_stderr_file="$2"
196 shift 2
53cc240b
MJ
197 local args=("$@")
198
58db335e
FD
199 local temp_stdout_output_file
200 local temp_stderr_output_file
644e0364 201 local ret=0
a70b6702
SM
202 local ret_stdout
203 local ret_stderr
644e0364 204
ff89ed28
MJ
205 temp_stdout_output_file="$(mktemp -t actual_stdout.XXXXXX)"
206 temp_stderr_output_file="$(mktemp -t actual_stderr.XXXXXX)"
644e0364 207
58db335e 208 # Run the CLI to get a detailed file.
22703f66 209 bt_cli "$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}"
58db335e 210
a70b6702
SM
211 bt_diff "$expected_stdout_file" "$temp_stdout_output_file" "${args[@]}"
212 ret_stdout=$?
213 bt_diff "$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}"
214 ret_stderr=$?
215
216 if ((ret_stdout != 0 || ret_stderr != 0)); then
217 ret=1
218 fi
58db335e 219
ff89ed28 220 rm -f "$temp_stdout_output_file" "$temp_stderr_output_file"
644e0364 221
ff89ed28
MJ
222 return $ret
223}
224
53cc240b
MJ
225# Checks the difference between the content of the file with path "$1"
226# and the output of the CLI when called on the directory path "$2" with
227# the arguments '-c sink.text.details' and the rest of the arguments to
228# this function.
644e0364
MJ
229#
230# Returns 0 if there's no difference, and 1 if there is, also printing
231# said difference to the standard error.
232bt_diff_details_ctf_single() {
58db335e 233 local expected_stdout_file="$1"
53cc240b
MJ
234 local trace_dir="$2"
235 shift 2
236 local extra_details_args=("$@")
58db335e 237 expected_stderr_file="/dev/null"
644e0364
MJ
238
239 # Compare using the CLI with `sink.text.details`
8b729209
PP
240 bt_diff_cli "$expected_stdout_file" "$expected_stderr_file" "$trace_dir" \
241 "-c" "sink.text.details" "${extra_details_args[@]+${extra_details_args[@]}}"
644e0364
MJ
242}
243
244# Calls bt_diff_details_ctf_single(), except that "$1" is the path to a
245# program which generates the CTF trace to compare to. The program "$1"
246# receives the path to a temporary, empty directory where to write the
247# CTF trace as its first argument.
248bt_diff_details_ctf_gen_single() {
249 local ctf_gen_prog_path="$1"
58db335e 250 local expected_stdout_file="$2"
53cc240b
MJ
251 shift 2
252 local extra_details_args=("$@")
644e0364
MJ
253
254 local temp_trace_dir
255 local ret
256
257 temp_trace_dir="$(mktemp -d)"
258
259 # Run the CTF trace generator program to get a CTF trace
260 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then
261 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
262 rm -rf "$temp_trace_dir"
263 return 1
264 fi
265
266 # Compare using the CLI with `sink.text.details`
8b729209
PP
267 bt_diff_details_ctf_single "$expected_stdout_file" "$temp_trace_dir" \
268 "${extra_details_args[@]+${extra_details_args[@]}}"
644e0364
MJ
269 ret=$?
270 rm -rf "$temp_trace_dir"
271 return $ret
272}
273
274
275### Functions ###
276
277check_coverage() {
278 coverage run "$@"
279}
280
281# Execute a shell command in the appropriate environment to have access to the
282# bt2 Python bindings.
283run_python_bt2() {
e23e08c4
MJ
284 local env_args
285
286 env_args=(
287 "BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1" \
288 "BABELTRACE_PLUGIN_PATH=${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \
289 "LIBBABELTRACE2_PLUGIN_PROVIDER_DIR=${BT_TESTS_PROVIDER_DIR}" \
f3c9a159 290 "BT_TESTS_DATADIR=${BT_TESTS_DATADIR}" \
e23e08c4
MJ
291 "BT_CTF_TRACES_PATH=${BT_CTF_TRACES_PATH}" \
292 "BT_PLUGINS_PATH=${BT_PLUGINS_PATH}" \
293 "PYTHONPATH=${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python"
294 )
644e0364 295
0408934a
SM
296 local main_lib_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs"
297
644e0364 298 # Set the library search path so the python interpreter can load libbabeltrace2
df9db467 299 if [ "$BT_OS_TYPE" = "mingw" ] || [ "$BT_OS_TYPE" = "cygwin" ]; then
e23e08c4 300 env_args+=("PATH=${main_lib_path}:${PATH:-}")
5058d31b 301 elif [ "$BT_OS_TYPE" = "darwin" ]; then
e23e08c4 302 env_args+=("DYLD_LIBRARY_PATH=${main_lib_path}:${DYLD_LIBRARY_PATH:-}")
644e0364 303 else
e23e08c4
MJ
304 env_args+=("LD_LIBRARY_PATH=${main_lib_path}:${LD_LIBRARY_PATH:-}")
305 fi
306
307 # On Windows, an embedded Python interpreter needs a way to locate the path
308 # to it's internal modules, set the prefix from python-config to the
309 # PYTHONHOME variable.
310 if [ "$BT_OS_TYPE" = "mingw" ]; then
311 env_args+=("PYTHONHOME=$($BT_TESTS_PYTHON_CONFIG_BIN --prefix)")
644e0364
MJ
312 fi
313
e23e08c4 314 env "${env_args[@]}" "$@"
644e0364
MJ
315}
316
317# Set the environment and run python tests in the directory.
318#
319# $1 : The directory containing the python test scripts
320# $2 : The pattern to match python test script names (optional)
644e0364
MJ
321run_python_bt2_test() {
322 local test_dir="$1"
a818a617 323 local test_pattern="${2:-'*'}" # optional, if none default to "*"
644e0364
MJ
324
325 local ret
326 local test_runner_args=()
327
328 test_runner_args+=("$test_dir")
329 if [ "x${test_pattern}" != "x" ]; then
330 test_runner_args+=("${test_pattern}")
331 fi
332
333 if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then
334 python_exec="check_coverage"
335 else
336 python_exec="${BT_TESTS_PYTHON_BIN}"
337 fi
338
339 run_python_bt2 \
340 "${python_exec}" \
341 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
a818a617
FD
342 --pattern "$test_pattern" \
343 "$test_dir" \
344
644e0364
MJ
345 ret=$?
346
347 if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then
348 coverage report -m
349 fi
350
351 if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then
352 coverage html
353 fi
354
355 return $ret
356}
This page took 0.05567 seconds and 4 git commands to generate.