summaryrefslogtreecommitdiffstats
path: root/tests/py/tools/test-sanitizer.sh
blob: 92354d2b04e39b795ae2408b0a80cbe66cfe70c3 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash

# Do some simple sanity checks on tests:
# - Report tests where reply matches command
# - Report tests with non-ok exit but reply
# - Check for duplicate test commands in *.t files
# - Check for duplicate or stale payload records in *.t.payload* files
# - Check for duplicate or stale json equivalents in *.t.json files

cd $(dirname $0)/../

[[ $1 ]] && tests="$@" || tests="*/*.t"

reportfile=""
report() { # (file, msg)
	[[ "$reportfile" == "$1" ]] || {
		reportfile="$1"
		echo ""
		echo "In $reportfile:"
	}
	shift
	echo "$@"
}

for t in $tests; do
	[[ -f $t ]] || continue

	readarray -t cmdlines <<< $(grep -v -e '^ *[:*#-?]' -e '^ *$' $t)

	cmds=""
	for cmdline in "${cmdlines[@]}"; do
		readarray -t -d ';' cmdparts <<< "$cmdline"
		cmd="${cmdparts[0]}"
		rc="${cmdparts[1]}"
		out="${cmdparts[2]}"

		[[ -n $cmd ]] || continue

		#echo "cmdline: $cmdline"
		#echo "cmd: $cmd"
		#echo "rc: $rc"
		#echo "out: $out"

		[[ "$cmd" != "$out" ]] || \
			report $t "reply matches cmd: $cmd"
		[[ "$rc" != "ok" && "$out" ]] && \
			report $t "output record with non-ok exit: $cmd"

		cmds+="${cmd}\n"
	done

	readarray -t dups <<< $(echo -e "$cmds" | sort | uniq -d)
	for dup in "${dups[@]}"; do
		[[ -n $dup ]] || continue
		report $t "duplicate command: $dup"
	done

	for p in $t.payload* $t.json; do
		[[ -f $p ]] || continue
		[[ $p == *.got ]] && continue
		[[ $p == *.json ]] && t="json" || t="payload"

		pcmds=$(grep '^#' $p)
		readarray -t dups <<< $(echo "$pcmds" | sort | uniq -d)
		readarray -t stales <<< $(echo "$pcmds" | while read hash pcmd; do
			echo -e "$cmds" | grep -qxF "${pcmd}" || echo "# ${pcmd}"
		done)

		for stale in "${stales[@]}"; do
			[[ -n $stale ]] || continue
			report $p "stale $t record: $stale"
		done
		for dup in "${dups[@]}"; do
			[[ -n $dup ]] || continue
			report $p "duplicate $t record: $dup"
		done
	done
done