doc: Use DYLD_LIBRARY_PATH on macOSX
[babeltrace.git] / tests / utils / utils.sh
CommitLineData
644e0364
MJ
1#!/bin/bash
2
3# Copyright (c) 2019 Michael Jeanson <mjeanson@efficios.com>
4# Copyright (C) 2019 Philippe Proulx <pproulx@efficios.com>
5#
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.
9#
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.
14#
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.
18
19# This file is meant to be sourced at the start of shell script-based tests.
20
21
22# Error out when encountering an undefined variable
23set -u
24
25scriptdir="$(dirname "${BASH_SOURCE[0]}")"
26
27# Allow overriding the source and build directories
28if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then
4881a20e 29 BT_TESTS_SRCDIR="$scriptdir/.."
644e0364
MJ
30fi
31export BT_TESTS_SRCDIR
32
33if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then
4881a20e 34 BT_TESTS_BUILDDIR="$scriptdir/.."
644e0364
MJ
35fi
36export BT_TESTS_BUILDDIR
37
38# By default, it will not source tap.sh. If you to tap output directly from
39# the test script, define the 'SH_TAP' variable to '1' before sourcing this
40# script.
41if [ "x${SH_TAP:-}" = x1 ]; then
42 . "${BT_TESTS_SRCDIR}/utils/tap/tap.sh"
43fi
44
45# Allow overriding the babeltrace2 executables
46if [ "x${BT_TESTS_BT2_BIN:-}" = "x" ]; then
4881a20e 47 BT_TESTS_BT2_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2"
27e2c58d
JR
48 if [ "x${MSYSTEM:-}" != "x" ]; then
49 BT_TESTS_BT2_BIN="${BT_TESTS_BT2_BIN}.exe"
50 fi
644e0364
MJ
51fi
52export BT_TESTS_BT2_BIN
53
54if [ "x${BT_TESTS_BT2LOG_BIN:-}" = "x" ]; then
4881a20e 55 BT_TESTS_BT2LOG_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2-log"
27e2c58d
JR
56 if [ "x${MSYSTEM:-}" != "x" ]; then
57 BT_TESTS_BT2LOG_BIN="${BT_TESTS_BT2LOG_BIN}.exe"
58 fi
644e0364
MJ
59fi
60export BT_TESTS_BT2LOG_BIN
61
62# TODO: Remove when bindings/python/bt2/test_plugin.py is fixed
63BT_PLUGINS_PATH="${BT_TESTS_BUILDDIR}/../src/plugins"
64
65# Allow overriding the babeltrace2 plugin path
66if [ "x${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" = "x" ]; then
67 BT_TESTS_BABELTRACE_PLUGIN_PATH="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text"
68fi
69
70# Allow overriding the babeltrace2 executables
71if [ "x${BT_TESTS_PYTHONPATH:-}" = "x" ]; then
72 BT_TESTS_PYTHONPATH="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib"
73fi
74
75
76### External Tools ###
77if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then
78 BT_TESTS_AWK_BIN="awk"
79fi
80export BT_TESTS_AWK_BIN
81
82if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then
83 BT_TESTS_GREP_BIN="grep"
84fi
85export BT_TESTS_GREP_BIN
86
87if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then
88 BT_TESTS_PYTHON_BIN="python3"
89fi
90export BT_TESTS_PYTHON_BIN
91
92if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then
93 BT_TESTS_SED_BIN="sed"
94fi
95export BT_TESTS_SED_BIN
96
97
98# Data files path
99BT_TESTS_DATADIR="${BT_TESTS_SRCDIR}/data"
100BT_CTF_TRACES_PATH="${BT_TESTS_DATADIR}/ctf-traces"
101BT_DEBUG_INFO_PATH="${BT_TESTS_DATADIR}/debug-info"
102
103
104### Diff Functions ###
105
53cc240b
MJ
106# Checks the difference between the content of the file with path "$1"
107# and the output of the CLI when called with the rest of arguments
108# to this function.
644e0364
MJ
109#
110# Returns 0 if there's no difference, and 1 if there is, also printing
111# said difference to the standard error.
112bt_diff_cli() {
53cc240b
MJ
113 local expected_file="$1"
114 shift 1
115 local args=("$@")
116
644e0364
MJ
117 local temp_output_file
118 local temp_diff
119 local ret=0
120
121 temp_output_file="$(mktemp)"
122 temp_diff="$(mktemp)"
123
124 # Run the CLI to get a detailed file. Strip any \r present due to
125 # Windows (\n -> \r\n). "diff --string-trailing-cr" is not used since it
126 # is not present on Solaris.
53cc240b 127 "$BT_TESTS_BT2_BIN" "${args[@]}" | tr -d "\r" > "$temp_output_file"
644e0364
MJ
128
129 # Compare output with expected output
1af7795b 130 if ! diff -u "$temp_output_file" "$expected_file" 2>/dev/null >"$temp_diff"; then
53cc240b 131 echo "ERROR: for '${args[*]}': actual and expected outputs differ:" >&2
644e0364
MJ
132 cat "$temp_diff" >&2
133 ret=1
134 fi
135
136 rm -f "$temp_output_file" "$temp_diff"
137
138 return $ret
139}
140
53cc240b
MJ
141# Checks the difference between the content of the file with path "$1"
142# and the output of the CLI when called on the directory path "$2" with
143# the arguments '-c sink.text.details' and the rest of the arguments to
144# this function.
644e0364
MJ
145#
146# Returns 0 if there's no difference, and 1 if there is, also printing
147# said difference to the standard error.
148bt_diff_details_ctf_single() {
53cc240b
MJ
149 local expected_file="$1"
150 local trace_dir="$2"
151 shift 2
152 local extra_details_args=("$@")
644e0364
MJ
153
154 # Compare using the CLI with `sink.text.details`
53cc240b 155 bt_diff_cli "$expected_file" "$trace_dir" "-c" "sink.text.details" "${extra_details_args[@]}"
644e0364
MJ
156}
157
158# Calls bt_diff_details_ctf_single(), except that "$1" is the path to a
159# program which generates the CTF trace to compare to. The program "$1"
160# receives the path to a temporary, empty directory where to write the
161# CTF trace as its first argument.
162bt_diff_details_ctf_gen_single() {
163 local ctf_gen_prog_path="$1"
164 local expected_file="$2"
53cc240b
MJ
165 shift 2
166 local extra_details_args=("$@")
644e0364
MJ
167
168 local temp_trace_dir
169 local ret
170
171 temp_trace_dir="$(mktemp -d)"
172
173 # Run the CTF trace generator program to get a CTF trace
174 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then
175 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
176 rm -rf "$temp_trace_dir"
177 return 1
178 fi
179
180 # Compare using the CLI with `sink.text.details`
53cc240b 181 bt_diff_details_ctf_single "$expected_file" "$temp_trace_dir" "${extra_details_args[@]}"
644e0364
MJ
182 ret=$?
183 rm -rf "$temp_trace_dir"
184 return $ret
185}
186
187
188### Functions ###
189
190check_coverage() {
191 coverage run "$@"
192}
193
194# Execute a shell command in the appropriate environment to have access to the
195# bt2 Python bindings.
196run_python_bt2() {
197 local lib_search_var
198 local lib_search_path
199
0408934a
SM
200 local python_provider_path="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs"
201 local main_lib_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs"
202
644e0364
MJ
203 # Set the library search path so the python interpreter can load libbabeltrace2
204 if [ "x${MSYSTEM:-}" != "x" ]; then
205 lib_search_var="PATH"
0408934a 206 lib_search_path="${python_provider_path}:${main_lib_path}:${PATH:-}"
644e0364
MJ
207 else
208 lib_search_var="LD_LIBRARY_PATH"
0408934a 209 lib_search_path="${python_provider_path}:${main_lib_path}:${LD_LIBRARY_PATH:-}"
644e0364
MJ
210 fi
211
212 env \
213 BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1 \
214 BABELTRACE_PLUGIN_PATH="${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \
215 BT_CTF_TRACES_PATH="${BT_CTF_TRACES_PATH}" \
216 BT_PLUGINS_PATH="${BT_PLUGINS_PATH}" \
217 PYTHONPATH="${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python" \
218 "${lib_search_var}"="${lib_search_path}" \
219 "$@"
220}
221
222# Set the environment and run python tests in the directory.
223#
224# $1 : The directory containing the python test scripts
225# $2 : The pattern to match python test script names (optional)
644e0364
MJ
226run_python_bt2_test() {
227 local test_dir="$1"
a818a617 228 local test_pattern="${2:-'*'}" # optional, if none default to "*"
644e0364
MJ
229
230 local ret
231 local test_runner_args=()
232
233 test_runner_args+=("$test_dir")
234 if [ "x${test_pattern}" != "x" ]; then
235 test_runner_args+=("${test_pattern}")
236 fi
237
238 if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then
239 python_exec="check_coverage"
240 else
241 python_exec="${BT_TESTS_PYTHON_BIN}"
242 fi
243
244 run_python_bt2 \
245 "${python_exec}" \
246 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
a818a617
FD
247 --pattern "$test_pattern" \
248 "$test_dir" \
249
644e0364
MJ
250 ret=$?
251
252 if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then
253 coverage report -m
254 fi
255
256 if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then
257 coverage html
258 fi
259
260 return $ret
261}
This page took 0.033462 seconds and 4 git commands to generate.