-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathgenerate_tests
executable file
·235 lines (210 loc) · 6.61 KB
/
generate_tests
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env bash
probspec=
slug=
generator_config="generate_tests.json"
err() { printf '%s\n' "$*" >&2; }
die() { err "$*"; exit 1; }
usage() {
[[ $1 ]] && err "$1"
err "$(basename "$0") [-p problem_specs_path] exercise-name"
err ""
err "Default problem_specs_path is $(defaultProbSpecsPath)"
exit "${rc:-0}"
}
parseOpts() {
local OPTIND OPTARG
while getopts :hp: opt; do
case $opt in
h) rc=0 usage ;;
p) probspec=$OPTARG ;;
?) rc=2 usage "Unknown option '-$OPTARG'" ;;
esac
done
shift $((OPTIND - 1))
(($#)) || rc=2 usage "Specify an exercise slug."
slug=$1
local exerciseDir=./exercises/practice/$slug
[[ -d $exerciseDir ]] || die "Directory does not exist: $exerciseDir"
}
getCacheDir() {
local cacheDir
cacheDir=${XDG_CACHE_HOME:-"$HOME/.cache"}
printf '%s' "$cacheDir"
}
defaultProbSpecsPath() {
# configlet stashes the problem-specifications here:
printf '%s/exercism/configlet/problem-specifications' "$(getCacheDir)"
}
# this is taken from `configlet`
# - not fully implemented in case of unclean working directory
refreshProbSpecs() {
[[ -z $probspec ]] && probspec="$(defaultProbSpecsPath)" # global
local url="https://github.com/exercism/problem-specifications"
local dir
dir=$(dirname "$probspec")
if [[ ! -d $dir ]]; then
mkdir -p "$dir"
fi
if [[ ! -d $probspec ]]; then
(
cd "$dir" \
&& git clone --depth 1 --single-branch -- "$url"
)
else
(
cd "$probspec" \
&& git checkout main \
&& git fetch --quiet \
&& git merge --ff-only origin/main
)
fi
}
arrayContains() {
local -n _arr=$1
local elem=$2 e
for e in "${_arr[@]}"; do
[[ $elem == "$e" ]] && return 0
done
return 1
}
runCommand() {
jq -r --arg slug "$slug" '
(.["runner code"][$slug] // ["jq -r -f \($slug).jq"])
| join("\n")
' "$generator_config"
}
writeTestFile() {
local canonicalData="$probspec/exercises/$slug/canonical-data.json"
[[ -f $canonicalData ]] || die "No canonical data for $slug"
local property
property=$(
jq --arg slug "$slug" '
.["include .property in input json"] as $slugs
| $slug | IN($slugs[])
' "$generator_config"
)
local uuids
uuids=$(
awk -v RS='\n\n' -F '\n' '
$1 ~ /^[[]/ && ! /include = false/ {
gsub(/[][]/, "", $1)
print $1
}
' ./exercises/practice/"$slug"/.meta/tests.toml \
| jq -Rc '[., inputs]'
)
local testFile="./exercises/practice/$slug/test-${slug}.bats"
{
cat << END_PREAMBLE
#!/usr/bin/env bats
# generated on $(date --utc "+%FT%TZ")
load bats-extra
load bats-jq
END_PREAMBLE
jq -r --arg slug "$slug" \
--arg cmd "$(runCommand)" \
--arg Q "'" \
--argjson property "$property" \
--argjson included_uuids "$uuids" \
'
def prefix(desc): if desc == "" then "" else "\(desc):" end;
def test_cases(desc):
if has("cases") then
# recurse to extract the nested test cases
.description as $d | .cases[] | test_cases(prefix(desc) + $d)
elif $slug == "forth" and (.scenarios//[] as $s | "local-scope" | IN($s[])) then
# do not want this particular test
empty
else
# emit this case with an updated description
.description = prefix(desc) + .description
end
;
def included_tests:
map(select(.uuid | IN($included_uuids[])))
;
def generate_test:
(.expected | type == "object") as $isobj
| (.expected | type == "array") as $isarr
| ($isobj and (.expected | has("error"))) as $iserr
| (if ($isobj or $isarr)
then ($cmd | sub("jq -r";"jq -c"))
else $cmd
end) as $command
|
"\n"
+ "@test \(.description | @sh) {\n"
+ " "
+ "[[ $BATS_RUN_SKIPPED == \"true\" ]] || skip\n"
+ "\n"
+ " run \($command) << \($Q)END_INPUT\($Q)"
, if $property then {property, input} else .input end
, "END_INPUT\n"
, ( if $iserr
then
" assert_failure\n"
+ " expected=\(.expected.error | @sh)"
else
" assert_success"
, ( .expected
| if ($isobj or $isarr)
then " expected=\(@json | @sh)"
else " expected=\(@sh)"
end
)
end
,
if ($isobj and ($iserr | not))
then
" assert_objects_equal \"$output\" \"$expected\""
else
" assert_equal \"$output\" \"$expected\""
end
)
, "}"
;
# main
[.cases[] | test_cases("")] | included_tests
| .[] | generate_test
' "$canonicalData"
} \
| awk '
# un-skip the first test
/BATS_RUN_SKIPPED/ && !seen {
sub(/[[]/, "#[")
seen = 1
}
# indent the input
/^END_INPUT$/ {indent = 0}
indent {sub(/^/, " ")}
/<< .END_INPUT/ {indent = 1}
1
' \
| tee "$testFile"
if grep -q assert_objects_equal "$testFile"; then
local t=$(mktemp)
local func=$(cat <<'END_FUNC'
assert_objects_equal() {
local result=$(
jq -n --argjson actual "$1" \\
--argjson expected "$2" \\
'$actual == $expected'
)
[[ $result == "true" ]]
}
END_FUNC
)
awk -v fn="$func" '
{print}
/load bats-extra/ { print ""; print fn }
' "$testFile" > "$t" && mv "$t" "$testFile"
fi
}
[[ -d ./exercises/practice ]] || die "Run me from the repo root."
parseOpts "$@"
if jq --arg slug "$slug" '.omit as $o | $slug | IN($o[]) // halt_error' "$generator_config" >/dev/null 2>&1; then
err "Don't generate tests for exercise $slug"
exit
fi
refreshProbSpecs
writeTestFile