File: run_tests.sh

package info (click to toggle)
pcb 20140316-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 22,212 kB
  • ctags: 16,012
  • sloc: ansic: 123,955; sh: 7,306; yacc: 5,087; pascal: 4,118; makefile: 1,559; perl: 552; lex: 438; awk: 157; lisp: 86; tcl: 63; xml: 20
file content (684 lines) | stat: -rwxr-xr-x 16,876 bytes parent folder | download | duplicates (2)
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
#!/bin/sh
#
# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2009, 2010 Dan McMahill

#  This program is free software; you can redistribute it and/or modify
#  it under the terms of version 2 of the GNU General Public License as
#  published by the Free Software Foundation
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
# All rights reserved.
#
# This code was derived from code written by Dan McMahill as part of the
# latex-mk testsuite.  The original code was covered by a BSD license
# but the copyright holder (Dan McMahill) has re-released the code under
# the GPL.

magic_test_skip=${PCB_MAGIC_TEST_SKIP:-no}

if test "x${magic_test_skip}" = "xyes" ; then
	cat << EOF

The environment variable PCB_MAGIC_TEST_SKIP is set to yes.
This causes the testsuite to skip all tests and report no errors.
This is used for certain debugging *only*.  The primary use is to 
allow testing of the 'distcheck' target without including the effects
of the testsuite. The reason this is useful is that due to minor differences
in library versions and perhaps roundoff in different CPU's, the testsuite
may falsely report failures on some systems.  These reported failures
prevent using 'distcheck' for verifying the rest of the build system.
These comments only apply to the tests which try to compare image files
like PNG files.

EOF

	exit 0
fi

regen=no

usage() {
cat <<EOF

$0 -- Run pcb regression tests

$0 -h|--help
$0 [-d | --debug] [-g | --golden dir] [-r|--regen] [testname1 [testname2[ ...]]]

OVERVIEW

The $0 script is used both for running the pcb regression testsuite
as well as maintaining it.  The way the test suite works is a number
of different layouts are exported using the various export HID's.

The resulting output files are examined in various ways to make sure
they are correct.  The exact details of how they are compared varies.
For example, the PNG outputs are compared using tools from the ImageMagick
suite while the ASCII centroid and bill of materials files are normalized
with awk and then compared with the standard diff utility.  The normalization
removes things like a comment line which contains the creation date.

OPTIONS

-d | --debug              Enables extra debug output

-g | --golden <dir>    :  Specifies that <dir> should be used for the
                          reference files. 

LIMITATIONS

- The GUI interface is not checked via the regression testsuite.

- Currently actions are also not exercised

EOF
}

show_sep() {
    echo "----------------------------------------------------------------------"
}

##########################################################################
#
# debug print out
#

do_debug=no
debug() {
    if test $do_debug = yes ; then
	cat <<EOF
$*
EOF
    fi
}

##########################################################################
#
# command line processing
#


all_tests=""
while test -n "$1"
  do
  case "$1"
      in
      
      -d|--debug)
	  do_debug=yes
	  shift
	  ;;

      -h|--help)
	  usage
	  exit 0
	  ;;
      
      -g|--golden)
	# set the 'golden' directory.
	  REFDIR="$2"
	  shift 2
	  ;;
      
      -r|--regen)
	# regenerate the 'golden' output files.  Use this with caution.
	# In particular, all differences should be noted and understood.
	  regen=yes
	  shift
	  ;;
      
      -*)
	  echo "unknown option: $1"
	  exit 1
	  ;;
      
      *)
	  break
	  ;;
      
  esac
done

if test "X$regen" = "Xyes" && test $# -ne 1; then
    echo "Please regenerate only one test at a time."
    echo "This limitation is a safety measure."
    exit 1
fi

##########################################################################
#
# set up various tools
#


all_tests="$*"

# Source directory
srcdir=${srcdir:-.}
top_srcdir=${top_srcdir:-.}

