- Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathdiff-example.sh
executable file
·53 lines (43 loc) · 1.34 KB
/
diff-example.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env bash
# Copyright 2017 The Cockroach Authors.
#
# Use of this software is governed by the CockroachDB Software License
# included in the /LICENSE file.
# Diff a failed Go example [0] against its expected output.
#
# By default, when an example fails, Go prints "got:", followed by the
# actual output, then prints "want:", followed by the expected output. When the
# output grows beyond several lines, as it often does, it's virtually impossible
# to visually parse where the output differs, especially when whitespace is
# involved. This script parses the output and prints it as a unified diff to
# make the difference obvious.
#
# If this script produces no output, it means the actual and expected output
# matched.
#
# Sample usage:
#
# $ make test PKG=./pkg/cli TESTS=Example_zone | scripts/diff-example.sh
#
# [0]: https://golang.org/pkg/testing/#hdr-Examples
set -euo pipefail
do_diff() {
if [ -x"$(command -v colordiff)" ];then
colordiff "$@"
else
diff "$@"
fi
}
cd$(mktemp -d)
trap'rm -rf $PWD' EXIT
cat >in
# The actual output is everything after "got:" but before "want:".
<in sed -n "
/^got:$/,/^want:$/{
//d
p
}">got
# The desired output is everything after "want:", excluding the status about
# whether the test passed or failed.
<in sed "1,/^want:$/d"| grep -Ev "^(PASS|FAIL)">want
do_diff -u want got