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 # Allow overriding the source and build directories
28 if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then
29 BT_TESTS_SRCDIR
=$
(realpath
-s "$scriptdir/..")
31 export BT_TESTS_SRCDIR
33 if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then
34 BT_TESTS_BUILDDIR
=$
(realpath
-s "$scriptdir/..")
36 export BT_TESTS_BUILDDIR
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
41 if [ "x${SH_TAP:-}" = x1
]; then
42 .
"${BT_TESTS_SRCDIR}/utils/tap/tap.sh"
45 # Allow overriding the babeltrace2 executables
46 if [ "x${BT_TESTS_BT2_BIN:-}" = "x" ]; then
47 BT_TESTS_BT2_BIN
=$
(realpath
-s "$BT_TESTS_BUILDDIR/../src/cli/babeltrace2")
49 export BT_TESTS_BT2_BIN
51 if [ "x${BT_TESTS_BT2LOG_BIN:-}" = "x" ]; then
52 BT_TESTS_BT2LOG_BIN
=$
(realpath
-s "$BT_TESTS_BUILDDIR/../src/cli/babeltrace2-log")
54 export BT_TESTS_BT2LOG_BIN
56 # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed
57 BT_PLUGINS_PATH
="${BT_TESTS_BUILDDIR}/../src/plugins"
59 # Allow overriding the babeltrace2 plugin path
60 if [ "x${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" = "x" ]; then
61 BT_TESTS_BABELTRACE_PLUGIN_PATH
="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text"
64 # Allow overriding the babeltrace2 executables
65 if [ "x${BT_TESTS_PYTHONPATH:-}" = "x" ]; then
66 BT_TESTS_PYTHONPATH
="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib"
70 ### External Tools ###
71 if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then
72 BT_TESTS_AWK_BIN
="awk"
74 export BT_TESTS_AWK_BIN
76 if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then
77 BT_TESTS_GREP_BIN
="grep"
79 export BT_TESTS_GREP_BIN
81 if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then
82 BT_TESTS_PYTHON_BIN
="python3"
84 export BT_TESTS_PYTHON_BIN
86 if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then
87 BT_TESTS_SED_BIN
="sed"
89 export BT_TESTS_SED_BIN
93 BT_TESTS_DATADIR
="${BT_TESTS_SRCDIR}/data"
94 BT_CTF_TRACES_PATH
="${BT_TESTS_DATADIR}/ctf-traces"
95 BT_DEBUG_INFO_PATH
="${BT_TESTS_DATADIR}/debug-info"
98 ### Diff Functions ###
100 # Checks the difference between:
102 # 1. What the CLI outputs when given the arguments "$1" (passed to
103 # `xargs`, so they can include quoted arguments).
104 # 2. The file with path "$2".
106 # Returns 0 if there's no difference, and 1 if there is, also printing
107 # said difference to the standard error.
110 local expected_file
="$2"
111 local temp_output_file
115 temp_output_file
="$(mktemp)"
116 temp_diff
="$(mktemp)"
118 # Run the CLI to get a detailed file. Strip any \r present due to
119 # Windows (\n -> \r\n). "diff --string-trailing-cr" is not used since it
120 # is not present on Solaris.
121 echo "$args" |
xargs "$BT_TESTS_BT2_BIN" 2>/dev
/null |
tr -d "\r" > "$temp_output_file"
123 # Compare output with expected output
124 if ! diff "$temp_output_file" "$expected_file" 2>/dev
/null
>"$temp_diff"; then
125 echo "ERROR: for '$args': actual and expected outputs differ:" >&2
130 rm -f "$temp_output_file" "$temp_diff"
135 # Checks the difference between:
137 # 1. What the CLI outputs when given the arguments:
139 # "$1" -c sink.text.details $3
141 # 2. The file with path "$2".
143 # Parameter 3 is optional.
145 # Returns 0 if there's no difference, and 1 if there is, also printing
146 # said difference to the standard error.
147 bt_diff_details_ctf_single
() {
149 local expected_file
="$2"
150 local extra_details_args
="${3:-}"
152 # Compare using the CLI with `sink.text.details`
153 bt_diff_cli
"\"$trace_dir\" -c sink.text.details $extra_details_args" "$expected_file"
156 # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a
157 # program which generates the CTF trace to compare to. The program "$1"
158 # receives the path to a temporary, empty directory where to write the
159 # CTF trace as its first argument.
160 bt_diff_details_ctf_gen_single
() {
161 local ctf_gen_prog_path
="$1"
162 local expected_file
="$2"
163 local extra_details_args
="${3:-}"
168 temp_trace_dir
="$(mktemp -d)"
170 # Run the CTF trace generator program to get a CTF trace
171 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev
/null
; then
172 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
173 rm -rf "$temp_trace_dir"
177 # Compare using the CLI with `sink.text.details`
178 bt_diff_details_ctf_single
"$temp_trace_dir" "$expected_file" "$extra_details_args"
180 rm -rf "$temp_trace_dir"
191 # Execute a shell command in the appropriate environment to have access to the
192 # bt2 Python bindings.
195 local lib_search_path
197 # Set the library search path so the python interpreter can load libbabeltrace2
198 if [ "x${MSYSTEM:-}" != "x" ]; then
199 lib_search_var
="PATH"
200 lib_search_path
="${BT_TESTS_BUILDDIR}/../src/lib/.libs:${PATH:-}"
202 lib_search_var
="LD_LIBRARY_PATH"
203 lib_search_path
="${BT_TESTS_BUILDDIR}/../src/lib/.libs:${LD_LIBRARY_PATH:-}"
205 if [ "x${test_lib_search_path:-}" != "x" ]; then
206 lib_search_path
+=":${test_lib_search_path}"
210 BABELTRACE_PYTHON_BT2_NO_TRACEBACK
=1 \
211 BABELTRACE_PLUGIN_PATH
="${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \
212 BT_CTF_TRACES_PATH
="${BT_CTF_TRACES_PATH}" \
213 BT_PLUGINS_PATH
="${BT_PLUGINS_PATH}" \
214 PYTHONPATH
="${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python" \
215 "${lib_search_var}"="${lib_search_path}" \
219 # Set the environment and run python tests in the directory.
221 # $1 : The directory containing the python test scripts
222 # $2 : The pattern to match python test script names (optional)
223 # $3 : Additionnal library search path (optional)
224 run_python_bt2_test
() {
226 local test_pattern
="${2:-}" # optional
227 local test_lib_search_path
="${3:-}" # optional
230 local test_runner_args
=()
232 test_runner_args
+=("$test_dir")
233 if [ "x${test_pattern}" != "x" ]; then
234 test_runner_args
+=("${test_pattern}")
237 if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then
238 python_exec
="check_coverage"
240 python_exec
="${BT_TESTS_PYTHON_BIN}"
245 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
246 "${test_runner_args[@]}"
249 if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then
253 if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then