forked from antlr/grammars-v4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-cover.sh
409 lines (373 loc) · 12.2 KB
/
test-cover.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/bash
myrealpath ()
{
f=$@;
if [ -d "$f" ]; then
base="";
dir="$f";
else
base="/$(basename "$f")";
dir=$(dirname "$f");
fi;
dir=$(cd "$dir" && /bin/pwd);
echo "$dir$base"
}
# true or false
quiet=true
# Get full path of this script.
full_path_script=$(myrealpath $0)
full_path_templates=$(dirname $full_path_script)/templates
# Sanity checks for required environment.
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${unameOut}"
esac
if [[ "$machine" != "Linux" && "$machine" != "Mac" && "$machine" != "MinGw" ]]
then
echo "This script runs only in a Linux environment. Skipping."
exit 0
fi
thetime()
{
local hh
local DIFF
DIFF=$1
hh="$(($DIFF / 3600 ))"
mm="$((($DIFF % 3600) / 60))"
ss="$(($DIFF % 60))"
printf "%02d:%02d:%02d" $hh $mm $ss
}
function getopts-extra () {
OPTARG=()
declare i=0
# if the next argument is not an option, then append it to array OPTARG
while [[ ${OPTIND} -le $# && ${!OPTIND:0:1} != '-' ]]; do
OPTARG[i]=${!OPTIND}
let i++ OPTIND++
done
}
# filter="all" or "diff" or "agnostic"
filter="all"
# order="grammar" or "target"
order="grammar"
failed=()
succeeded=()
skipped=""
grammars=()
targets=( "CSharp" )
tests=()
# Get "root" of the repo clone.
cwd=`pwd`
prefix=`pwd`
pushd $prefix > /dev/null 2>&1
while true
do
if [ -f `pwd`/_scripts/test.sh ]
then
break
elif [ `pwd` == "/" ]
then
break
fi
cd ..
done
if [ ! -f `pwd`/_scripts/test-cover.sh ]
then
echo "Not in repo."
exit 1
fi
prefix=`pwd`
popd > /dev/null 2>&1
rm -rf `find . -name 'Generated*' -type d`
# Parse args, and update computation to perform.
order="grammars"
additional=()
antlr4jar=/tmp/antlr4-complete.jar
while getopts 'agthf' opt; do
case "$opt" in
a)
getopts-extra "$@"
antlr4jar="${OPTARG[0]}"
echo Antlr4jar is $antlr4jar
;;
g)
getopts-extra "$@"
for a in "${OPTARG[@]}"
do
grammars+=( "$a" )
done
;;
t)
getopts-extra "$@"
for a in "${OPTARG[@]}"
do
targets+=( "$a" )
done
;;
o)
order="$OPTARG"
;;
f)
getopts-extra "$@"
filter="${OPTARG[0]}"
for a in "${OPTARG[@]:1}"
do
additional+=( "$a" )
done
;;
?|h)
cat - <<EOF
NAME
test-cover - tests Antlr4 grammars
SYNOPSIS
$(basename $0) ([-g ...] | [-s ...] | [-t ...])
DESCRIPTION
Tests Antlr4 grammars. Each grammar requires pom.xml and desc.xml files.
OPTIONS
-g
Specifies a list of directories that are searched for grammars.
A grammar requires pom.xml and desc.xml files for it to be tested.
If the option is not specified, then the default is to search from
the current directory. Searching is subject to filters.
-f
Specifies the type of testing to filter. Possible types are "diff",
"all", or "agnostic". The default is "all".
-t
Specifies the targets to test. Possible targets are
"CSharp", "Cpp", "Dart", "Go", "Java", "JavaScript", "PHP", "Python3".
All targets are tested by default.
EOF
exit 0
;;
esac
done
if [ "$filter" == "diff" ]
then
if [ "${#additional[@]}" -eq 0 ]
then
diffs=`git diff --exit-code --name-only .`
if [ "$diffs" != "" ]
then
grammars=()
# Test grammars for the enclosing directories.
directories=`git diff --name-only . 2> /dev/null | sed 's#\(.*\)[/][^/]*$#\1#' | sort -u | grep -v _scripts`
for g in $directories
do
pushd $g > /dev/null
while true
do
if [ -f `pwd`/desc.xml ]
then
break
elif [ `pwd` == "$prefix" ]
then
break
fi
cd ..
done
g=`pwd`
g=${g##*$prefix}
g=${g##/}
if [ "$g" == "" ]
then
g="."
fi
popd > /dev/null
echo Adding diff $g
grammars+=( $g )
done
grammars=( $(for g in "${grammars[@]}"; do echo "${g}"; done | sort -u) )
fi
elif [ "${#additional[@]}" -eq 2 ]
then
grammars=()
# Test grammars for the enclosing directories.
directories=`git diff --name-only ${additional[0]} ${additional[1]} . 2> /dev/null | sed 's#\(.*\)[/][^/]*$#\1#' | sort -u | grep -v _scripts`
for g in $directories
do
pushd $g > /dev/null
while true
do
if [ -f `pwd`/desc.xml ]
then
break
elif [ `pwd` == "$prefix" ]
then
break
fi
cd ..
done
g=`pwd`
g=${g##*$prefix}
g=${g##/}
if [ "$g" == "" ]
then
g="."
fi
popd > /dev/null
echo Adding diff $g
grammars+=( $g )
done
else
echo ouch
exit 1
fi
elif [ "$filter" == "all" ]
then
grammars=()
# Test grammars for the enclosing directories.
directories=`find . -name desc.xml | sed 's#/desc.xml##' | sort -u`
for g in $directories
do
pushd $g > /dev/null 2>&1
g=`pwd`
g=${g##*$prefix/}
popd > /dev/null 2>&1
echo Adding $g
grammars+=( $g )
done
grammars=( $(for g in "${grammars[@]}"; do echo "${g}"; done | sort -u) )
elif [ "$filter" != "all" ]
then
echo Unknown filter $filter
exit 1
fi
echo "Number of grammars before sorting and making unique: ${#grammars[@]}"
grammars=($(echo "${grammars[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
echo "Number of grammars after sorting and making unique: ${#grammars[@]}"
if [ "${#grammars[@]}" -eq 0 ]
then
echo There are no grammars in current directory to test.
exit 0
else
echo Grammars to do are: ${grammars[@]}
fi
if [ "$targets" == "" ]
then
targets=( CSharp )
fi
echo grammars = ${grammars[@]}
echo targets = ${targets[@]}
echo order = $order
echo filter = $filter
# Compute cross product
for g in ${grammars[@]}
do
for t in ${targets[@]}
do
tests+=( "$g,$t" )
done
done
# Sort the list of <grammar, target> tuples if needed.
if [[ "$order" == "grammars" ]]; then sorted="1"; else sorted="2"; fi
#tests=`echo $tests | fmt -w 1 | sort -t ',' -k "$sorted"`
echo Tests now ${tests[@]}
# Perform tests in order.
for test in ${tests[@]}
do
all=( $(echo $test | tr "," "\n") )
testname=${all[0]}
target=${all[1]}
pushd "$prefix/$testname" > /dev/null
echo ""
echo "$testname,$target:"
if [ ! -f desc.xml ]
then
echo No desc.xml for $testname
popd > /dev/null
continue
fi
desc_targets=`dotnet trxml2 -- desc.xml | grep '/desc/targets'`
if [ "${PIPESTATUS[0]}" -ne 0 ]
then
echo "The desc.xml for $testname is malformed. Skipping."
popd > /dev/null
continue
fi
desc_targets="${desc_targets##*=}"
desc_targets=`echo "$desc_targets" | tr ',' ' ' | tr ';' ' '`
yes=false;
for t in $desc_targets
do
if [ "$t" == "+all" ]; then yes=true; fi
if [ "$t" == "-$target" ]; then yes=false; fi
if [ "$t" == "$target" ]; then yes=true; fi
done
if [ "$yes" == "false" ]
then
echo "Intentionally skipping grammar $testname target $target."
popd > /dev/null
continue
fi
if [ "$filter" == "agnostic" ]
then
# Test whether the grammars have actions.
count=`dotnet trparse -- -t antlr4 *.g4 2> /dev/null | dotnet trxgrep -- ' //(actionBlock | argActionBlock)' | dotnet trtext -- -c`
if [ "$count" == "0" ]
then
echo "no actions => skipping $testname."
popd > /dev/null
continue
fi
fi
# Generate driver source code.
if [ $quiet != "true" ]; then echo "Generating driver for $testname."; fi
bad=`dotnet trgen -- -t "$target" --template-sources-directory "$full_path_templates" --antlr-tool-path $antlr4jar 2> /dev/null`
for i in $bad; do failed+=( "$testname/$target" ); done
for d in `echo Generated-$target-* Generated-$target`
do
if [ ! -d $d ]; then continue; fi
if [ ! -f $d/build.sh ]; then echo " no build.sh"; continue; fi
# Build driver code.
if [ $quiet != "true" ]; then echo "Building."; fi
pushd $d > /dev/null
date1=$(date +"%s")
bash build.sh > output.txt 2>&1
status="$?"
date2=$(date +"%s")
DIFF=$(($date2-$date1))
length=" Build "`thetime $DIFF`
if [ "$status" != "0" ]
then
echo ""
echo ========
echo "Build of $testname for $target failed."
cat output.txt
failed+=( "$testname/$target" )
echo ========
popd > /dev/null
continue
else
echo "$length"
fi
# Test generated parser on examples.
if [ $quiet != "true" ]; then echo "Parsing."; fi
date1=$(date +"%s")
bash test-cover.sh
status="$?"
date2=$(date +"%s")
DIFF=$(($date2-$date1))
length=" Test "`thetime $DIFF`
echo "$length"
if [ "$status" != "0" ]
then
echo ""
failed+=( "$testname/$target" )
else
echo " Succeeded."
succeeded+=( "$testname,$target" )
fi
popd > /dev/null
done
popd > /dev/null
done
if [[ "$failed" == "" ]]
then
exit 0
else
exit 1
fi