Commit | Line | Data |
---|---|---|
644e0364 MJ |
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 | ||
5058d31b JR |
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)" | |
33 | case "$BT_OS_TYPE" in | |
34 | MINGW*) | |
35 | BT_OS_TYPE="mingw" | |
36 | ;; | |
37 | Darwin) | |
38 | BT_OS_TYPE="darwin" | |
39 | ;; | |
40 | Linux) | |
41 | BT_OS_TYPE="linux" | |
42 | ;; | |
df9db467 JR |
43 | CYGWIN*) |
44 | BT_OS_TYPE="cygwin" | |
45 | ;; | |
5058d31b JR |
46 | *) |
47 | BT_OS_TYPE="unsupported" | |
48 | ;; | |
49 | esac | |
50 | fi | |
51 | export BT_OS_TYPE | |
52 | ||
644e0364 MJ |
53 | # Allow overriding the source and build directories |
54 | if [ "x${BT_TESTS_SRCDIR:-}" = "x" ]; then | |
4881a20e | 55 | BT_TESTS_SRCDIR="$scriptdir/.." |
644e0364 MJ |
56 | fi |
57 | export BT_TESTS_SRCDIR | |
58 | ||
59 | if [ "x${BT_TESTS_BUILDDIR:-}" = "x" ]; then | |
4881a20e | 60 | BT_TESTS_BUILDDIR="$scriptdir/.." |
644e0364 MJ |
61 | fi |
62 | export BT_TESTS_BUILDDIR | |
63 | ||
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 | |
66 | # script. | |
67 | if [ "x${SH_TAP:-}" = x1 ]; then | |
1d970e04 | 68 | # shellcheck source=./tap/tap.sh |
644e0364 MJ |
69 | . "${BT_TESTS_SRCDIR}/utils/tap/tap.sh" |
70 | fi | |
71 | ||
72 | # Allow overriding the babeltrace2 executables | |
73 | if [ "x${BT_TESTS_BT2_BIN:-}" = "x" ]; then | |
4881a20e | 74 | BT_TESTS_BT2_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2" |
5058d31b | 75 | if [ "$BT_OS_TYPE" = "mingw" ]; then |
27e2c58d JR |
76 | BT_TESTS_BT2_BIN="${BT_TESTS_BT2_BIN}.exe" |
77 | fi | |
644e0364 MJ |
78 | fi |
79 | export BT_TESTS_BT2_BIN | |
80 | ||
81 | if [ "x${BT_TESTS_BT2LOG_BIN:-}" = "x" ]; then | |
4881a20e | 82 | BT_TESTS_BT2LOG_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2-log" |
5058d31b | 83 | if [ "$BT_OS_TYPE" = "mingw" ]; then |
27e2c58d JR |
84 | BT_TESTS_BT2LOG_BIN="${BT_TESTS_BT2LOG_BIN}.exe" |
85 | fi | |
644e0364 MJ |
86 | fi |
87 | export BT_TESTS_BT2LOG_BIN | |
88 | ||
89 | # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed | |
90 | BT_PLUGINS_PATH="${BT_TESTS_BUILDDIR}/../src/plugins" | |
91 | ||
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" | |
95 | fi | |
96 | ||
b14c7bf1 MJ |
97 | if [ "x${BT_TESTS_PROVIDER_DIR:-}" = "x" ]; then |
98 | BT_TESTS_PROVIDER_DIR="${BT_TESTS_BUILDDIR}/../src/python-plugin-provider/.libs" | |
99 | fi | |
100 | ||
644e0364 MJ |
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" | |
104 | fi | |
105 | ||
106 | ||
107 | ### External Tools ### | |
108 | if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then | |
109 | BT_TESTS_AWK_BIN="awk" | |
110 | fi | |
111 | export BT_TESTS_AWK_BIN | |
112 | ||
113 | if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then | |
114 | BT_TESTS_GREP_BIN="grep" | |
115 | fi | |
116 | export BT_TESTS_GREP_BIN | |
117 | ||
118 | if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then | |
119 | BT_TESTS_PYTHON_BIN="python3" | |
120 | fi | |
121 | export BT_TESTS_PYTHON_BIN | |
122 | ||
123 | if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then | |
124 | BT_TESTS_SED_BIN="sed" | |
125 | fi | |
126 | export BT_TESTS_SED_BIN | |
127 | ||
128 | ||
129 | # Data files path | |
130 | BT_TESTS_DATADIR="${BT_TESTS_SRCDIR}/data" | |
131 | BT_CTF_TRACES_PATH="${BT_TESTS_DATADIR}/ctf-traces" | |
644e0364 MJ |
132 | |
133 | ||
134 | ### Diff Functions ### | |
135 | ||
58db335e FD |
136 | # Checks the difference between: |
137 | # | |
138 | # 1. What the CLI outputs on its standard output when given the arguments | |
139 | # "$@" (excluding the first two arguments). | |
140 | # 2. The file with path "$1". | |
141 | # | |
142 | # And the difference between: | |
143 | # | |
144 | # 1. What the CLI outputs on its standard error when given the arguments | |
145 | # "$@" (excluding the first two arguments). | |
146 | # 2. The file with path "$2". | |
644e0364 MJ |
147 | # |
148 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
149 | # said difference to the standard error. | |
150 | bt_diff_cli() { | |
58db335e FD |
151 | local expected_stdout_file="$1" |
152 | local expected_stderr_file="$2" | |
153 | shift 2 | |
53cc240b MJ |
154 | local args=("$@") |
155 | ||
58db335e FD |
156 | local temp_stdout_output_file |
157 | local temp_stderr_output_file | |
644e0364 MJ |
158 | local temp_diff |
159 | local ret=0 | |
160 | ||
58db335e FD |
161 | temp_stdout_output_file="$(mktemp)" |
162 | temp_stderr_output_file="$(mktemp)" | |
644e0364 MJ |
163 | temp_diff="$(mktemp)" |
164 | ||
58db335e FD |
165 | # Run the CLI to get a detailed file. |
166 | run_python_bt2 "$BT_TESTS_BT2_BIN" "${args[@]}" 1>"$temp_stdout_output_file" 2>"$temp_stderr_output_file" | |
167 | ||
168 | # Strip any \r present due to Windows (\n -> \r\n). | |
169 | # "diff --string-trailing-cr" is not used since it is not present on | |
170 | # Solaris. | |
171 | "$BT_TESTS_SED_BIN" -i 's/\r//g' "$temp_stdout_output_file" | |
172 | "$BT_TESTS_SED_BIN" -i 's/\r//g' "$temp_stderr_output_file" | |
173 | ||
174 | # Compare stdout output with expected stdout output | |
175 | if ! diff -u "$temp_stdout_output_file" "$expected_stdout_file" 2>/dev/null >"$temp_diff"; then | |
176 | echo "ERROR: for '${args[*]}': actual standard output and expected output differ:" >&2 | |
177 | cat "$temp_diff" >&2 | |
178 | ret=1 | |
179 | fi | |
644e0364 | 180 | |
58db335e FD |
181 | # Compare stderr output with expected stderr output |
182 | if ! diff -u "$temp_stderr_output_file" "$expected_stderr_file" 2>/dev/null >"$temp_diff"; then | |
183 | echo "ERROR: for '${args[*]}': actual standard error and expected error differ:" >&2 | |
644e0364 MJ |
184 | cat "$temp_diff" >&2 |
185 | ret=1 | |
186 | fi | |
187 | ||
58db335e | 188 | rm -f "$temp_stdout_output_file" "$temp_stderr_output_file" "$temp_diff" |
644e0364 MJ |
189 | |
190 | return $ret | |
191 | } | |
192 | ||
53cc240b MJ |
193 | # Checks the difference between the content of the file with path "$1" |
194 | # and the output of the CLI when called on the directory path "$2" with | |
195 | # the arguments '-c sink.text.details' and the rest of the arguments to | |
196 | # this function. | |
644e0364 MJ |
197 | # |
198 | # Returns 0 if there's no difference, and 1 if there is, also printing | |
199 | # said difference to the standard error. | |
200 | bt_diff_details_ctf_single() { | |
58db335e | 201 | local expected_stdout_file="$1" |
53cc240b MJ |
202 | local trace_dir="$2" |
203 | shift 2 | |
204 | local extra_details_args=("$@") | |
58db335e | 205 | expected_stderr_file="/dev/null" |
644e0364 MJ |
206 | |
207 | # Compare using the CLI with `sink.text.details` | |
58db335e | 208 | bt_diff_cli "$expected_stdout_file" "$expected_stderr_file" "$trace_dir" "-c" "sink.text.details" "${extra_details_args[@]}" |
644e0364 MJ |
209 | } |
210 | ||
211 | # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a | |
212 | # program which generates the CTF trace to compare to. The program "$1" | |
213 | # receives the path to a temporary, empty directory where to write the | |
214 | # CTF trace as its first argument. | |
215 | bt_diff_details_ctf_gen_single() { | |
216 | local ctf_gen_prog_path="$1" | |
58db335e | 217 | local expected_stdout_file="$2" |
53cc240b MJ |
218 | shift 2 |
219 | local extra_details_args=("$@") | |
644e0364 MJ |
220 | |
221 | local temp_trace_dir | |
222 | local ret | |
223 | ||
224 | temp_trace_dir="$(mktemp -d)" | |
225 | ||
226 | # Run the CTF trace generator program to get a CTF trace | |
227 | if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then | |
228 | echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2 | |
229 | rm -rf "$temp_trace_dir" | |
230 | return 1 | |
231 | fi | |
232 | ||
233 | # Compare using the CLI with `sink.text.details` | |
58db335e | 234 | bt_diff_details_ctf_single "$expected_stdout_file" "$temp_trace_dir" "${extra_details_args[@]}" |
644e0364 MJ |
235 | ret=$? |
236 | rm -rf "$temp_trace_dir" | |
237 | return $ret | |
238 | } | |
239 | ||
240 | ||
241 | ### Functions ### | |
242 | ||
243 | check_coverage() { | |
244 | coverage run "$@" | |
245 | } | |
246 | ||
247 | # Execute a shell command in the appropriate environment to have access to the | |
248 | # bt2 Python bindings. | |
249 | run_python_bt2() { | |
250 | local lib_search_var | |
251 | local lib_search_path | |
252 | ||
0408934a SM |
253 | local main_lib_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs" |
254 | ||
644e0364 | 255 | # Set the library search path so the python interpreter can load libbabeltrace2 |
df9db467 | 256 | if [ "$BT_OS_TYPE" = "mingw" ] || [ "$BT_OS_TYPE" = "cygwin" ]; then |
644e0364 | 257 | lib_search_var="PATH" |
b14c7bf1 | 258 | lib_search_path="${main_lib_path}:${PATH:-}" |
5058d31b JR |
259 | elif [ "$BT_OS_TYPE" = "darwin" ]; then |
260 | lib_search_var="DYLD_LIBRARY_PATH" | |
b14c7bf1 | 261 | lib_search_path="${main_lib_path}:${DYLD_LIBRARY_PATH:-}" |
644e0364 MJ |
262 | else |
263 | lib_search_var="LD_LIBRARY_PATH" | |
b14c7bf1 | 264 | lib_search_path="${main_lib_path}:${LD_LIBRARY_PATH:-}" |
644e0364 MJ |
265 | fi |
266 | ||
267 | env \ | |
268 | BABELTRACE_PYTHON_BT2_NO_TRACEBACK=1 \ | |
269 | BABELTRACE_PLUGIN_PATH="${BT_TESTS_BABELTRACE_PLUGIN_PATH}" \ | |
b14c7bf1 | 270 | LIBBABELTRACE2_PLUGIN_PROVIDER_DIR=${BT_TESTS_PROVIDER_DIR} \ |
644e0364 MJ |
271 | BT_CTF_TRACES_PATH="${BT_CTF_TRACES_PATH}" \ |
272 | BT_PLUGINS_PATH="${BT_PLUGINS_PATH}" \ | |
273 | PYTHONPATH="${BT_TESTS_PYTHONPATH}:${BT_TESTS_SRCDIR}/utils/python" \ | |
274 | "${lib_search_var}"="${lib_search_path}" \ | |
275 | "$@" | |
276 | } | |
277 | ||
278 | # Set the environment and run python tests in the directory. | |
279 | # | |
280 | # $1 : The directory containing the python test scripts | |
281 | # $2 : The pattern to match python test script names (optional) | |
644e0364 MJ |
282 | run_python_bt2_test() { |
283 | local test_dir="$1" | |
a818a617 | 284 | local test_pattern="${2:-'*'}" # optional, if none default to "*" |
644e0364 MJ |
285 | |
286 | local ret | |
287 | local test_runner_args=() | |
288 | ||
289 | test_runner_args+=("$test_dir") | |
290 | if [ "x${test_pattern}" != "x" ]; then | |
291 | test_runner_args+=("${test_pattern}") | |
292 | fi | |
293 | ||
294 | if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then | |
295 | python_exec="check_coverage" | |
296 | else | |
297 | python_exec="${BT_TESTS_PYTHON_BIN}" | |
298 | fi | |
299 | ||
300 | run_python_bt2 \ | |
301 | "${python_exec}" \ | |
302 | "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \ | |
a818a617 FD |
303 | --pattern "$test_pattern" \ |
304 | "$test_dir" \ | |
305 | ||
644e0364 MJ |
306 | ret=$? |
307 | ||
308 | if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then | |
309 | coverage report -m | |
310 | fi | |
311 | ||
312 | if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then | |
313 | coverage html | |
314 | fi | |
315 | ||
316 | return $ret | |
317 | } |