3 # Copyright (c) 2019 Michael Jeanson <mjeanson@efficios.com>
4 # Copyright (C) 2019 Philippe Proulx <pproulx@efficios.com>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; under version 2 of the License.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 # This file is meant to be sourced at the start of shell script-based tests.
22 # Error out when encountering an undefined variable
25 # If "readlink -f" is available, get a resolved absolute path to the
26 # tests source dir, otherwise make do with a relative path.
27 scriptdir
="$(dirname "${BASH_SOURCE[0]}")"
28 if readlink
-f "." >/dev
/null
2>&1; then
29 testsdir
=$
(readlink
-f "$scriptdir/..")
31 testsdir
="$scriptdir/.."
34 # The OS on which we are running. See [1] for possible values of 'uname -s'.
35 # We do a bit of translation to ease our life down the road for comparison.
36 # Export it so that called executables can use it.
37 # [1] https://en.wikipedia.org/wiki/Uname#Examples
38 if [ "x${BT_OS_TYPE:-}" = "x" ]; then
39 BT_OS_TYPE
="$(uname -s)"
54 BT_OS_TYPE
="unsupported"
60 # Allow overriding the source and build directories
61 if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then
62 BT_TESTS_SRCDIR
="$testsdir"
64 export BT_TESTS_SRCDIR
66 if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then
67 BT_TESTS_BUILDDIR
="$testsdir"
69 export BT_TESTS_BUILDDIR
71 # By default, it will not source tap.sh. If you to tap output directly from
72 # the test script, define the 'SH_TAP' variable to '1' before sourcing this
74 if [ "x${SH_TAP:-}" = x1
]; then
75 # shellcheck source=./tap/tap.sh
76 .
"${BT_TESTS_SRCDIR}/utils/tap/tap.sh"
79 # Allow overriding the babeltrace2 executables
80 if [ "x${BT_TESTS_BT2_BIN:-}" = "x" ]; then
81 BT_TESTS_BT2_BIN
="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2"
82 if [ "$BT_OS_TYPE" = "mingw" ]; then
83 BT_TESTS_BT2_BIN
="${BT_TESTS_BT2_BIN}.exe"
86 export BT_TESTS_BT2_BIN
88 # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed
89 BT_PLUGINS_PATH
="${BT_TESTS_BUILDDIR}/../src/plugins"
91 # Allow overriding the babeltrace2 plugin path
92 if [ "x${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" = "x" ]; then
93 BT_TESTS_BABELTRACE_PLUGIN_PATH
="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text:${BT_PLUGINS_PATH}/lttng-utils"
96 if [ "x${BT_TESTS_PROVIDER_DIR:-}" = "x" ]; then
97 BT_TESTS_PROVIDER_DIR
="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs"
100 # Allow overriding the babeltrace2 executables
101 if [ "x${BT_TESTS_PYTHONPATH:-}" = "x" ]; then
102 BT_TESTS_PYTHONPATH
="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib"
106 ### External Tools ###
107 if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then
108 BT_TESTS_AWK_BIN
="awk"
110 export BT_TESTS_AWK_BIN
112 if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then
113 BT_TESTS_GREP_BIN
="grep"
115 export BT_TESTS_GREP_BIN
117 if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then
118 BT_TESTS_PYTHON_BIN
="python3"
120 export BT_TESTS_PYTHON_BIN
122 if [ "x${BT_TESTS_PYTHON_CONFIG_BIN:-}" = "x" ]; then
123 BT_TESTS_PYTHON_CONFIG_BIN
="python3-config"
125 export BT_TESTS_PYTHON_CONFIG_BIN
127 if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then
128 BT_TESTS_SED_BIN
="sed"
130 export BT_TESTS_SED_BIN
134 BT_TESTS_DATADIR
="${BT_TESTS_SRCDIR}/data"
135 BT_CTF_TRACES_PATH
="${BT_TESTS_DATADIR}/ctf-traces"
137 # Remove CR characters in file "$1".
140 "$BT_TESTS_SED_BIN" -i 's/\r//g' "$1"
143 # Run the Babeltrace CLI, redirecting stdout and stderr to specified files.
145 # $1: file to redirect stdout to
146 # $2: file to redirect stderr to
147 # remaining args: arguments to pass to the CLI
149 # Return the exit code of the CLI.
152 local stdout_file
="$1"
153 local stderr_file
="$2"
157 echo "Running: $BT_TESTS_BT2_BIN ${args[*]}" >&2
158 run_python_bt2
"$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$stdout_file" 2>"$stderr_file"
161 ### Diff Functions ###
163 # Check the differences between two files (typically some expected output vs
164 # some actual output). If there are differences, print the diff to stderr.
166 # $1: file 1 (expected)
167 # $2: file 2 (actual)
169 # Return 0 if there's no difference, and non-zero if there are.
171 # Note that this function modifies the actual output file ($2) _in-place_ to
172 # remove any \r character.
175 local expected_file
="$1"
176 local actual_file
="$2"
179 # Strip any \r present due to Windows (\n -> \r\n).
180 # "diff --string-trailing-cr" is not used since it is not present on
182 bt_remove_cr
"$actual_file"
184 diff -u "$expected_file" "$actual_file" 1>&2
189 # Checks the difference between:
191 # 1. What the CLI outputs on its standard output when given the arguments
192 # "$@" (excluding the first two arguments).
193 # 2. The file with path "$1".
195 # And the difference between:
197 # 1. What the CLI outputs on its standard error when given the arguments
198 # "$@" (excluding the first two arguments).
199 # 2. The file with path "$2".
201 # Returns 0 if there's no difference, and 1 if there is, also printing
202 # said difference to the standard error.
204 local expected_stdout_file
="$1"
205 local expected_stderr_file
="$2"
209 local temp_stdout_output_file
210 local temp_stderr_output_file
215 temp_stdout_output_file
="$(mktemp -t actual_stdout.XXXXXX)"
216 temp_stderr_output_file
="$(mktemp -t actual_stderr.XXXXXX)"
218 # Run the CLI to get a detailed file.
219 bt_cli
"$temp_stdout_output_file" "$temp_stderr_output_file" "${args[@]}"
221 bt_diff
"$expected_stdout_file" "$temp_stdout_output_file" "${args[@]}"
223 bt_diff
"$expected_stderr_file" "$temp_stderr_output_file" "${args[@]}"
226 if ((ret_stdout
!= 0 || ret_stderr
!= 0)); then
230 rm -f "$temp_stdout_output_file" "$temp_stderr_output_file"
235 # Checks the difference between the content of the file with path "$1"
236 # and the output of the CLI when called on the directory path "$2" with
237 # the arguments '-c sink.text.details' and the rest of the arguments to
240 # Returns 0 if there's no difference, and 1 if there is, also printing
241 # said difference to the standard error.
242 bt_diff_details_ctf_single
() {
243 local expected_stdout_file
="$1"
246 local extra_details_args
=("$@")
247 expected_stderr_file
="/dev/null"
249 # Compare using the CLI with `sink.text.details`
250 bt_diff_cli
"$expected_stdout_file" "$expected_stderr_file" "$trace_dir" \
251 "-c" "sink.text.details" "${extra_details_args[@]+${extra_details_args[@]}}"
254 # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a
255 # program which generates the CTF trace to compare to. The program "$1"
256 # receives the path to a temporary, empty directory where to write the
257 # CTF trace as its first argument.
258 bt_diff_details_ctf_gen_single
() {
259 local ctf_gen_prog_path
="$1"
260 local expected_stdout_file
="$2"
262 local extra_details_args
=("$@")
267 temp_trace_dir
="$(mktemp -d)"
269 # Run the CTF trace generator program to get a CTF trace
270 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev
/null
; then
271 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
272 rm -rf "$temp_trace_dir"
276 # Compare using the CLI with `sink.text.details`
277 bt_diff_details_ctf_single
"$expected_stdout_file" "$temp_trace_dir" \
278 "${extra_details_args[@]+${extra_details_args[@]}}"
280 rm -rf "$temp_trace_dir"
291 # Execute a shell command in the appropriate environment to have access to the
292 # bt2 Python bindings.
297 "BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1" \
298 "BABELTRACE_PLUGIN_PATH=${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \
299 "LIBBABELTRACE2_PLUGIN_PROVIDER_DIR=${BT_TESTS_PROVIDER_DIR}" \
300 "BT_TESTS_DATADIR=${BT_TESTS_DATADIR}" \
301 "BT_CTF_TRACES_PATH=${BT_CTF_TRACES_PATH}" \
302 "BT_PLUGINS_PATH=${BT_PLUGINS_PATH}" \
303 "PYTHONPATH=${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python"
306 local main_lib_path
="${BT_TESTS_BUILDDIR}/../src/lib/.libs"
308 # Set the library search path so the python interpreter can load libbabeltrace2
309 if [ "$BT_OS_TYPE" = "mingw" ] ||
[ "$BT_OS_TYPE" = "cygwin" ]; then
310 env_args
+=("PATH=${main_lib_path}:${PATH:-}")
311 elif [ "$BT_OS_TYPE" = "darwin" ]; then
312 env_args
+=("DYLD_LIBRARY_PATH=${main_lib_path}:${DYLD_LIBRARY_PATH:-}")
314 env_args
+=("LD_LIBRARY_PATH=${main_lib_path}:${LD_LIBRARY_PATH:-}")
317 # On Windows, an embedded Python interpreter needs a way to locate the path
318 # to it's internal modules, set the prefix from python-config to the
319 # PYTHONHOME variable.
320 if [ "$BT_OS_TYPE" = "mingw" ]; then
321 env_args
+=("PYTHONHOME=$($BT_TESTS_PYTHON_CONFIG_BIN --prefix)")
324 env
"${env_args[@]}" "$@"
327 # Set the environment and run python tests in the directory.
329 # $1 : The directory containing the python test scripts
330 # $2 : The pattern to match python test script names (optional)
331 run_python_bt2_test
() {
333 local test_pattern
="${2:-'*'}" # optional, if none default to "*"
336 local test_runner_args
=()
338 test_runner_args
+=("$test_dir")
339 if [ "x${test_pattern}" != "x" ]; then
340 test_runner_args
+=("${test_pattern}")
343 if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then
344 python_exec
="check_coverage"
346 python_exec
="${BT_TESTS_PYTHON_BIN}"
351 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
352 --pattern "$test_pattern" \
357 if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then
361 if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then