# The pcb wrapper script we want to test
#
# by default we will be running it from $(top_builddir)/tests/outputs/testname
# so we need to look 3 levels up and then down to src
PCB=${PCB:-../../../src/pcbtest.sh}

# The gerbv executible 
GERBV=${GERBV:-gerbv}
GERBV_DEFAULT_FLAGS=${GERBV_DEFAULT_FLAGS:---export=png --window=640x480}

# various ImageMagick tools
IM_ANIMATE=${IM_ANIMATE:-animate}
IM_COMPARE=${IM_COMPARE:-compare}
IM_COMPOSITE=${IM_COMPOSITE:-composite}
IM_CONVERT=${IM_CONVERT:-convert}
IM_DISPLAY=${IM_DISPLAY:-display}
IM_MONTAGE=${IM_MONTAGE:-montage}

# golden directories
INDIR=${INDIR:-${srcdir}/inputs}
OUTDIR=outputs
REFDIR=${REFDIR:-${srcdir}/golden}

# some system tools
AWK=${AWK:-awk}

# the list of tests to run
TESTLIST=${srcdir}/tests.list

if test "X$regen" = "Xyes" ; then
    OUTDIR="${REFDIR}"
fi

# create output directory
if test ! -d $OUTDIR ; then
    mkdir -p $OUTDIR
    if test $? -ne 0 ; then
	echo "Failed to create output directory ${OUTDIR}"
	exit 1
    fi
fi

if test -z "$all_tests" ; then
    if test ! -f ${TESTLIST} ; then
	echo "ERROR: ($0)  Test list $TESTLIST does not exist"
	exit 1
    fi
    all_tests=`${AWK} 'BEGIN{FS="|"} /^#/{next} {print $1}' ${TESTLIST} | sed 's; ;;g'`
fi

if test -z "${all_tests}" ; then
    echo "$0:  No tests specified"
    exit 0
fi


# fail/pass/total counts
fail=0
pass=0
skip=0
tot=0

##########################################################################
#
# config summary
#


cat << EOF

srcdir                ${srcdir}
top_srcdir            ${top_srcdir}

AWK                   ${AWK}
PCB                   ${PCB}
INDIR                 ${INDIR}
OUTDIR                ${OUTDIR}
REFDIR                ${REFDIR}
TESTLIST              ${TESTLIST}

Gerbv:

GERBV                 ${GERBV}
GERBV_DEFAULT_FLAGS   ${GERBV_DEFAULT_FLAGS}

ImageMagick Tools:

IM_ANIMATE               ${IM_ANIMATE}
IM_COMPARE               ${IM_COMPARE}
IM_COMPOSITE             ${IM_COMPOSITE}
IM_CONVERT               ${IM_CONVERT}
IM_DISPLAY               ${IM_DISPLAY}
IM_MONTAGE               ${IM_MONTAGE}

EOF

tmpd=/tmp/pcb_tests.$$
mkdir -p -m 0700 $tmpd
rc=$?
if test $rc -ne 0 ; then
    echo "$0:  ERROR:  could not create $tmpd"
    exit 1
fi

##########################################################################
#
# utility functions for comparison
#

# Usage:
#  compare_check "test_name" "file1" "file2"
#
# Makes sure that file1 and file2 both exist.  If not, mark the current
# test as skipped and give an error message
#
compare_check() {
    local fn="$1"
    local f1="$2"
    local f2="$3"

    if test ! -f "$f1" ; then 
	echo "$0:  ${fn}(): $f1 does not exist"
	test_skipped=yes
	return 1
    fi

    if test ! -f "$f2" ; then 
	echo "$0:  ${fn}(): $f2 does not exist"
	test_skipped=yes
	return 1
    fi
    return 0
}

##########################################################################
#
# ASCII file comparison routines
#

# Usage:
#   run_diff "file1" "file2"
#
run_diff() {
    local f1="$1"
    local f2="$2"
    diff -U 2 $f1 $f2
    if test $? -ne 0 ; then return 1 ; fi
    return 0
}

##########################################################################
#
# BOM comparison routines
#

