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)"
47 BT_OS_TYPE
="unsupported"
53 # Allow overriding the source and build directories
54 if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then
55 BT_TESTS_SRCDIR
="$scriptdir/.."
57 export BT_TESTS_SRCDIR
59 if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then
60 BT_TESTS_BUILDDIR
="$scriptdir/.."
62 export BT_TESTS_BUILDDIR
64 # By default, it will not source tap.sh. If you to tap output directly from
65 # the test script, define the 'SH_TAP' variable to '1' before sourcing this
67 if [ "x${SH_TAP:-}" = x1
]; then
68 # shellcheck source=./tap/tap.sh
69 .
"${BT_TESTS_SRCDIR}/utils/tap/tap.sh"
72 # Allow overriding the babeltrace2 executables
73 if [ "x${BT_TESTS_BT2_BIN:-}" = "x" ]; then
74 BT_TESTS_BT2_BIN
="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2"
75 if [ "$BT_OS_TYPE" = "mingw" ]; then
76 BT_TESTS_BT2_BIN
="${BT_TESTS_BT2_BIN}.exe"
79 export BT_TESTS_BT2_BIN
81 if [ "x${BT_TESTS_BT2LOG_BIN:-}" = "x" ]; then
82 BT_TESTS_BT2LOG_BIN
="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2-log"
83 if [ "$BT_OS_TYPE" = "mingw" ]; then
84 BT_TESTS_BT2LOG_BIN
="${BT_TESTS_BT2LOG_BIN}.exe"
87 export BT_TESTS_BT2LOG_BIN
89 # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed
90 BT_PLUGINS_PATH
="${BT_TESTS_BUILDDIR}/../src/plugins"
92 # Allow overriding the babeltrace2 plugin path
93 if [ "x${BT_TESTS_BABELTRACE_PLUGIN_PATH:-}" = "x" ]; then
94 BT_TESTS_BABELTRACE_PLUGIN_PATH
="${BT_PLUGINS_PATH}/ctf:${BT_PLUGINS_PATH}/utils:${BT_PLUGINS_PATH}/text"
97 if [ "x${BT_TESTS_PROVIDER_DIR:-}" = "x" ]; then
98 BT_TESTS_PROVIDER_DIR
="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs"
101 # Allow overriding the babeltrace2 executables
102 if [ "x${BT_TESTS_PYTHONPATH:-}" = "x" ]; then
103 BT_TESTS_PYTHONPATH
="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib"
107 ### External Tools ###
108 if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then
109 BT_TESTS_AWK_BIN
="awk"
111 export BT_TESTS_AWK_BIN
113 if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then
114 BT_TESTS_GREP_BIN
="grep"
116 export BT_TESTS_GREP_BIN
118 if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then
119 BT_TESTS_PYTHON_BIN
="python3"
121 export BT_TESTS_PYTHON_BIN
123 if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then
124 BT_TESTS_SED_BIN
="sed"
126 export BT_TESTS_SED_BIN
130 BT_TESTS_DATADIR
="${BT_TESTS_SRCDIR}/data"
131 BT_CTF_TRACES_PATH
="${BT_TESTS_DATADIR}/ctf-traces"
132 BT_DEBUG_INFO_PATH
="${BT_TESTS_DATADIR}/debug-info"
135 ### Diff Functions ###
137 # Checks the difference between:
139 # 1. What the CLI outputs on its standard output when given the arguments
140 # "$@" (excluding the first two arguments).
141 # 2. The file with path "$1".
143 # And the difference between:
145 # 1. What the CLI outputs on its standard error when given the arguments
146 # "$@" (excluding the first two arguments).
147 # 2. The file with path "$2".
149 # Returns 0 if there's no difference, and 1 if there is, also printing
150 # said difference to the standard error.
152 local expected_stdout_file
="$1"
153 local expected_stderr_file
="$2"
157 local temp_stdout_output_file
158 local temp_stderr_output_file
162 temp_stdout_output_file
="$(mktemp)"
163 temp_stderr_output_file
="$(mktemp)"
164 temp_diff
="$(mktemp)"
166 # Run the CLI to get a detailed file.
167 run_python_bt2
"$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$temp_stdout_output_file" 2>"$temp_stderr_output_file"
169 # Strip any \r present due to Windows (\n -> \r\n).
170 # "diff --string-trailing-cr" is not used since it is not present on
172 "$BT_TESTS_SED_BIN" -i 's/\r//g' "$temp_stdout_output_file"
173 "$BT_TESTS_SED_BIN" -i 's/\r//g' "$temp_stderr_output_file"
175 # Compare stdout output with expected stdout output
176 if ! diff -u "$temp_stdout_output_file" "$expected_stdout_file" 2>/dev
/null
>"$temp_diff"; then
177 echo "ERROR: for '${args[*]}': actual standard output and expected output differ:" >&2
182 # Compare stderr output with expected stderr output
183 if ! diff -u "$temp_stderr_output_file" "$expected_stderr_file" 2>/dev
/null
>"$temp_diff"; then
184 echo "ERROR: for '${args[*]}': actual standard error and expected error differ:" >&2
189 rm -f "$temp_stdout_output_file" "$temp_stderr_output_file" "$temp_diff"
194 # Checks the difference between the content of the file with path "$1"
195 # and the output of the CLI when called on the directory path "$2" with
196 # the arguments '-c sink.text.details' and the rest of the arguments to
199 # Returns 0 if there's no difference, and 1 if there is, also printing
200 # said difference to the standard error.
201 bt_diff_details_ctf_single
() {
202 local expected_stdout_file
="$1"
205 local extra_details_args
=("$@")
206 expected_stderr_file
="/dev/null"
208 # Compare using the CLI with `sink.text.details`
209 bt_diff_cli
"$expected_stdout_file" "$expected_stderr_file" "$trace_dir" "-c" "sink.text.details" "${extra_details_args[@]}"
212 # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a
213 # program which generates the CTF trace to compare to. The program "$1"
214 # receives the path to a temporary, empty directory where to write the
215 # CTF trace as its first argument.
216 bt_diff_details_ctf_gen_single
() {
217 local ctf_gen_prog_path
="$1"
218 local expected_stdout_file
="$2"
220 local extra_details_args
=("$@")
225 temp_trace_dir
="$(mktemp -d)"
227 # Run the CTF trace generator program to get a CTF trace
228 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev
/null
; then
229 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
230 rm -rf "$temp_trace_dir"
234 # Compare using the CLI with `sink.text.details`
235 bt_diff_details_ctf_single
"$expected_stdout_file" "$temp_trace_dir" "${extra_details_args[@]}"
237 rm -rf "$temp_trace_dir"
248 # Execute a shell command in the appropriate environment to have access to the
249 # bt2 Python bindings.
252 local lib_search_path
254 local main_lib_path
="${BT_TESTS_BUILDDIR}/../src/lib/.libs"
256 # Set the library search path so the python interpreter can load libbabeltrace2
257 if [ "$BT_OS_TYPE" = "mingw" ] ||
[ "$BT_OS_TYPE" = "cygwin" ]; then
258 lib_search_var
="PATH"
259 lib_search_path
="${main_lib_path}:${PATH:-}"
260 elif [ "$BT_OS_TYPE" = "darwin" ]; then
261 lib_search_var
="DYLD_LIBRARY_PATH"
262 lib_search_path
="${main_lib_path}:${DYLD_LIBRARY_PATH:-}"
264 lib_search_var
="LD_LIBRARY_PATH"
265 lib_search_path
="${main_lib_path}:${LD_LIBRARY_PATH:-}"
269 BABELTRACE_PYTHON_BT2_NO_TRACEBACK
=1 \
270 BABELTRACE_PLUGIN_PATH
="${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \
271 LIBBABELTRACE2_PLUGIN_PROVIDER_DIR
=${BT_TESTS_PROVIDER_DIR} \
272 BT_CTF_TRACES_PATH
="${BT_CTF_TRACES_PATH}" \
273 BT_PLUGINS_PATH
="${BT_PLUGINS_PATH}" \
274 PYTHONPATH
="${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python" \
275 "${lib_search_var}"="${lib_search_path}" \
279 # Set the environment and run python tests in the directory.
281 # $1 : The directory containing the python test scripts
282 # $2 : The pattern to match python test script names (optional)
283 run_python_bt2_test
() {
285 local test_pattern
="${2:-'*'}" # optional, if none default to "*"
288 local test_runner_args
=()
290 test_runner_args
+=("$test_dir")
291 if [ "x${test_pattern}" != "x" ]; then
292 test_runner_args
+=("${test_pattern}")
295 if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then
296 python_exec
="check_coverage"
298 python_exec
="${BT_TESTS_PYTHON_BIN}"
303 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
304 --pattern "$test_pattern" \
309 if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then
313 if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then