tests: 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
5058d31b
JR
27# The OS on which we are running. See [1] for possible values of 'uname -s'.
28# We do a bit of translation to ease our life down the road for comparison.
29# Export it so that called executables can use it.
30# [1] https://en.wikipedia.org/wiki/Uname#Examples
31if [ "x${BT_OS_TYPE:-}" = "x" ]; then
32 BT_OS_TYPE="$(uname -s)"
33 case "$BT_OS_TYPE" in
34 MINGW*)
35 BT_OS_TYPE="mingw"
36 ;;
37 Darwin)
38 BT_OS_TYPE="darwin"
39 ;;
40 Linux)
41 BT_OS_TYPE="linux"
42 ;;
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
4881a20e 52 BT_TESTS_SRCDIR="$scriptdir/.."
644e0364
MJ
53fi
54export BT_TESTS_SRCDIR
55
56if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then
4881a20e 57 BT_TESTS_BUILDDIR="$scriptdir/.."
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
65 . "${BT_TESTS_SRCDIR}/utils/tap/tap.sh"
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"
5058d31b 71 if [ "$BT_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
77if [ "x${BT_TESTS_BT2LOG_BIN:-}" = "x" ]; then
4881a20e 78 BT_TESTS_BT2LOG_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2-log"
5058d31b 79 if [ "$BT_OS_TYPE" = "mingw" ]; then
27e2c58d
JR
80 BT_TESTS_BT2LOG_BIN="${BT_TESTS_BT2LOG_BIN}.exe"
81 fi
644e0364
MJ
82fi
83export BT_TESTS_BT2LOG_BIN
84
85# TODO: Remove when bindings/python/bt2/test_plugin.py is fixed
86BT_PLUGINS_PATH="${BT_TESTS_BUILDDIR}/../src/plugins"
87
88# Allow overriding the babeltrace2 plugin path
89if [ "x${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" = "x" ]; then
90 BT_TESTS_BABELTRACE_PLUGIN_PATH="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text"
91fi
92
93# Allow overriding the babeltrace2 executables
94if [ "x${BT_TESTS_PYTHONPATH:-}" = "x" ]; then
95 BT_TESTS_PYTHONPATH="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib"
96fi
97
98
99### External Tools ###
100if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then
101 BT_TESTS_AWK_BIN="awk"
102fi
103export BT_TESTS_AWK_BIN
104
105if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then
106 BT_TESTS_GREP_BIN="grep"
107fi
108export BT_TESTS_GREP_BIN
109
110if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then
111 BT_TESTS_PYTHON_BIN="python3"
112fi
113export BT_TESTS_PYTHON_BIN
114
115if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then
116 BT_TESTS_SED_BIN="sed"
117fi
118export BT_TESTS_SED_BIN
119
120
121# Data files path
122BT_TESTS_DATADIR="${BT_TESTS_SRCDIR}/data"
123BT_CTF_TRACES_PATH="${BT_TESTS_DATADIR}/ctf-traces"
124BT_DEBUG_INFO_PATH="${BT_TESTS_DATADIR}/debug-info"
125
126
127### Diff Functions ###
128
53cc240b
MJ
129# Checks the difference between the content of the file with path "$1"
130# and the output of the CLI when called with the rest of arguments
131# to this function.
644e0364
MJ
132#
133# Returns 0 if there's no difference, and 1 if there is, also printing
134# said difference to the standard error.
135bt_diff_cli() {
53cc240b
MJ
136 local expected_file="$1"
137 shift 1
138 local args=("$@")
139
644e0364
MJ
140 local temp_output_file
141 local temp_diff
142 local ret=0
143
144 temp_output_file="$(mktemp)"
145 temp_diff="$(mktemp)"
146
147 # Run the CLI to get a detailed file. Strip any \r present due to
148 # Windows (\n -> \r\n). "diff --string-trailing-cr" is not used since it
149 # is not present on Solaris.
53cc240b 150 "$BT_TESTS_BT2_BIN" "${args[@]}" | tr -d "\r" > "$temp_output_file"
644e0364
MJ
151
152 # Compare output with expected output
1af7795b 153 if ! diff -u "$temp_output_file" "$expected_file" 2>/dev/null >"$temp_diff"; then
53cc240b 154 echo "ERROR: for '${args[*]}': actual and expected outputs differ:" >&2
644e0364
MJ
155 cat "$temp_diff" >&2
156 ret=1
157 fi
158
159 rm -f "$temp_output_file" "$temp_diff"
160
161 return $ret
162}
163
53cc240b
MJ
164# Checks the difference between the content of the file with path "$1"
165# and the output of the CLI when called on the directory path "$2" with
166# the arguments '-c sink.text.details' and the rest of the arguments to
167# this function.
644e0364
MJ
168#
169# Returns 0 if there's no difference, and 1 if there is, also printing
170# said difference to the standard error.
171bt_diff_details_ctf_single() {
53cc240b
MJ
172 local expected_file="$1"
173 local trace_dir="$2"
174 shift 2
175 local extra_details_args=("$@")
644e0364
MJ
176
177 # Compare using the CLI with `sink.text.details`
53cc240b 178 bt_diff_cli "$expected_file" "$trace_dir" "-c" "sink.text.details" "${extra_details_args[@]}"
644e0364
MJ
179}
180
181# Calls bt_diff_details_ctf_single(), except that "$1" is the path to a
182# program which generates the CTF trace to compare to. The program "$1"
183# receives the path to a temporary, empty directory where to write the
184# CTF trace as its first argument.
185bt_diff_details_ctf_gen_single() {
186 local ctf_gen_prog_path="$1"
187 local expected_file="$2"
53cc240b
MJ
188 shift 2
189 local extra_details_args=("$@")
644e0364
MJ
190
191 local temp_trace_dir
192 local ret
193
194 temp_trace_dir="$(mktemp -d)"
195
196 # Run the CTF trace generator program to get a CTF trace
197 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then
198 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
199 rm -rf "$temp_trace_dir"
200 return 1
201 fi
202
203 # Compare using the CLI with `sink.text.details`
53cc240b 204 bt_diff_details_ctf_single "$expected_file" "$temp_trace_dir" "${extra_details_args[@]}"
644e0364
MJ
205 ret=$?
206 rm -rf "$temp_trace_dir"
207 return $ret
208}
209
210
211### Functions ###
212
213check_coverage() {
214 coverage run "$@"
215}
216
217# Execute a shell command in the appropriate environment to have access to the
218# bt2 Python bindings.
219run_python_bt2() {
220 local lib_search_var
221 local lib_search_path
222
0408934a
SM
223 local python_provider_path="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs"
224 local main_lib_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs"
225
644e0364 226 # Set the library search path so the python interpreter can load libbabeltrace2
5058d31b 227 if [ "$BT_OS_TYPE" = "mingw" ]; then
644e0364 228 lib_search_var="PATH"
0408934a 229 lib_search_path="${python_provider_path}:${main_lib_path}:${PATH:-}"
5058d31b
JR
230 elif [ "$BT_OS_TYPE" = "darwin" ]; then
231 lib_search_var="DYLD_LIBRARY_PATH"
232 lib_search_path="${python_provider_path}:${main_lib_path}:${DYLD_LIBRARY_PATH:-}"
644e0364
MJ
233 else
234 lib_search_var="LD_LIBRARY_PATH"
0408934a 235 lib_search_path="${python_provider_path}:${main_lib_path}:${LD_LIBRARY_PATH:-}"
644e0364
MJ
236 fi
237
238 env \
239 BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1 \
240 BABELTRACE_PLUGIN_PATH="${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \
241 BT_CTF_TRACES_PATH="${BT_CTF_TRACES_PATH}" \
242 BT_PLUGINS_PATH="${BT_PLUGINS_PATH}" \
243 PYTHONPATH="${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python" \
244 "${lib_search_var}"="${lib_search_path}" \
245 "$@"
246}
247
248# Set the environment and run python tests in the directory.
249#
250# $1 : The directory containing the python test scripts
251# $2 : The pattern to match python test script names (optional)
644e0364
MJ
252run_python_bt2_test() {
253 local test_dir="$1"
a818a617 254 local test_pattern="${2:-'*'}" # optional, if none default to "*"
644e0364
MJ
255
256 local ret
257 local test_runner_args=()
258
259 test_runner_args+=("$test_dir")
260 if [ "x${test_pattern}" != "x" ]; then
261 test_runner_args+=("${test_pattern}")
262 fi
263
264 if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then
265 python_exec="check_coverage"
266 else
267 python_exec="${BT_TESTS_PYTHON_BIN}"
268 fi
269
270 run_python_bt2 \
271 "${python_exec}" \
272 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
a818a617
FD
273 --pattern "$test_pattern" \
274 "$test_dir" \
275
644e0364
MJ
276 ret=$?
277
278 if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then
279 coverage report -m
280 fi
281
282 if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then
283 coverage html
284 fi
285
286 return $ret
287}
This page took 0.034724 seconds and 4 git commands to generate.