forked from italia/api-oas-checker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-ruleset.sh
executable file
·98 lines (80 loc) · 2.33 KB
/
test-ruleset.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
#!/bin/bash
#
# Run bash -x test-ruleset.sh RULESET_DIR [RULE_NAME|all] [short]
#
shopt -s extglob
export PATH="$PATH:$PWD/node_modules/.bin:"
DIFF_OPTS='-wubBEr --color'
BASEDIR="${1?Missing base directory}"; shift
spectral_diff(){
local rule="${1?Missing rule parameter}"
spectral lint tests/$rule-{,[0-9]-}test.yml -r $rule.yml | \
diff $DIFF_OPTS -I '^.*tests/.*-test.yml$' "tests/$rule-test.snapshot" -
}
spectral_diff_display(){
local rule="${1?Missing rule parameter}"
local SED_IGNORE_LINES='s/^\s\+[0-9:]\+//g'
spectral lint tests/$rule-{,[0-9]-}test.yml -r $rule.yml | \
sed -e "$SED_IGNORE_LINES" | \
diff $DIFF_OPTS -I '^.*tests/.*-test.yml$' \
<(sed -e "$SED_IGNORE_LINES" "tests/$rule-test.snapshot") -
echo "You are watching a simplified diff."
}
do_or_die(){
local TEST_OUT=$1
if [ "$TEST_OUT" != "0" ]; then
echo "X-( TEST ERROR X-("
echo "X-( Unexpected test result X-("
exit 1
fi
}
cd "$BASEDIR" || {
echo "missing directory $BASEDIR"
exit 1
}
RULES=$(echo *.yml | sed -e 's/.yml\b//g')
RULES_REGEXP="${RULES// /|}"
case "$1" in
"--snapshot")
if [ "$2" != "" ]; then
RULE="$2"
echo -n "Snapshotting rule $RULE.."
spectral lint tests/$RULE-{,[0-9]-}test.yml -r $RULE.yml > tests/$RULE-test.snapshot
exit 0
fi
for RULE in $RULES; do
echo -n "Snapshotting rule $RULE.."
spectral lint tests/$RULE-{,[0-9]-}test.yml -r $RULE.yml > tests/$RULE-test.snapshot
done
exit 0
;;
all)
for RULE in $RULES; do
echo -n "Executing rule $RULE.."
spectral_diff $RULE && echo "Ok"
do_or_die "$?"
done
;;
@($RULES_REGEXP))
RULE="$1"
SHORT="$2"
if [ ! -f "tests/$RULE-test.snapshot" ]; then
echo "Missing test snapshot for rule: $RULE"
exit 1
fi
if [ -n "$SHORT" ]; then
spectral_diff_display $RULE
else
spectral_diff $RULE
fi
spectral_diff $RULE >& /dev/null && echo "Ok"
TEST_OUT="$?"
do_or_die $TEST_OUT
echo "Ok"
exit 0
;;
*)
echo >&2 "Please specify a rule in: $RULES"
exit 1
;;
esac