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