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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
|
########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1994-2011 AT&T Intellectual Property #
# and is licensed under the #
# Eclipse Public License, Version 1.0 #
# by AT&T Intellectual Property #
# #
# A copy of the License is available at #
# http://www.eclipse.org/org/documents/epl-v10.html #
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
# #
# Information and Software Systems Research #
# AT&T Research #
# Florham Park NJ #
# #
# Glenn Fowler <gsf@research.att.com> #
# #
########################################################################
: mktest - generate regress or shell regression test scripts
command=mktest
stdin=8
stdout=9
PREFIX=test
STYLE=regress
WIDTH=80
eval "exec $stdout>&1"
case $(getopts '[-][123:xyz]' opt --xyz 2>/dev/null; echo 0$opt) in
0123) ARGV0="-a $command"
USAGE=$'
[-?
@(#)$Id: mktest (AT&T Labs Research) 2010-08-11 $
]
'$USAGE_LICENSE$'
[+NAME?mktest - generate a regression test scripts]
[+DESCRIPTION?\bmktest\b generates regression test scripts from test
template commands in the \aunit\a.\brt\b file. The generated test
script writes temporary output to '$PREFIX$'\aunit\a.tmp and compares
it to the expected output in '$PREFIX$'\aunit\a.out. Run the test
script with the \b--accept\b option to (re)generate the
'$PREFIX$'\aunit\a.out.]
[s:style?The script style:]:[style:='$STYLE$']
{
[+regress?\bregress\b(1) command input.]
[+shell?Standalone test shell script.]
}
[w:width?Set the output format width to approximately
\awidth\a.]:[width:='$WIDTH$']
unit.rt [ unit [ arg ... ] ]
[+INPUT FILES?The regression test command file \aunit\a\b.rt\b is a
\bksh\b(1) script that makes calls to the following functions:]
{
[+DATA \afile\a [ - | [ options ]] data]]?Create input data
\afile\a that is empty (-) or contains \adata\a subject to
\bprint\b(1) \aoptions\a or that is a copy of the DATA command
standard input. Set \afile\a to \b-\b to name the standard
input.]
[+DIAGNOSTICS?Diagnostic messages of unspecified format are
expected.]
[+DO \acommand\a [ \aarg\a ... ]]?Execute \acommand\a if the
current test is active.]
[+EXEC [ \aarg\a ... ]]?Run the command under test with
optional arguments. If the standard input is not specified then
the standard input of the previous EXEC is used. The standard
input of the first EXEC in a TEST group is an empty regular
file.]
[+EXPORT \aname\a=\avalue\a ...?Export list for subsequent
commands in the TEST group or for all TEST groups if before
the first TEST group.]
[+IGNORESPACE [ 0 | 1 ]
?Ignore space differences when comparing expected output.]
[+KEEP \apattern\a ...?File match patterns of files to retain
between TEST groups.]
[+NOTE \acomment\a?\acomment\a is added to the current test
script.]
[+PROG \acommand\a [ \aarg\a ... ]]?Run \acommand\a with
optional arguments.]
[+TEST [ \anumber\a ]] [ \adescription\a ... ]]?Define a new
test group with optional \anumber\a and \adescripion\a.]
[+TWD [ \adir\a ... ]]?Set the temporary test dir to \adir\a.
The default is \aunit\a\b.tmp\b, where \aunit\a is the test
input file sans directory and suffix. If \adir\a matches \b/*\b
then it is the directory name; if \adir\a is non-null then the
prefix \b${TMPDIR:-/tmp}\b is added; otherwise if \adir\a is
omitted then
\b${TMPDIR:-/tmp}/tst-\b\aunit\a-$$-$RANDOM.\b\aunit\a is
used.]
[+UMASK [ \amask\a ]]?Run subsequent tests with \bumask\b(1)
\amask\a. If \amask\a is omitted then the original \bumask\b is
used.]
[+UNIT \acommand\a [ \aarg\a ... ]]?Define the command and
optional default arguments to be tested. \bUNIT\b explicitly
overrides the default command name derived from the test script
file name.]
[+WIDTH \awidth\a?Set the output format width to approximately
\awidth\a.]
}
[+SEE ALSO?\bregress\b(1), \bksh\b(1)]
'
;;
*) ARGV0=""
USAGE='s: unit.rt [ arg ... ]'
;;
esac
typeset ARG SCRIPT UNIT TEMP=${TMPDIR:-/tmp}/$command.$$.tmp WORK
typeset IO INPUT INPUT_N OUTPUT OUTPUT_N ERROR ERROR_N KEEP
typeset -C STATE
typeset -A DATA STATE.RESET REMOVE FORMAT
integer KEEP_UNIT=0 SCRIPT_UNIT=0 TEST=0 CODE=0 EXIT=0 ACCEPT=0 DIAGNOSTICS=0 code
while getopts $ARGV0 "$USAGE" OPT
do case $OPT in
s) case $OPTARG in
regress|shell)
STYLE=$OPTARG
;;
*) print -u2 -r -- $command: --style=$OPTARG: regress or shell expected
exit 1
;;
esac
;;
w) WIDTH=$OPTARG
;;
*) OPTIND=0
getopts $ARGV0 "$USAGE" OPT '-?'
exit 2
;;
esac
done
shift $OPTIND-1
typeset SINGLE= quote='%${SINGLE}..${WIDTH}q'
if [[ $1 == - ]]
then shift
fi
if (( ! $# ))
then
print -u2 -r -- $command: test command script path expected
exit 1
fi
SCRIPT=$1
shift
if [[ ! -r $SCRIPT ]]
then print -u2 -r -- $command: $SCRIPT: cannot read
exit 1
fi
(ulimit -c 0) >/dev/null 2>&1 && ulimit -c 0
if (( $# ))
then set -A UNIT -- "$@"
KEEP_UNIT=1
else ARG=${SCRIPT##*/}
set -A UNIT -- "${ARG%.*}"
fi
WORK=${UNIT[0]}.tmp
rm -rf $WORK
mkdir $WORK || exit
export PATH=$PWD:$PATH
function LINE
{
if [[ $STYLE == regress ]]
then print -u$stdout
fi
}
function NOTE
{
case $STYLE in
regress)LINE
print -u$stdout -r -- '#' "$@"
;;
shell) print -u$stdout -r -f ": $QUOTE"$'\n' -- "$*"
;;
esac
}
function UNIT
{
(( KEEP_UNIT )) || set -A UNIT -- "$@"
case $STYLE in
regress)LINE
print -u$stdout -r -f $'UNIT'
for ARG in "$@"
do print -u$stdout -r -f " $QUOTE" -- "$ARG"
done
print -u$stdout
;;
shell) print -u$stdout -r -f $'set x'
for ARG in "$@"
do print -u$stdout -r -f " $QUOTE" -- "$ARG"
done
print -u$stdout
print -u$stdout shift
;;
esac
}
function TEST
{
typeset i
typeset -A REM
if (( ${#STATE.RESET[@]} ))
then unset ${!STATE.RESET[@]}
case $STYLE in
shell) print -u$stdout -r -- unset ${!STATE.RESET[@]} ;;
esac
unset STATE.RESET
typeset -A STATE.RESET
fi
if (( ${#REMOVE[@]} ))
then rm -f -- "${!REMOVE[@]}"
case $STYLE in
shell) print -u$stdout -r -f $'rm -f'
for i in ${!REMOVE[@]}
do print -u$stdout -r -f " $QUOTE" "$i"
done
print -u$stdout
;;
esac
for i in ${!REMOVE[@]}
do unset REMOVE[$i]
done
fi
rm -rf $WORK/*
if [[ $1 == +([0-9]) ]]
then TEST=${1##0}
shift
else ((TEST++))
fi
LINE
case $STYLE in
regress)print -u$stdout -r -f "TEST %02d $QUOTE"$'\n' -- $TEST "$*"
;;
shell) print -u$stdout -r -f ": TEST %02d $QUOTE"$'\n' -- $TEST "$*"
;;
esac
: > $TEMP.INPUT > $TEMP.in
INPUT=
INPUT_N=
OUTPUT=
OUTPUT_N=
ERROR=
ERROR_N=
UMASK=$UMASK_ORIG
UMASK_DONE=$UMASK
CODE=0
}
function TWD
{
case $STYLE in
regress)LINE
print -u$stdout -r -f $'TWD'
for ARG in "$@"
do print -u$stdout -r -f " $QUOTE" -- "$ARG"
done
print -u$stdout
;;
esac
}
function RUN
{
typeset i n p op unit sep output=1 error=1 exitcode=1
op=$1
shift
while :
do case $1 in
++NOOUTPUT) output= ;;
++NOERROR) error= ;;
++NOEXIT) exitcode= ;;
++*) print -u2 -r -- $command: $0: $1: unknown option; exit 1 ;;
*) break ;;
esac
shift
done
if [[ $op == PROG ]]
then unit=$1
shift
elif (( ! ${#UNIT[@]} ))
then print -u2 -r -- $command: $SCRIPT: UNIT statement or operand expected
exit 1
fi
LINE
case $STYLE in
regress)if [[ $op == PROG ]]
then print -u$stdout -r -f $'\t'"$op"$'\t'"$unit"
sep=$' '
else print -u$stdout -r -f $'\t'"$op"
sep=$'\t'
fi
for ARG in "$@"
do LC_CTYPE=C print -u$stdout -r -f "$sep$QUOTE" -- "$ARG"
sep=$' '
done
print -u$stdout
[[ ${DATA[-]} || /dev/fd/0 -ef /dev/fd/$stdin ]] || cat > $TEMP.in
IO=$(cat $TEMP.in; print :)
if [[ $IO == ?*$'\n:' ]]
then IO=${IO%??}
n=
else IO=${IO%?}
n=-n
fi
{
[[ $UMASK != $UMASK_ORIG ]] && umask $UMASK
cd $WORK
if [[ $op == PROG ]]
then "$unit" "$@"
code=$?
else "${UNIT[@]}" "$@"
code=$?
fi
cd ..
[[ $UMASK != $UMASK_ORIG ]] && umask $UMASK_ORIG
} < $TEMP.in > $TEMP.out 2> $TEMP.err
if [[ $IO != "$INPUT" || $n != "$INPUT_N" ]]
then INPUT=$IO
INPUT_N=$n
if [[ ${FORMAT[-]} ]]
then print -u$stdout -n -r -- $'\t\tINPUT'
print -u$stdout -r -f " $QUOTE" -- "${FORMAT[-]}"
print -u$stdout -r -f " $QUOTE" -- -
unset FORMAT[-]
else print -u$stdout -n -r -- $'\t\tINPUT' $n -
[[ $IO ]] && LC_CTYPE=C print -u$stdout -r -f " $QUOTE" -- "$IO"
fi
print -u$stdout
unset DATA[-]
fi
for i in ${!DATA[@]}
do if [[ ${FORMAT[$i]} ]]
then print -u$stdout -n -r -- $'\t\tINPUT'
print -u$stdout -r -f " $QUOTE" -- "${FORMAT[$i]}"
print -u$stdout -r -f " $QUOTE" -- "$i"
unset FORMAT[$i]
else case $i in
-) p=$TEMP.in ;;
*) p=$WORK/$i ;;
esac
IO=$(cat $p; print :)
if [[ $IO == ?*$'\n:' ]]
then IO=${IO%??}
n=
else IO=${IO%?}
n=-n
fi
print -u$stdout -n -r -- $'\t\tINPUT' $n
print -u$stdout -r -f " $QUOTE" -- "$i"
[[ $IO ]] && LC_CTYPE=C print -u$stdout -r -f " $QUOTE" -- "$IO"
fi
print -u$stdout
unset DATA[$i]
done
IO=$(cat $TEMP.out; print :)
if [[ $IO == ?*$'\n:' ]]
then IO=${IO%??}
n=
else IO=${IO%?}
n=-n
fi
if [[ $IO != "$OUTPUT" || $n != "$OUTPUT_N" ]]
then OUTPUT=$IO
OUTPUT_N=$n
if [[ $output ]]
then if [[ ! -s $TEMP.out ]]
then print -u$stdout -n -r -- $'\t\tOUTPUT' -
elif cmp -s $TEMP.in $TEMP.out
then OUTPUT=not-$OUTPUT
print -u$stdout -n -r -- $'\t\tSAME OUTPUT INPUT'
else print -u$stdout -n -r -- $'\t\tOUTPUT' $n -
[[ $IO ]] && LC_CTYPE=C print -u$stdout -r -f " $QUOTE" -- "$IO"
fi
print -u$stdout
fi
fi
IO=$(cat $TEMP.err; print :)
IO=${IO//$command\[*([0-9])\]:\ .\[*([0-9])\]:\ @(EXEC|PROG)\[*([0-9])\]:\ /}
if [[ $IO == ?*$'\n:' ]]
then IO=${IO%??}
n=
else IO=${IO%?}
n=-n
fi
if [[ $IO != "$ERROR" || $n != "$ERROR_N" ]]
then ERROR=$IO
ERROR_N=$n
if [[ $error ]]
then print -u$stdout -n -r -- $'\t\tERROR' $n -
[[ $IO ]] && LC_CTYPE=C print -u$stdout -r -f " $QUOTE" -- "$IO"
print -u$stdout
fi
fi
case $output:$error in
:) OUTPUT=
OUTPUT_N=
ERROR=
ERROR_N=
print -u$stdout -r -- $'\t\tIGNORE OUTPUT ERROR'
;;
:1) OUTPUT=
OUTPUT_N=
print -u$stdout -r -- $'\t\tIGNORE OUTPUT'
;;
1:) ERROR=
ERROR_N=
print -u$stdout -r -- $'\t\tIGNORE ERROR'
;;
esac
if [[ $UMASK_DONE != $UMASK ]]
then UMASK_DONE=$UMASK
print -u$stdout -r -f $'\t\tUMASK %s\n' $UMASK
fi
if (( code != CODE ))
then (( CODE=code ))
if [[ $exitcode ]]
then print -u$stdout -r -f $'\t\tEXIT %d\n' $CODE
fi
fi
;;
shell) [[ $UMASK != $UMASK_ORIG ]] && print -u$stdout -r -f "{ umask $UMASK; "
if [[ $op == PROG ]]
then print -u$stdout -r -f $'"'"$unit"$'"'
else print -u$stdout -r -f $'"$@"'
fi
for ARG in "$@"
do print -u$stdout -r -f " $QUOTE" -- "$ARG"
done
[[ $UMASK != $UMASK_ORIG ]] && print -u$stdout -r -f "umask $UMASK_ORIG; } "
if [[ ! $output ]]
then print -u$stdout -r -f " >/dev/null"
fi
if [[ ! $error ]]
then if [[ ! $output ]]
then print -u$stdout -r -f " 2>&1"
else print -u$stdout -r -f " 2>/dev/null"
fi
fi
IO=$(cat)
if [[ $IO ]]
then print -u$stdout -r -- "<<'!TEST-INPUT!'"
print -u$stdout -r -- "$IO"
print -u$stdout -r -- !TEST-INPUT!
else print -u$stdout
fi
if [[ $exitcode ]]
then print -u$stdout -r -- $'CODE=$?\ncase $CODE in\n0) ;;\n*) echo exit status $CODE ;;\nesac'
fi
;;
esac
}
function DO
{
LINE
print -r $'\t'DO "$@"
}
function EXEC
{
RUN EXEC "$@"
}
function DATA
{
typeset f p o
f=$1
shift
case $f in
-) p=$TEMP.in ;;
*) p=$WORK/$f ;;
esac
case $1 in
'') cat ;;
-) ;;
*) print -r "$@" ;;
esac > $p
DATA[$f]=1
if (( $# == 1 )) && [[ $1 == -?* ]]
then FORMAT[$f]=$1
else FORMAT[$f]=
fi
if [[ $f != $KEEP ]]
then REMOVE[$f]=1
fi
if [[ $STYLE == shell ]]
then {
print -r -f "cat > $QUOTE <<'!TEST-INPUT!'"$'\n' -- "$f"
cat "$p"
print -r -- !TEST-INPUT!
} >&$stdout
fi
}
function KEEP
{
typeset p
for p
do if [[ $KEEP ]]
then KEEP=$KEEP'|'
fi
KEEP=$KEEP$p
done
}
function DIAGNOSTICS
{
LINE
case $STYLE in
regress) print -u$stdout -r $'DIAGNOSTICS' ;;
shell) DIAGNOSTICS=1 ;;
esac
}
function EXPORT
{
typeset x n v
LINE
case $STYLE in
regress) print -u$stdout -r -f $'EXPORT' ;;
shell) print -u$stdout -r -f $'export' ;;
esac
for x
do n=${x%%=*}
v=${x#*=}
export "$x"
print -u$stdout -r -f " %s=$QUOTE" "$n" "$v"
(( TEST )) && STATE.RESET["$n"]=1
done
print -u$stdout
}
function PROG
{
RUN PROG "$@"
}
function WIDTH
{
WIDTH=${1:-80}
eval QUOTE='"'$quote'"'
}
function IGNORESPACE
{
IGNORESPACE=-b
LINE
print -u$stdout -r IGNORESPACE
}
function UMASK # [ mask ]
{
[[ $UMASK_ORIG ]] || UMASK_ORIG=$(umask)
UMASK=$1
[[ $UMASK ]] || UMASK=$UMASK_ORIG
}
trap 'CODE=$?; rm -rf $TEMP.* $WORK; exit $CODE' 0 1 2 3 15
typeset IGNORESPACE UMASK UMASK_ORIG UMASK_DONE
UMASK_ORIG=$(umask)
IFS=$IFS$'\n'
print -u$stdout -r "# : : generated from $SCRIPT by $command : : #"
case $STYLE in
shell) cat <<!
ACCEPT=0
while :
do case \$1 in
-a|--accept)
ACCEPT=1
;;
--help|--man)
cat 1>&2 <<!!
Usage: \\\$SHELL $PREFIX${UNIT[0]}.sh [ --accept ] [ unit ... ]
${UNIT[0]} regression test script. Run this script to generate new
results in $PREFIX${UNIT[0]}.tmp and compare with expected results in
$PREFIX${UNIT[0]}.out. The --accept option generates $PREFIX${UNIT[0]}.tmp
and moves it to $PREFIX${UNIT[0]}.out.
!!
exit 2
;;
-*) echo \$0: \$1: invalid option >&2
exit 1
;;
*) break
;;
esac
shift
done
export COLUMNS=80
{
!
;;
esac
export COLUMNS=80
case $STYLE in
shell) SINGLE='#'
eval QUOTE='"'$quote'"'
. $SCRIPT < /dev/null | sed -e $'s,\\\\n,\n,g' -e $'s,\\\\t,\t,g' -e $'s,\\$\',\',g'
;;
*) eval QUOTE='"'$quote'"'
: > $TEMP.INPUT > $TEMP.in
eval "exec $stdin<$TEMP.INPUT"
. $SCRIPT <&$stdin
;;
esac
case $STYLE in
shell) cat <<!
} > $PREFIX${UNIT[0]}.tmp 2>&1 < /dev/null
case \$ACCEPT in
0) if grep '
$' $PREFIX${UNIT[0]}.tmp >/dev/null
then mv $PREFIX${UNIT[0]}.tmp $PREFIX${UNIT[0]}.junk
sed 's/
$//' < $PREFIX${UNIT[0]}.junk > $PREFIX${UNIT[0]}.tmp
rm -f $PREFIX${UNIT[0]}.junk
fi
if cmp -s $PREFIX${UNIT[0]}.tmp $PREFIX${UNIT[0]}.out
then echo ${UNIT[0]} tests PASSED
rm -f $PREFIX${UNIT[0]}.tmp
else echo ${UNIT[0]} tests FAILED
diff $IGNORESPACE $PREFIX${UNIT[0]}.tmp $PREFIX${UNIT[0]}.out
fi
;;
*) mv $PREFIX${UNIT[0]}.tmp $PREFIX${UNIT[0]}.out
;;
esac
!
;;
esac
|