2 # A simple unit testing script
3 # (c) 2004--2007 Martin Mares <mj@ucw.cz>
4 # (c) 2007 Pavel Charvat <pchar@ucw.cz>
6 # Tests in the test file have a syntax similar to mail headers,
7 # individual test case are separated by blank lines and they can contain
8 # the following fields:
10 # Name: name of the case (default: sequence number since start of file)
11 # Run: command to run (default: command from the previous test case)
12 # This can be an arbitrary shell pipeline, sequences $0 to $9 are
13 # replaced by file names of In<N> or Out<N> files (see below).
14 # In: lines to pass to the program as standard input
15 # Out: lines to expect at the program's standard output
16 # In<N>: lines to pass to the program as input file <N>
17 # Out<N>: lines to expect from the program in output file <N>
18 # Both In<N> and Out<N> can be specified simultaneously if we
19 # are testing a program which modifies some of its input files.
20 # Exit: expected exit code of the program (default: 0)
26 GetOptions("verbose!" => \$verbose,
27 "rundir=s" => \$rundir)
28 or die "Usage: tester [--verbose] [--rundir=<dir>] <tests>\n";
39 } elsif (defined($append_to) && /^\s+(.*)$/) {
40 $$append_to .= "\n$1";
41 } elsif (my ($n,$v) = /^(\w+):\s+(.*)$/) {
46 ($tt->{$n}) && die "$n already defined";
48 $append_to = \($tt->{$n});
50 die "Test script syntax error";
54 if (! -d "$rundir/tmp") {
55 mkdir "$rundir/tmp" or die "Unable to create $rundir/tmp: $!";
61 TEST: foreach $tt (@tests) {
63 my $name = $tt->{'Name'} || $i;
65 $run = ($tt->{'Run'} || $prev_run) or die "Don't know what to run";
72 if (defined $tt->{'In'}) {
73 my $ifi = "tmp/test$i.in";
74 open X, ">$rundir/$ifi" or die "Unable to create $ifi";
75 print X $tt->{'In'}, "\n";
79 $redirs .= " </dev/null";
81 if (defined $tt->{'Out'}) {
82 my $ofi = "tmp/test$i.out";
83 unlink "$rundir/$ofi";
85 push @out_files, $ofi;
86 push @out_checks, $tt->{'Out'};
88 $redirs .= " >/dev/null";
90 foreach my $arg (0..9) {
91 my $f = "tmp/test$i.$arg";
92 if (defined $tt->{"Out$arg"}) {
95 push @out_checks, $tt->{"Out$arg"};
97 if (defined $tt->{"In$arg"}) {
98 open X, ">$rundir/$f" or die "Unable to create $f";
99 print X $tt->{"In$arg"}, "\n";
103 $run =~ s/\$(\d)/tmp\/test$i.$1/g;
104 print "(running $run) " if $verbose;
105 system "cd $rundir && ( $run ) $redirs";
107 print "FAILED with status code $?\n";
112 my $expect_ec = $tt->{'Exit'} || 0;
113 if ($ec != $expect_ec) {
114 print "FAILED: unexpected exit code $ec\n";
119 for (my $i=0; $i<=$#out_files; $i++) {
120 my $ofi = $out_files[$i];
121 open X, "<$rundir/$ofi" or die "Unable to read $ofi";
128 if ($out ne $out_checks[$i] . "\n") {
129 print "FAILED (see $ofi)\n";
135 system "rm -f $rundir/tmp/test$i.*";