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 scriptdir
="$(dirname "${BASH_SOURCE[0]}")"
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
31 if [ "x${BT_OS_TYPE:-}" = "x" ]; then
32 BT_OS_TYPE
="$(uname -s)"
44 BT_OS_TYPE
="unsupported"
50 # Allow overriding the source and build directories
51 if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then
52 BT_TESTS_SRCDIR
="$scriptdir/.."
54 export BT_TESTS_SRCDIR
56 if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then
57 BT_TESTS_BUILDDIR
="$scriptdir/.."
59 export BT_TESTS_BUILDDIR
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
64 if [ "x${SH_TAP:-}" = x1
]; then
65 # shellcheck source=./tap/tap.sh
66 .
"${BT_TESTS_SRCDIR}/utils/tap/tap.sh"
69 # Allow overriding the babeltrace2 executables
70 if [ "x${BT_TESTS_BT2_BIN:-}" = "x" ]; then
71 BT_TESTS_BT2_BIN
="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2"
72 if [ "$BT_OS_TYPE" = "mingw" ]; then
73 BT_TESTS_BT2_BIN
="${BT_TESTS_BT2_BIN}.exe"
76 export BT_TESTS_BT2_BIN
78 if [ "x${BT_TESTS_BT2LOG_BIN:-}" = "x" ]; then
79 BT_TESTS_BT2LOG_BIN
="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2-log"
80 if [ "$BT_OS_TYPE" = "mingw" ]; then
81 BT_TESTS_BT2LOG_BIN
="${BT_TESTS_BT2LOG_BIN}.exe"
84 export BT_TESTS_BT2LOG_BIN
86 # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed
87 BT_PLUGINS_PATH
="${BT_TESTS_BUILDDIR}/../src/plugins"
89 # Allow overriding the babeltrace2 plugin path
90 if [ "x${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" = "x" ]; then
91 BT_TESTS_BABELTRACE_PLUGIN_PATH
="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text"
94 if [ "x${BT_TESTS_PROVIDER_DIR:-}" = "x" ]; then
95 BT_TESTS_PROVIDER_DIR
="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs"
98 # Allow overriding the babeltrace2 executables
99 if [ "x${BT_TESTS_PYTHONPATH:-}" = "x" ]; then
100 BT_TESTS_PYTHONPATH
="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib"
104 ### External Tools ###
105 if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then
106 BT_TESTS_AWK_BIN
="awk"
108 export BT_TESTS_AWK_BIN
110 if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then
111 BT_TESTS_GREP_BIN
="grep"
113 export BT_TESTS_GREP_BIN
115 if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then
116 BT_TESTS_PYTHON_BIN
="python3"
118 export BT_TESTS_PYTHON_BIN
120 if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then
121 BT_TESTS_SED_BIN
="sed"
123 export BT_TESTS_SED_BIN
127 BT_TESTS_DATADIR
="${BT_TESTS_SRCDIR}/data"
128 BT_CTF_TRACES_PATH
="${BT_TESTS_DATADIR}/ctf-traces"
129 BT_DEBUG_INFO_PATH
="${BT_TESTS_DATADIR}/debug-info"
132 ### Diff Functions ###
134 # Checks the difference between the content of the file with path "$1"
135 # and the output of the CLI when called with the rest of arguments
138 # Returns 0 if there's no difference, and 1 if there is, also printing
139 # said difference to the standard error.
141 local expected_file
="$1"
145 local temp_output_file
149 temp_output_file
="$(mktemp)"
150 temp_diff
="$(mktemp)"
152 # Run the CLI to get a detailed file. Strip any \r present due to
153 # Windows (\n -> \r\n). "diff --string-trailing-cr" is not used since it
154 # is not present on Solaris.
155 run_python_bt2
"$BT_TESTS_BT2_BIN" "${args[@]}" |
tr -d "\r" > "$temp_output_file"
157 # Compare output with expected output
158 if ! diff -u "$temp_output_file" "$expected_file" 2>/dev
/null
>"$temp_diff"; then
159 echo "ERROR: for '${args[*]}': actual and expected outputs differ:" >&2
164 rm -f "$temp_output_file" "$temp_diff"
169 # Checks the difference between the content of the file with path "$1"
170 # and the output of the CLI when called on the directory path "$2" with
171 # the arguments '-c sink.text.details' and the rest of the arguments to
174 # Returns 0 if there's no difference, and 1 if there is, also printing
175 # said difference to the standard error.
176 bt_diff_details_ctf_single
() {
177 local expected_file
="$1"
180 local extra_details_args
=("$@")
182 # Compare using the CLI with `sink.text.details`
183 bt_diff_cli
"$expected_file" "$trace_dir" "-c" "sink.text.details" "${extra_details_args[@]}"
186 # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a
187 # program which generates the CTF trace to compare to. The program "$1"
188 # receives the path to a temporary, empty directory where to write the
189 # CTF trace as its first argument.
190 bt_diff_details_ctf_gen_single
() {
191 local ctf_gen_prog_path
="$1"
192 local expected_file
="$2"
194 local extra_details_args
=("$@")
199 temp_trace_dir
="$(mktemp -d)"
201 # Run the CTF trace generator program to get a CTF trace
202 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev
/null
; then
203 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
204 rm -rf "$temp_trace_dir"
208 # Compare using the CLI with `sink.text.details`
209 bt_diff_details_ctf_single
"$expected_file" "$temp_trace_dir" "${extra_details_args[@]}"
211 rm -rf "$temp_trace_dir"
222 # Execute a shell command in the appropriate environment to have access to the
223 # bt2 Python bindings.
226 local lib_search_path
228 local main_lib_path
="${BT_TESTS_BUILDDIR}/../src/lib/.libs"
230 # Set the library search path so the python interpreter can load libbabeltrace2
231 if [ "$BT_OS_TYPE" = "mingw" ]; then
232 lib_search_var
="PATH"
233 lib_search_path
="${main_lib_path}:${PATH:-}"
234 elif [ "$BT_OS_TYPE" = "darwin" ]; then
235 lib_search_var
="DYLD_LIBRARY_PATH"
236 lib_search_path
="${main_lib_path}:${DYLD_LIBRARY_PATH:-}"
238 lib_search_var
="LD_LIBRARY_PATH"
239 lib_search_path
="${main_lib_path}:${LD_LIBRARY_PATH:-}"
243 BABELTRACE_PYTHON_BT2_NO_TRACEBACK
=1 \
244 BABELTRACE_PLUGIN_PATH
="${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \
245 LIBBABELTRACE2_PLUGIN_PROVIDER_DIR
=${BT_TESTS_PROVIDER_DIR} \
246 BT_CTF_TRACES_PATH
="${BT_CTF_TRACES_PATH}" \
247 BT_PLUGINS_PATH
="${BT_PLUGINS_PATH}" \
248 PYTHONPATH
="${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python" \
249 "${lib_search_var}"="${lib_search_path}" \
253 # Set the environment and run python tests in the directory.
255 # $1 : The directory containing the python test scripts
256 # $2 : The pattern to match python test script names (optional)
257 run_python_bt2_test
() {
259 local test_pattern
="${2:-'*'}" # optional, if none default to "*"
262 local test_runner_args
=()
264 test_runner_args
+=("$test_dir")
265 if [ "x${test_pattern}" != "x" ]; then
266 test_runner_args
+=("${test_pattern}")
269 if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then
270 python_exec
="check_coverage"
272 python_exec
="${BT_TESTS_PYTHON_BIN}"
277 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
278 --pattern "$test_pattern" \
283 if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then
287 if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then