-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunTests.sh
executable file
·534 lines (447 loc) · 10 KB
/
runTests.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/bin/bash
# xmllint core dumps, on certain schemas, dont use it in tests
XMLLINT=`which xmllint`
TEST_FAILED=false
FAILED_DIRS=""
INPUT_XSD=""
EX_DIRS=""
W3C_TESTS_DIRS=""
XPLUS_TESTS_DIRS=""
XPLUS_NEGTESTS_DIRS=""
ascertain_test_dirs()
{
EX_DIRS=`find examples -maxdepth 1 -mindepth 1 -type d | grep -v "svn*"`
W3C_TESTS_DIRS=`find Tests/w3c_tests -maxdepth 1 -mindepth 1 -type d | grep -v "svn*"`
XPLUS_TESTS_DIRS=`find Tests/xplus_tests -maxdepth 1 -mindepth 1 -type d | grep -v "svn*"`
XPLUS_NEGTESTS_DIRS=`find Tests/xplus_neg_tests -maxdepth 1 -mindepth 1 -type d | grep -v "svn*"`
TESTS_DIRS="$EX_DIRS $W3C_TESTS_DIRS $XPLUS_TESTS_DIRS"
NEG_TESTS_DIRS="$XPLUS_NEGTESTS_DIRS"
ALL_DIRS="$TESTS_DIRS $NEG_TESTS_DIRS"
}
change_dir_abort()
{
cd $dir
if [ $? -ne 0 ]; then
echo "failed to change-dir: $dir" >> $LOG_FILE
exit 2
fi
}
get_INPUT_XSD()
{
INPUT_XSD=`ls -1 *.xsd`
cnt_xsds=`echo "$INPUT_XSD" | wc -l | sed -e 's/ *//g'`
if [ $cnt_xsds -ne 1 ]; then
if [ -f README ]; then
INPUT_XSD=`cat README | grep INPUT_XSD | cut -d= -f2 | sed -e 's/ *//g'`
if [ -z "$INPUT_XSD" ]; then
echo "unable to ascertain INPUT_XSD, exiting..."
fail_test
fi
else
echo "unable to ascertain INPUT_XSD in dir:$dir, exiting..."
fail_test
fi
fi
}
log_tests_dir()
{
LOG_FILE_DIR=$TEST_REPORT_DIR/logs
mkdir -p $LOG_FILE_DIR
LOG_FILE=$LOG_FILE_DIR/`basename $dir`.log
echo $dir >> $LOCK_FILE
}
fail_test()
{
TEST_FAILED=true
echo $dir >> $FAILED_REPORT
}
pass_test()
{
if [ $TEST_FAILED != 'true' ]; then
echo $dir >> $PASSED_REPORT
fi
}
cleanup_dir()
{
change_dir_abort
find . | grep -v svn | grep -v README | grep -v xsd | grep -v xml | grep -v testme | grep -v "main.cpp" | xargs rm -rf 2>/dev/null
rm -f *.template *.bak t.xml* *.xml.rt.xml *.xml.row.xml sample.xml *.save README.build.txt
cd - > /dev/null 2>&1
}
cleanup()
{
echo
echo " ========================= WARNING ============================"
echo " Requested execution will cleanup many files recursively inside"
echo " certain directories, so that any of user added files and edits"
echo " may get lost. If you think you have added/edited important "
echo " changes inside these directories, please back them up, before"
echo " proceeding."
echo
echo " Following directories would get cleaned up recursively:"
echo " * Tests/ "
echo " * examples/"
echo
echo -n "Do you want to continue [y/N]? "
read ans
if [ "$ans" != 'y' ]; then
echo " => aborting the execution..."
echo
exit 2
fi
echo
echo
echo "=> Starting..."
echo "=> Cleaning..."
for dir in $TESTS_DIRS
do
cleanup_dir
done
}
test_valid()
{
if [ $TEST_FAILED = 'true' ]; then
return
fi
validXmlFiles=`ls valid*.xml 2>/dev/null`
# validate valid.xml
for xmlValid in $validXmlFiles
do
./build/bin/$run -v $xmlValid >> $LOG_FILE 2>&1
if [ $? -ne 0 ]; then
echo " failed to validate valid xml file: $xmlValid" >> $LOG_FILE
fail_test
return
fi
done
#if [ ! -z "$XMLLINT" ]; then
# $XMLLINT --noout --schema $INPUT_XSD valid.xml > /dev/null 2>&1
# if [ $? -ne 0 ]; then
# echo " failed to validate valid.xml using xmllint"
# #fail_test
# #return
# fi
#fi
}
test_invalid()
{
if [ $TEST_FAILED = 'true' ]; then
return
fi
invalidXmlFiles=`ls invalid*.xml 2>/dev/null`
# invalidate invalid[N].xml
for xmlInvalid in $invalidXmlFiles
do
./build/bin/$run -v $xmlInvalid >> $LOG_FILE 2>&1
if [ $? -eq 0 ]; then
echo " failed to invalidate invalid xml file: $xmlInvalid" >> $LOG_FILE
fail_test
return
fi
done
}
# 1 testcase
test_build()
{
if [ $TEST_FAILED = 'true' ]; then
return
fi
# verify that the dir builds
xsd2cpp $INPUT_XSD . >> $LOG_FILE 2>&1 && ./autogen.sh >> $LOG_FILE 2>&1 && make install >> $LOG_FILE 2>&1
if [ $? -ne 0 ]; then
echo " failed to build" >> $LOG_FILE
fail_test
return
fi
}
# 1 testcase
test_sample()
{
if [ $TEST_FAILED = 'true' ]; then
return
fi
# write sample.xml
./build/bin/$run -s >> $LOG_FILE 2>&1
# check sample.xml exists
if [ ! -f sample.xml ]; then
echo " failed to write sample.xml" >> $LOG_FILE
fail_test
return
fi
#./build/bin/$run -v sample.xml >> $LOG_FILE 2>&1
#if [ $? -ne 0 ]; then
# echo " failed to validate file: sample.xml"
# fail_test
# return
#fi
}
# 2 testcases
test_write()
{
if [ $TEST_FAILED = 'true' ]; then
return
fi
# write t.xml
./build/bin/$run -w >> $LOG_FILE 2>&1
# check t.xml exists
if [ ! -f t.xml ]; then
echo " failed to write t.xml" >> $LOG_FILE
fail_test
return
fi
# verify diff
#differ=`diff t.xml valid.xml`
#if [ ! -z "$differ" ]; then
# echo " failed to compare t.xml with valid.xml"
# fail_test
# return
#fi
}
# 2 testcases
test_roundtrip()
{
if [ $TEST_FAILED = 'true' ]; then
return
fi
# rountrip
validXmlFiles=`ls valid*.xml` 2>/dev/null
for xmlValid in $validXmlFiles
do
./build/bin/$run -r $xmlValid >> $LOG_FILE 2>&1
# check xyz.xml.rt.xml exists
if [ ! -f $xmlValid.rt.xml ]; then
echo " failed to roundtrip input file [$xmlValid] to write roundtripped file [$xmlValid.rt.xml]" >> $LOG_FILE
fail_test
return
fi
done
# ROW: rountrip with callback
for xmlValid in $validXmlFiles
do
./build/bin/$run -u $xmlValid >> $LOG_FILE 2>&1
# check xyz.xml.row.xml exists
if [ ! -f $xmlValid.row.xml ]; then
echo " failed to roundtrip input file [$xmlValid] to write roundtripped file [$xmlValid.row.xml]" >> $LOG_FILE
fail_test
return
fi
done
# verify diff
#differ=`diff t.xml.rt.xml t.xml`
#if [ ! -z "$differ" ]; then
# echo " failed to compare t.xml.rt.xml with t.xml"
# fail_test
# return
#fi
}
neg_test_dir()
{
echo
cd $dir
log_tests_dir
get_INPUT_XSD
xsd2cpp $INPUT_XSD . >> $LOG_FILE 2>&1
if [ $? -eq 0 ]; then
echo " failed because xsd2cpp succeeded" >> $LOG_FILE
fail_test
return
fi
pass_test
cd - >/dev/null 2>&1
}
# several testcases are run in each test directory
# this functions does all the tests to be done, inside
# a particular test directory
test_dir()
{
cd $dir
log_tests_dir
if [ -f testme ]; then
echo " ->running custom tests" >> $LOG_FILE
./testme >> $LOG_FILE 2>&1
if [ $? -ne 0 ]; then
fail_test
fi
else
get_INPUT_XSD
run=`basename $INPUT_XSD | cut -d'.' -f1 |sed -e 's/-/_/g'`run
#run=`basename $INPUT_XSD | cut -d'.' -f1`run
TEST_FAILED=false
test_build
test_valid
test_invalid
#test_sample
test_write
test_roundtrip
fi
pass_test
cd - >/dev/null 2>&1
}
wait_for_lock()
{
while [ -f $LOCK_FILE ]
do
sleep 1
done
}
# need not be atomic, as it is 1) based on understanding within this script 2) lock violations causes no loss
acquire_lock()
{
wait_for_lock
touch $LOCK_FILE
}
release_lock()
{
rm -f $LOCK_FILE
}
wait_for_children_to_finish()
{
for job in `jobs -p`
do
wait $job || let "FAIL+=1"
done
}
test_dirs_in_buffer()
{
acquire_lock
for dir in $DIR_BUFFER_4_TEST
do
if [ $NEG = 0 ]; then
echo "./runTests.sh -p $dir & "
./runTests.sh -p $dir &
else
echo "./runTests.sh -n $dir & "
./runTests.sh -n $dir &
fi
done
wait_for_children_to_finish
release_lock
}
test_all()
{
echo "=> Running tests ..."
echo
DIR_BUFFER_4_TEST=""
count=0
# ----------------
for dir in $TESTS_DIRS
do
count=`expr $count + 1`
DIR_BUFFER_4_TEST="$DIR_BUFFER_4_TEST $dir"
if [ $count -eq 2 ]; then
test_dirs_in_buffer
count=0
DIR_BUFFER_4_TEST=""
fi
done
# for the odd one
if [ $count -eq 1 ]; then
test_dirs_in_buffer
count=0
DIR_BUFFER_4_TEST=""
fi
# ----------------
DIR_BUFFER_4_TEST=""
count=0
NEG=1
NEG_TESTS_DIRS="Tests/xplus_neg_tests/scRest Tests/xplus_neg_tests/scRest2"
for dir in $NEG_TESTS_DIRS
do
count=`expr $count + 1`
DIR_BUFFER_4_TEST="$DIR_BUFFER_4_TEST $dir"
if [ $count -eq 2 ]; then
test_dirs_in_buffer
count=0
DIR_BUFFER_4_TEST=""
fi
done
# for the odd one
if [ $count -eq 1 ]; then
test_dirs_in_buffer
count=0
DIR_BUFFER_4_TEST=""
fi
# ----------------
wait_for_children_to_finish
for job in `jobs -p`
do
wait $job || let "FAIL+=1"
done
echo "==========================================================="
echo
echo " ................ [FAILED] ............... "
cat $FAILED_REPORT
echo
echo
echo
echo " ................ [PASSED] ............... "
cat $PASSED_REPORT
echo
echo "==========================================================="
rm -f $LOCK_FILE
}
init_reports()
{
rm -f $LOCK_FILE
rm -rf $TEST_REPORT_DIR
mkdir -p $TEST_REPORT_DIR
>$FAILED_REPORT
>$PASSED_REPORT
}
print_usage()
{
echo
echo "Usage:"
echo "$ `basename $0` [-c | -t ]"
echo " -c cleanup all the test directories"
echo " -t cleanup and test all the test directories"
echo " -h print help"
echo
echo " (test directories include: Tests/ examples/) "
echo
exit 2
}
#-----------------------------------------------------------------
# MAIN
PID=$$
FAIL=0
TEST_REPORT_DIR=`pwd`/.TESTS
LOCK_FILE=$TEST_REPORT_DIR/xmlplus.lock
FAILED_REPORT=$TEST_REPORT_DIR/failed.txt
PASSED_REPORT=$TEST_REPORT_DIR/passed.txt
LOG_FILE=""
NEG=0
if [ $# = 0 ]; then
print_usage
exit 2
fi
while getopts "chtp:n:" op;
do
case "${op}" in
h)
print_usage
shift;break;;
c)
ascertain_test_dirs
cleanup
shift;break;;
p)
dir=$OPTARG
test_dir
shift;break;;
n)
dir=$OPTARG
neg_test_dir
shift;break;;
t)
ascertain_test_dirs
cleanup
init_reports
test_all
shift;break;;
*)
print_usage
shift; break;;
esac
done
shift $((OPTIND-1))