-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
executable file
·94 lines (82 loc) · 2.52 KB
/
main.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# https://github.com/slve/json.test
# sanitize removes the give paths from responses and expected to ignore changes we don't care
# shellcheck disable=SC2037
sanitize(){
IFS= read out
while read -r jqcmd
do
out=$(echo $out | jq $jqcmd 2>/dev/null)
done < ./sanitize
echo $out
}
# ellipsis truncates long string and adds '...'
ellipsis(){
awk -v len=120 '{
if (length($0) > len) print substr($0, 1, len-3) "...";
else print;
}'
}
# log pipes the input to ./log file and adds an empty line after
log(){ tee -a log; echo >> ./log; }
# cleanup + init
rm ./queries.sh ./responses ./expected ./titles ./log 2>/dev/null
touch ./queries.sh ./responses ./expected ./titles && chmod +x ./queries.sh
# generate ./titles
for f in *.test; do
grep '^\s*\(title\|name\)\s*:\?' $f >> ./titles;
done
sed -i 's/^\s*\(title\|name\)\s*:\?\s*//' ./titles
# generate ./queries.sh
for f in *.test; do
grep '^\s*\(given\|when\)\s*:\?' $f >> ./queries.sh;
done
sed -i 's/^\s*\(given\|when\)\s*:\?\s*//' ./queries.sh
# generate ./expected
for f in *.test; do
grep '^\s*\(expect\|then\)\s*:\?' $f >> ./expected;
done
sed -i 's/^\s*\(expect\|then\)\s*:\?\s*//' ./expected
# actual run
# run the queries and generate ./responses
rm ./responses ./log 2>/dev/null
while read -r cmd
do
out=`eval $cmd`
echo $out >> ./responses
done < ./queries.sh
# generate ./expected if there is no one yet
if [ ! -r ./expected ]; then
echo "Could not find ./expected file, so just cloned ./responses to make one."
cp ./responses ./expected
echo "Now returning with failure as a reminder to add your ./expected file to your repository."
exit 1
fi
# go through each response and compare with the expected
i=0
passed=0
failed=0
exitcode=0
while IFS="$(printf '\t')" read -r resp exp query title
do
i=$[i + 1]
respSan=$(echo $resp | sanitize)
expSan=$(echo $exp | sanitize)
d=$(diff <(echo "$respSan" ) <(echo "$expSan"))
if [[ ! -z $d ]]; then
exitcode=1
failed=$[failed + 1]
printf 'Diff at: "%s". query: %s\n' "$title" "$query" | log | ellipsis
printf 'Response: %s\n' "$resp" | log >/dev/null
printf 'Expected: %s\n' "$exp" | log >/dev/null
diff -u <(echo $expSan | jq -S .) <(echo $respSan | jq -S .) | log
else
passed=$[passed + 1]
fi
done <<< "$(paste ./responses ./expected ./queries.sh ./titles)"
# test report
echo "Passed: $passed Failed: $failed"
if (($exitcode > 0)); then
echo "Some test have failed. See more in ./log";
echo "Hint: you can copy ./responses over ./expected to have a fresh snapshot.";
fi
exit $exitcode