84c3c52bdcb62d3f60f0bfa3b091b14131431f4d
[babeltrace.git] / tests / utils / utils.sh
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
23 set -u
24
25 scriptdir="$(dirname "${BASH_SOURCE[0]}")"
26
27 # Allow overriding the source and build directories
28 if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then
29 BT_TESTS_SRCDIR="$scriptdir/.."
30 fi
31 export BT_TESTS_SRCDIR
32
33 if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then
34 BT_TESTS_BUILDDIR="$scriptdir/.."
35 fi
36 export BT_TESTS_BUILDDIR
37
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
40 # script.
41 if [ "x${SH_TAP:-}" = x1 ]; then
42 . "${BT_TESTS_SRCDIR}/utils/tap/tap.sh"
43 fi
44
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"
50 fi
51 fi
52 export BT_TESTS_BT2_BIN
53
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"
58 fi
59 fi
60 export BT_TESTS_BT2LOG_BIN
61
62 # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed
63 BT_PLUGINS_PATH="${BT_TESTS_BUILDDIR}/../src/plugins"
64
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"
68 fi
69
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"
73 fi
74
75
76 ### External Tools ###
77 if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then
78 BT_TESTS_AWK_BIN="awk"
79 fi
80 export BT_TESTS_AWK_BIN
81
82 if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then
83 BT_TESTS_GREP_BIN="grep"
84 fi
85 export BT_TESTS_GREP_BIN
86
87 if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then
88 BT_TESTS_PYTHON_BIN="python3"
89 fi
90 export BT_TESTS_PYTHON_BIN
91
92 if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then
93 BT_TESTS_SED_BIN="sed"
94 fi
95 export BT_TESTS_SED_BIN
96
97
98 # Data files path
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"
102
103
104 ### Diff Functions ###
105
106 # Checks the difference between the content of the file with path "$1"
107 # and the output of the CLI when called with the rest of arguments
108 # to this function.
109 #
110 # Returns 0 if there's no difference, and 1 if there is, also printing
111 # said difference to the standard error.
112 bt_diff_cli() {
113 local expected_file="$1"
114 shift 1
115 local args=("$@")
116
117 local temp_output_file
118 local temp_diff
119 local ret=0
120
121 temp_output_file="$(mktemp)"
122 temp_diff="$(mktemp)"
123
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 "$BT_TESTS_BT2_BIN" "${args[@]}" | tr -d "\r" > "$temp_output_file"
128
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
132 cat "$temp_diff" >&2
133 ret=1
134 fi
135
136 rm -f "$temp_output_file" "$temp_diff"
137
138 return $ret
139 }
140
141 # Checks the difference between the content of the file with path "$1"
142 # and the output of the CLI when called on the directory path "$2" with
143 # the arguments '-c sink.text.details' and the rest of the arguments to
144 # this function.
145 #
146 # Returns 0 if there's no difference, and 1 if there is, also printing
147 # said difference to the standard error.
148 bt_diff_details_ctf_single() {
149 local expected_file="$1"
150 local trace_dir="$2"
151 shift 2
152 local extra_details_args=("$@")
153
154 # Compare using the CLI with `sink.text.details`
155 bt_diff_cli "$expected_file" "$trace_dir" "-c" "sink.text.details" "${extra_details_args[@]}"
156 }
157
158 # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a
159 # program which generates the CTF trace to compare to. The program "$1"
160 # receives the path to a temporary, empty directory where to write the
161 # CTF trace as its first argument.
162 bt_diff_details_ctf_gen_single() {
163 local ctf_gen_prog_path="$1"
164 local expected_file="$2"
165 shift 2
166 local extra_details_args=("$@")
167
168 local temp_trace_dir
169 local ret
170
171 temp_trace_dir="$(mktemp -d)"
172
173 # Run the CTF trace generator program to get a CTF trace
174 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then
175 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
176 rm -rf "$temp_trace_dir"
177 return 1
178 fi
179
180 # Compare using the CLI with `sink.text.details`
181 bt_diff_details_ctf_single "$expected_file" "$temp_trace_dir" "${extra_details_args[@]}"
182 ret=$?
183 rm -rf "$temp_trace_dir"
184 return $ret
185 }
186
187
188 ### Functions ###
189
190 check_coverage() {
191 coverage run "$@"
192 }
193
194 # Execute a shell command in the appropriate environment to have access to the
195 # bt2 Python bindings.
196 run_python_bt2() {
197 local lib_search_var
198 local lib_search_path
199
200 local python_provider_path="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs"
201 local main_lib_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs"
202
203 # Set the library search path so the python interpreter can load libbabeltrace2
204 if [ "x${MSYSTEM:-}" != "x" ]; then
205 lib_search_var="PATH"
206 lib_search_path="${python_provider_path}:${main_lib_path}:${PATH:-}"
207 else
208 lib_search_var="LD_LIBRARY_PATH"
209 lib_search_path="${python_provider_path}:${main_lib_path}:${LD_LIBRARY_PATH:-}"
210 fi
211
212 env \
213 BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1 \
214 BABELTRACE_PLUGIN_PATH="${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \
215 BT_CTF_TRACES_PATH="${BT_CTF_TRACES_PATH}" \
216 BT_PLUGINS_PATH="${BT_PLUGINS_PATH}" \
217 PYTHONPATH="${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python" \
218 "${lib_search_var}"="${lib_search_path}" \
219 "$@"
220 }
221
222 # Set the environment and run python tests in the directory.
223 #
224 # $1 : The directory containing the python test scripts
225 # $2 : The pattern to match python test script names (optional)
226 run_python_bt2_test() {
227 local test_dir="$1"
228 local test_pattern="${2:-'*'}" # optional, if none default to "*"
229
230 local ret
231 local test_runner_args=()
232
233 test_runner_args+=("$test_dir")
234 if [ "x${test_pattern}" != "x" ]; then
235 test_runner_args+=("${test_pattern}")
236 fi
237
238 if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then
239 python_exec="check_coverage"
240 else
241 python_exec="${BT_TESTS_PYTHON_BIN}"
242 fi
243
244 run_python_bt2 \
245 "${python_exec}" \
246 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
247 --pattern "$test_pattern" \
248 "$test_dir" \
249
250 ret=$?
251
252 if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then
253 coverage report -m
254 fi
255
256 if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then
257 coverage html
258 fi
259
260 return $ret
261 }
This page took 0.033979 seconds and 3 git commands to generate.