# used to remove things like creation date from BOM files
normalize_bom() {
    local f1="$1"
    local f2="$2"
    $AWK '
	/^# Date:/ {print "# Date: today"; next}
	/^# Author:/ {print "# Author: PCB"; next}
	{print}' \
	$f1 > $f2
}

# top level function to compare BOM output
compare_bom() {
    local f1="$1"
    local f2="$2"
    compare_check "compare_bom" "$f1" "$f2" || return 1

    # an example BOM file is:

    #  # PcbBOM Version 1.0
    #  # Date: Wed Jun 17 14:41:43 2009 UTC
    #  # Author: Dan McMahill
    #  # Title: Basic BOM/XY Test - PCB BOM
    #  # Quantity, Description, Value, RefDes
    #  # --------------------------------------------
    #  8,"Standard SMT resistor, capacitor etc","RESC3216N",R90_TOP R180_TOP R270_TOP R0_TOP R270_BOT R180_BOT R90_BOT R0_BOT 
    #  8,"Dual in-line package, narrow (300 mil)","DIP8",UDIP90_TOP UDIP180_TOP UDIP270_TOP UDIP0_TOP UDIP270_BOT UDIP180_BOT UDIP90_BOT UDIP0_BOT 
    #  8,"Small outline package, narrow (150mil)","SO8",USO90_TOP USO180_TOP USO270_TOP USO0_TOP USO270_BOT USO180_BOT USO90_BOT USO0_BOT 

    #  For comparison, we need to ignore changes in the Date and Author lines.
    local cf1=${tmpd}/`basename $f1`-ref
    local cf2=${tmpd}/`basename $f2`-out

    normalize_bom $f1 $cf1
    normalize_bom $f2 $cf2
    run_diff $cf1 $cf2 || test_failed=yes
}

##########################################################################
#
# X-Y (centroid) comparison routines
#

# used to remove things like creation date from BOM files
normalize_xy() {
    local f1="$1"
    local f2="$2"
    $AWK '
	/^# Date:/ {print "# Date: today"; next}
	/^# Author:/ {print "# Author: PCB"; next}
	{print}' \
	$f1 > $f2
}

compare_xy() {
    local f1="$1"
    local f2="$2"
    compare_check "compare_xy" "$f1" "$f2" || return 1

    local cf1=${tmpd}/`basename $f1`-ref
    local cf2=${tmpd}/`basename $f2`-out
    normalize_xy "$f1" "$cf1"
    normalize_xy "$f2" "$cf2"
    run_diff "$cf1" "$cf2" || test_failed=yes
}

##########################################################################
#
# GCODE comparison routines
#

# used to remove things like creation date from gcode files
normalize_gcode() {
    local f1="$1"
    local f2="$2"
    # matches string such as '( Tue Mar  9 17:45:43 2010 )'
    $AWK --posix '!/^\( *[A-Z][a-z]{2} [A-Z][a-z]{2} [0123 ][0-9] [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4} *\)$/' \
	$f1 > $f2
}

compare_gcode() {
    local f1="$1"
    local f2="$2"
    compare_check "compare_gcode" "$f1" "$f2" || return 1

    #  For comparison, we need to ignore changes in the Date and Author lines.
    local cf1=${tmpd}/`basename $f1`-ref
    local cf2=${tmpd}/`basename $f2`-out

    normalize_gcode $f1 $cf1
    normalize_gcode $f2 $cf2

    run_diff "$cf1" "$cf2" || test_failed=yes
}

##########################################################################
#
# RS274-X and Excellon comparison
#

