fix: don't use realpath from GNU coreutils
[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 fi
49 export BT_TESTS_BT2_BIN
50
51 if [ "x${BT_TESTS_BT2LOG_BIN:-}" = "x" ]; then
52 BT_TESTS_BT2LOG_BIN="$BT_TESTS_BUILDDIR/../src/cli/babeltrace2-log"
53 fi
54 export BT_TESTS_BT2LOG_BIN
55
56 # TODO: Remove when bindings/python/bt2/test_plugin.py is fixed
57 BT_PLUGINS_PATH="${BT_TESTS_BUILDDIR}/../src/plugins"
58
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"
62 fi
63
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"
67 fi
68
69
70 ### External Tools ###
71 if [ "x${BT_TESTS_AWK_BIN:-}" = "x" ]; then
72 BT_TESTS_AWK_BIN="awk"
73 fi
74 export BT_TESTS_AWK_BIN
75
76 if [ "x${BT_TESTS_GREP_BIN:-}" = "x" ]; then
77 BT_TESTS_GREP_BIN="grep"
78 fi
79 export BT_TESTS_GREP_BIN
80
81 if [ "x${BT_TESTS_PYTHON_BIN:-}" = "x" ]; then
82 BT_TESTS_PYTHON_BIN="python3"
83 fi
84 export BT_TESTS_PYTHON_BIN
85
86 if [ "x${BT_TESTS_SED_BIN:-}" = "x" ]; then
87 BT_TESTS_SED_BIN="sed"
88 fi
89 export BT_TESTS_SED_BIN
90
91
92 # Data files path
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"
96
97
98 ### Diff Functions ###
99
100 # Checks the difference between:
101 #
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".
105 #
106 # Returns 0 if there's no difference, and 1 if there is, also printing
107 # said difference to the standard error.
108 bt_diff_cli() {
109 local args="$1"
110 local expected_file="$2"
111 local temp_output_file
112 local temp_diff
113 local ret=0
114
115 temp_output_file="$(mktemp)"
116 temp_diff="$(mktemp)"
117
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"
122
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
126 cat "$temp_diff" >&2
127 ret=1
128 fi
129
130 rm -f "$temp_output_file" "$temp_diff"
131
132 return $ret
133 }
134
135 # Checks the difference between:
136 #
137 # 1. What the CLI outputs when given the arguments:
138 #
139 # "$1" -c sink.text.details $3
140 #
141 # 2. The file with path "$2".
142 #
143 # Parameter 3 is optional.
144 #
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() {
148 local trace_dir="$1"
149 local expected_file="$2"
150 local extra_details_args="${3:-}"
151
152 # Compare using the CLI with `sink.text.details`
153 bt_diff_cli "\"$trace_dir\" -c sink.text.details $extra_details_args" "$expected_file"
154 }
155
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:-}"
164
165 local temp_trace_dir
166 local ret
167
168 temp_trace_dir="$(mktemp -d)"
169
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"
174 return 1
175 fi
176
177 # Compare using the CLI with `sink.text.details`
178 bt_diff_details_ctf_single "$temp_trace_dir" "$expected_file" "$extra_details_args"
179 ret=$?
180 rm -rf "$temp_trace_dir"
181 return $ret
182 }
183
184
185 ### Functions ###
186
187 check_coverage() {
188 coverage run "$@"
189 }
190
191 # Execute a shell command in the appropriate environment to have access to the
192 # bt2 Python bindings.
193 run_python_bt2() {
194 local lib_search_var
195 local lib_search_path
196
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:-}"
201 else
202 lib_search_var="LD_LIBRARY_PATH"
203 lib_search_path="${BT_TESTS_BUILDDIR}/../src/lib/.libs:${LD_LIBRARY_PATH:-}"
204 fi
205 if [ "x${test_lib_search_path:-}" != "x" ]; then
206 lib_search_path+=":${test_lib_search_path}"
207 fi
208
209 env \
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}" \
216 "$@"
217 }
218
219 # Set the environment and run python tests in the directory.
220 #
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() {
225 local test_dir="$1"
226 local test_pattern="${2:-}" # optional
227 local test_lib_search_path="${3:-}" # optional
228
229 local ret
230 local test_runner_args=()
231
232 test_runner_args+=("$test_dir")
233 if [ "x${test_pattern}" != "x" ]; then
234 test_runner_args+=("${test_pattern}")
235 fi
236
237 if test "x${BT_TESTS_COVERAGE:-}" = "x1"; then
238 python_exec="check_coverage"
239 else
240 python_exec="${BT_TESTS_PYTHON_BIN}"
241 fi
242
243 run_python_bt2 \
244 "${python_exec}" \
245 "${BT_TESTS_SRCDIR}/utils/python/testrunner.py" \
246 "${test_runner_args[@]}"
247 ret=$?
248
249 if test "x${BT_TESTS_COVERAGE_REPORT:-}" = "x1"; then
250 coverage report -m
251 fi
252
253 if test "x${BT_TESTS_COVERAGE_HTML:-}" = "x1"; then
254 coverage html
255 fi
256
257 return $ret
258 }
This page took 0.03465 seconds and 4 git commands to generate.