Add facilities to test CLI and plugin regressions with expectation files
[babeltrace.git] / tests / utils / diff.sh.in
1 #!/bin/sh
2
3 # Copyright (C) 2019 - Philippe Proulx <pproulx@efficios.com>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; only version 2
8 # 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
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 . "@abs_top_builddir@/tests/utils/common.sh"
20
21 # Checks the difference between:
22 #
23 # 1. What the CLI outputs when given the arguments "$1" (passed to
24 # `xargs`, so they can include quoted arguments).
25 # 2. The file with path "$2".
26 #
27 # Returns 0 if there's no difference, and 1 if there is, also printing
28 # said difference to the standard error.
29 bt_diff_cli() {
30 args="$1"
31 expected_file="$2"
32 temp_output_file="$(mktemp)"
33 temp_diff="$(mktemp)"
34
35 # Run the CLI to get a detailed file
36 echo "$args" | xargs "$BT_BIN" 2>/dev/null >"$temp_output_file"
37
38 # Compare output with expected output
39 if ! diff "$temp_output_file" "$expected_file" 2>/dev/null >"$temp_diff"; then
40 echo "ERROR: for '$args': actual and expected outputs differ:" >&2
41 cat "$temp_diff" >&2
42 rm -rf "$temp_output_file" "$temp_diff"
43 return 1
44 fi
45
46 rm -f "$temp_output_file" "$temp_diff"
47 }
48
49 # Checks the difference between:
50 #
51 # 1. What the CLI outputs when given the arguments:
52 #
53 # "$1" -c sink.text.details $3
54 #
55 # 2. The file with path "$2".
56 #
57 # Parameter 3 is optional.
58 #
59 # Returns 0 if there's no difference, and 1 if there is, also printing
60 # said difference to the standard error.
61 bt_diff_details_ctf_single() {
62 trace_dir="$1"
63 expected_file="$2"
64 extra_details_args=""
65
66 if [ $# -ge 3 ]; then
67 extra_details_args="$3"
68 fi
69
70 # Compare using the CLI with `sink.text.details`
71 bt_diff_cli "\"$trace_dir\" -c sink.text.details $extra_details_args" "$expected_file"
72 }
73
74 # Calls bt_diff_details_ctf_single(), except that "$1" is the path to a
75 # program which generates the CTF trace to compare to. The program "$1"
76 # receives the path to a temporary, empty directory where to write the
77 # CTF trace as its first argument.
78 bt_diff_details_ctf_gen_single() {
79 ctf_gen_prog_path="$1"
80 expected_file="$2"
81 extra_details_args=""
82
83 if [ $# -ge 3 ]; then
84 extra_details_args="$3"
85 fi
86
87 temp_trace_dir="$(mktemp -d)"
88
89 # Run the CTF trace generator program to get a CTF trace
90 if ! "$ctf_gen_prog_path" "$temp_trace_dir" 2>/dev/null; then
91 echo "ERROR: \"$ctf_gen_prog_path\" \"$temp_trace_dir\" failed" >&2
92 rm -rf "$temp_trace_dir"
93 return 1
94 fi
95
96 # Compare using the CLI with `sink.text.details`
97 bt_diff_details_ctf_single "$temp_trace_dir" "$expected_file" "$extra_details_args"
98 ret=$?
99 rm -rf "$temp_trace_dir"
100 return $ret
101 }
This page took 0.031579 seconds and 4 git commands to generate.