compare_rs274x() {
    local f1="$1"
    local f2="$2"
    compare_check "compare_rs274x" "$f1" "$f2" || return 1

    # use gerbv to export our reference RS274-X file and our generated
    # RS274-X file to png.  Then we'll use ImageMagick to see
    # if there are any differences in the resulting files
    pngdir=${rundir}/png
    mkdir -p ${pngdir}
    nb=`basename $f1`
    png1=${pngdir}/${nb}-ref.png
    png2=${pngdir}/${nb}-out.png

    debug "${GERBV} ${GERBV_DEFAULT_FLAGS} --output=${png1} ${f1}"
    ${GERBV} ${GERBV_DEFAULT_FLAGS} --output=${png1} ${f1}

    debug "${GERBV} ${GERBV_DEFAULT_FLAGS} --output=${png2} ${f2}"
    ${GERBV} ${GERBV_DEFAULT_FLAGS} --output=${png2} ${f2}

    compare_image ${png1} ${png2}

}

compare_cnc() {
    compare_rs274x $*
}

##########################################################################
#
# GIF/JPEG/PNG comparison routines
#

compare_image() {
    local f1="$1"
    local f2="$2"
    compare_check "compare_image" "$f1" "$f2" || return 1

    # now see if the image files are the same
    debug "${IM_COMPARE} -metric MAE ${f1} ${f2}  null:"
    same=`${IM_COMPARE} -metric MAE ${f1} ${f2}  null: 2>&1 | \
          ${AWK} '{if($1 == 0){print "yes"} else {print "no"}}'`
    debug "compare_image():  same = $same"

    if test "$same" != yes ; then
	test_failed=yes
	echo "FAILED:  See ${errdir}"
	mkdir -p ${errdir}
	${IM_COMPARE} ${f1} ${f2} ${errdir}/compare.png
	${IM_COMPOSITE} ${f1} ${f2} -compose difference ${errdir}/composite.png
	${IM_CONVERT} ${f1} ${f2} -compose difference -composite  -colorspace gray   ${errdir}/gray.png
	cat > ${errdir}/animate.sh << EOF
#!/bin/sh
${IM_CONVERT} -label "%f" ${f1} ${f2} miff:- | \
${IM_MONTAGE} - -geometry +0+0 -tile 1x1 miff:- | \
${IM_ANIMATE} -delay 0.5 -loop 0 -
EOF
	chmod a+x ${errdir}/animate.sh
    fi
}

##########################################################################
#
# The main program loop
#

