1#!/usr/bin/env bash
2
3# Our custom diff tool which normalizes result and expected files before
4# doing the actual diff. Rules for normalization are in normalize.sed. See
5# the comments there for more information.
6#
7# Note that src/test/regress/Makefile adds this directory to $PATH so
8# pg_regress uses this diff tool instead of the system diff tool.
9set -eu -o pipefail
10
11file1="${@:(-2):1}"
12file2="${@:(-1):1}"
13# pg_regress passes the expected file as the first argument ($file1 above),
14# and results file as the second argument ($file2 above). We take the base
15# filename of the expected file as the test name. We can have multiple
16# expected files for a single test with _0, _1, ... suffixes.
17# So for the test name, we also strip the additional suffix.
18test=$(basename "$file1" .out | sed -E "s/_[0-9]+$//")
19args=${@:1:$#-2}
20BASEDIR=$(dirname "$0")
21
22# whereis searches for standard unix places before $PATH. So select the first
23# entry as the original diff tool.
24DIFF=$(whereis diff | sed "s/diff://g" | awk '{print $1}')
25if [ -z "$DIFF" ]
26then
27    DIFF=/usr/bin/diff
28fi
29
30if test -z "${VANILLATEST:-}"
31then
32	touch "$file1"  # when adding a new test the expected file does not exist
33	normalize_file="$BASEDIR/normalize.sed"
34	# when running tests on an existing cluster some changes need to be done on
35	# normalize.sed file. So a new file is used.
36	if [[ -f "$BASEDIR/normalize_modified.sed" ]]
37	then
38		normalize_file="$BASEDIR/normalize_modified.sed"
39	fi
40	sed -Ef "$normalize_file" < "$file1" > "$file1.modified"
41	sed -Ef "$normalize_file" < "$file2" > "$file2.modified"
42	"$DIFF" -w $args "$file1.modified" "$file2.modified" | LC_CTYPE=C.UTF-8 diff-filter "$BASEDIR/normalize.sed"
43	exit ${PIPESTATUS[0]}
44else
45	exec "$DIFF" -w $args "$file1" "$file2"
46fi
47
48