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
="$scriptdir/.."
31 export BT_TESTS_SRCDIR
33 if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then
34 BT_TESTS_BUILDDIR
="$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
="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2"
48 if [ "x${MSYSTEM:-}" != "x" ]; then
49 BT_TESTS_BT2_BIN
="${BT_TESTS_BT2_BIN}.exe"
52 export BT_TESTS_BT2_BIN
54 if [ "x${BT_TESTS_BT2LOG_BIN:-}" = "x" ]; then
55 BT_TESTS_BT2LOG_BIN
="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2-log"
56 if [ "x${MSYSTEM:-}" != "x" ]; then
57 BT_TESTS_BT2LOG_BIN
="${BT_TESTS_BT2LOG_BIN}.exe"
60 export BT_TESTS_BT2LOG_BIN
62 # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed
63 BT_PLUGINS_PATH
="${BT_TESTS_BUILDDIR}/../src/plugins"
65 # Allow overriding the babeltrace2 plugin path
66 if [ "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"
70 # Allow overriding the babeltrace2 executables
71 if [ "x${BT_TESTS_PYTHONPATH:-}" = "x" ]; then
72 BT_TESTS_PYTHONPATH
="${BT_TESTS_BUILDDIR}/../src/bindings/python/bt2/build/build_lib"
76 ### External Tools ###
77 if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then
78 BT_TESTS_AWK_BIN
="awk"
80 export BT_TESTS_AWK_BIN
82 if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then
83 BT_TESTS_GREP_BIN
="grep"
85 export BT_TESTS_GREP_BIN
87 if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then
88 BT_TESTS_PYTHON_BIN
="python3"
90 export BT_TESTS_PYTHON_BIN
92 if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then
93 BT_TESTS_SED_BIN
="sed"
95 export BT_TESTS_SED_BIN
99 BT_TESTS_DATADIR
="${BT_TESTS_SRCDIR}/data"
100 BT_CTF_TRACES_PATH
="${BT_TESTS_DATADIR}/ctf-traces"
101 BT_DEBUG_INFO_PATH
="${BT_TESTS_DATADIR}/debug-info"
104 ### Diff Functions ###
106 # Checks the difference between:
108 # 1. What the CLI outputs when given the arguments "$1" (passed to
109 # `xargs`, so they can include quoted arguments).
110 # 2. The file with path "$2".
112 # Returns 0 if there's no difference, and 1 if there is, also printing
113 # said difference to the standard error.
116 local expected_file
="$2"
117 local temp_output_file
121 temp_output_file
="$(mktemp)"
122 temp_diff
="$(mktemp)"
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.
127 echo "$args" |
xargs "$BT_TESTS_BT2_BIN" 2>/dev
/null |
tr -d "\r" > "$temp_output_file"
129 # Compare output with expected output
130 if ! diff -u "$temp_output_file" "$expected_file" 2>/dev
/null
>"$temp_diff"; then
131 echo "ERROR: for '$args': actual and expected outputs differ:" >&2
136 rm -f "$temp_output_file" "$temp_diff"
141 # Checks the difference between:
143 # 1. What the CLI outputs when given the arguments:
145 # "$1" -c sink.text.details $3
147 # 2. The file with path "$2".
149 # Parameter 3 is optional.
151 # Returns 0 if there's no difference, and 1 if there is, also printing
152 # said difference to the standard error.
153 bt_diff_details_ctf_single
() {
155 local expected_file
="$2"
156 local extra_details_args
="${3:-}"
158 # Compare using the CLI with `sink.text.details`
159 bt_diff_cli
"\"$trace_dir\" -c sink.text.details $extra_details_args" "$expected_file"
162 # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a
163 # program which generates the CTF trace to compare to. The program "$1"
164 # receives the path to a temporary, empty directory where to write the
165 # CTF trace as its first argument.
166 bt_diff_details_ctf_gen_single
() {
167 local ctf_gen_prog_path
="$1"
168 local expected_file
="$2"
169 local extra_details_args
="${3:-}"
174 temp_trace_dir
="$(mktemp -d)"
176 # Run the CTF trace generator program to get a CTF trace
177 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev
/null
; then
178 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
179 rm -rf "$temp_trace_dir"
183 # Compare using the CLI with `sink.text.details`
184 bt_diff_details_ctf_single
"$temp_trace_dir" "$expected_file" "$extra_details_args"
186 rm -rf "$temp_trace_dir"
197 # Execute a shell command in the appropriate environment to have access to the
198 # bt2 Python bindings.
201 local lib_search_path
203 local python_provider_path
="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs"
204 local main_lib_path
="${BT_TESTS_BUILDDIR}/../src/lib/.libs"
206 # Set the library search path so the python interpreter can load libbabeltrace2
207 if [ "x${MSYSTEM:-}" != "x" ]; then
208 lib_search_var
="PATH"
209 lib_search_path
="${python_provider_path}:${main_lib_path}:${PATH:-}"
211 lib_search_var
="LD_LIBRARY_PATH"
212 lib_search_path
="${python_provider_path}:${main_lib_path}:${LD_LIBRARY_PATH:-}"
216 BABELTRACE_PYTHON_BT2_NO_TRACEBACK
=1 \
217 BABELTRACE_PLUGIN_PATH
="${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \
218 BT_CTF_TRACES_PATH
="${BT_CTF_TRACES_PATH}" \
219 BT_PLUGINS_PATH
="${BT_PLUGINS_PATH}" \
220 PYTHONPATH
="${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python" \
221 "${lib_search_var}"="${lib_search_path}" \
225 # Set the environment and run python tests in the directory.
227 # $1 : The directory containing the python test scripts
228 # $2 : The pattern to match python test script names (optional)
229 run_python_bt2_test
() {
231 local test_pattern
="${2:-'*'}" # optional, if none default to "*"
234 local test_runner_args
=()
236 test_runner_args
+=("$test_dir")
237 if [ "x${test_pattern}" != "x" ]; then
238 test_runner_args
+=("${test_pattern}")
241 if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then
242 python_exec
="check_coverage"
244 python_exec
="${BT_TESTS_PYTHON_BIN}"
249 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
250 --pattern "$test_pattern" \
255 if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then
259 if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then