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