for t in $all_tests ; do
    show_sep
    echo "Test:  $t"

    tot=`expr $tot + 1`

    ######################################################################
    #
    # extract the details for the test
    #

    pcb_flags="${PCB_DEFAULT_FLAGS}"

    rundir="${OUTDIR}/${t}"
    refdir="${REFDIR}/${t}"
    errdir=${rundir}/mismatch

    # test_name | layout file(s) | [export hid name] | [optional arguments to pcb] |  [mismatch] 
    # | output files
    name=`grep "^[ \t]*${t}[ \t]*|" $TESTLIST | $AWK 'BEGIN{FS="|"} {print $1}'`
    files=`grep "^[ \t]*${t}[ \t]*|" $TESTLIST | $AWK 'BEGIN{FS="|"} {print $2}'`
    hid=`grep "^[ \t]*${t}[ \t]*|" $TESTLIST | $AWK 'BEGIN{FS="|"} {gsub(/[ \t]*/, ""); print $3}'`
    args=`grep "^[ \t]*${t}[ \t]*|" $TESTLIST | $AWK 'BEGIN{FS="|"} {print $4}'`
    mismatch=`grep "^[ \t]*${t}[ \t]*|" $TESTLIST | $AWK 'BEGIN{FS="|"} {if($5 == "mismatch"){print "yes"}else{print "no"}}'`
    out_files=`grep "^[ \t]*${t}[ \t]*|" $TESTLIST | $AWK 'BEGIN{FS="|"} {print $6}'`
   
    if test "X${name}" = "X" ; then
	echo "ERROR:  Specified test ${t} does not appear to exist"
	skip=`expr $skip + 1`
	continue
    fi
    
    if test "X${args}" != "X" ; then
	pcb_flags="${args}"
    fi

    if test "X${hid}" = "Xgerber" ; then
	pcb_flags="--fab-author Fab_Author ${pcb_flags}"
    fi

    ######################################################################
    #
    # Set up the run directory
    #

    test -d ${rundir} && rm -fr ${rundir}
    mkdir -p ${rundir}
    if test $? -ne 0 ; then
	echo "$0:  Could not create run directory ${rundir}"
	skip=`expr $skip + 1`
	continue
    fi


    ######################################################################
    #
    # check to see if the files we need exist and copy them to the run
    # directory
    #

    missing_files=no
    path_files=""
    for f in $files ; do
	if test ! -f ${INDIR}/${f} ; then
	    echo "ERROR:  File $f specified as part of the $t test does not exist"
	    missing_files=yes
	else
	    path_files="${path_files} ${INDIR}/${f}"
	    cp "${INDIR}/${f}" "${rundir}"
	fi
    done

    if test "$missing_files" = "yes" ; then
	echo "${t} had missing input files.  Skipping test"
	skip=`expr $skip + 1`
	continue
    fi
    
    ######################################################################
    #
    # run PCB
    #

    echo "${PCB} -x ${hid} ${pcb_flags} ${path_files}"
    (cd ${rundir} && ${PCB} -x ${hid} ${pcb_flags} ${files})
    pcb_rc=$?

    if test $pcb_rc -ne 0 ; then
	echo "${PCB} returned ${pcb_rc}.  This is a failure."
	fail=`expr $fail + 1`
	continue
    fi

    # and clean up the input files we'd copied over
    for f in $files ; do
	rm -f ${rundir}/${f}
    done

    ######################################################################
    #
    # check the result
    #

    if test "X$regen" = "Xyes" ; then
	echo    "## -*- makefile -*-"   > ${rundir}/Makefile.am
	echo                           >> ${rundir}/Makefile.am
	echo -n "EXTRA_DIST="          >> ${rundir}/Makefile.am
	for f in $out_files ; do
	    fn=`echo $f | sed 's;.*:;;g'`
	    echo    " \\"              >> ${rundir}/Makefile.am
	    echo -n "\t$fn"            >> ${rundir}/Makefile.am
	done
	echo                           >> ${rundir}/Makefile.am

	echo "Regenerated ${t}"
    else
	# compare the result to our reference file
	test_failed=no
	test_skipped=no
	for f in $out_files ; do
	    debug "processing $f"
	    # break apart type:fn into the type and file name
	    type=`echo $f | sed 's;:.*;;g'`
	    fn=`echo $f | sed 's;.*:;;g'`

	    case $type in
		# BOM HID
		bom)
		    compare_bom ${refdir}/${fn} ${rundir}/${fn}
		    ;;

	        xy)
		    compare_xy ${refdir}/${fn} ${rundir}/${fn}
		    ;;

		# GCODE HID
		gcode)
		    compare_gcode ${refdir}/${fn} ${rundir}/${fn}
		    ;;

		# GERBER HID
		cnc)
		    compare_cnc ${refdir}/${fn} ${rundir}/${fn}
		    ;;

		gbx)
		    compare_rs274x ${refdir}/${fn} ${rundir}/${fn}
		    ;;

		# PNG HID
	        gif)
		    compare_image ${refdir}/${fn} ${rundir}/${fn}
		    ;;

		jpg)
		    compare_image ${refdir}/${fn} ${rundir}/${fn}
		    ;;

		png)
		    compare_image ${refdir}/${fn} ${rundir}/${fn}
		    ;;

		# unknown
		*)
		    echo "internal error:  $type is not a known file type"
		    exit 1
		    ;;
	    esac

	done


	if test $test_failed = yes ; then
	    echo "FAIL"
	    fail=`expr $fail + 1`
	elif test $test_skipped = yes ; then
	    echo "SKIPPED"
	    skip=`expr $skip + 1`
	else
	    echo "PASSED"
	    pass=`expr $pass + 1`
	fi

    fi
    
done

show_sep
echo "Passed $pass, failed $fail, skipped $skip out of $tot tests."

rc=0
if test $pass -ne $tot ; then
    rc=1
fi

exit $rc