]> mj.ucw.cz Git - libucw.git/blob - build/tester
6a827b194ffcacbe08829bde52ed189636fb7050
[libucw.git] / build / tester
1 #!/usr/bin/perl
2 # A simple unit testing script
3 # (c) 2004--2013 Martin Mares <mj@ucw.cz>
4 # (c) 2007 Pavel Charvat <pchar@ucw.cz>
5
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:
9 #
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 #       Err:    lines to expect at the program's standard error output
17 #       In<N>:  lines to pass to the program as input file <N>
18 #       Out<N>: lines to expect from the program in output file <N>
19 #               Both In<N> and Out<N> can be specified simultaneously if we
20 #               are testing a program which modifies some of its input files.
21 #       Exit:   expected exit code of the program (default: 0)
22 #
23 # A value of a field can be optionally given as a shell-style here-document:
24 #
25 #       In <<AMEN
26 #       multiple
27 #       lines
28 #       of input
29 #       AMEN
30
31 use Getopt::Long;
32
33 my $verbose = 0;
34 my $rundir = ".";
35 GetOptions("verbose!" => \$verbose,
36            "rundir=s" => \$rundir)
37         or die "Usage: tester [--verbose] [--rundir=<dir>] <tests>\n";
38
39 my @tests = ();
40 my $tt;
41 my $append_to;
42
43 while (<>) {
44         /^#/ && next;
45         if (/^\s*$/) {
46                 $tt = undef;
47                 $append_to = undef;
48         } elsif (defined($append_to) && /^\s+(.*)$/) {
49                 $$append_to .= "\n$1";
50         } elsif (my ($n,$v) = /^(\w+):\s+(.*)$/) {
51                 if (!$tt) {
52                         $tt = {};
53                         push @tests, $tt;
54                 }
55                 ($tt->{$n}) && die "$n already defined";
56                 $tt->{$n} = $v;
57                 $append_to = \($tt->{$n});
58         } elsif (my ($n,$sep) = /^(\w+)\s*<<(\w+)\s*$/) {
59                 if (!$tt) {
60                         $tt = {};
61                         push @tests, $tt;
62                 }
63                 ($tt->{$n}) && die "$n already defined";
64                 $tt->{$n} = "";
65                 $sep .= "\n";
66                 while (1) {
67                         my $line = <>;
68                         defined $line or die "Here-document not terminated";
69                         last if $line eq $sep;
70                         $tt->{$n} .= $line;
71                 }
72         } else {
73                 die "Test script syntax error";
74         }
75 }
76
77 if (! -d "$rundir/tmp") {
78         mkdir "$rundir/tmp" or die "Unable to create $rundir/tmp: $!";
79 }
80
81 my $i = 0;
82 my $errors = 0;
83 my $prev_run = undef;
84 TEST: foreach $tt (@tests) {
85         $i++;
86         my $name = $tt->{'Name'} || $i;
87         print "Test $name: ";
88         $run = ($tt->{'Run'} || $prev_run) or die "Don't know what to run";
89         $prev_run = $run;
90
91         my @out_files = ();
92         my @out_checks = ();
93         my $redirs = "";
94
95         if (defined $tt->{'In'}) {
96                 my $ifi = "tmp/test$i.in";
97                 open X, ">$rundir/$ifi" or die "Unable to create $ifi";
98                 print X $tt->{'In'}, "\n";
99                 close X;
100                 $redirs .= " <$ifi";
101         } else {
102                 $redirs .= " </dev/null";
103         }
104         if (defined $tt->{'Out'}) {
105                 my $ofi = "tmp/test$i.out";
106                 unlink "$rundir/$ofi";
107                 $redirs .= " >$ofi";
108                 push @out_files, $ofi;
109                 push @out_checks, $tt->{'Out'};
110         } else {
111                 $redirs .= " >/dev/null";
112         }
113         if (defined $tt->{'Err'}) {
114                 my $efi = "tmp/test$i.err";
115                 unlink "$rundir/$efi";
116                 $redirs .= " 2>$efi";
117                 push @out_files, $efi;
118                 push @out_checks, $tt->{'Err'};
119         }
120         foreach my $arg (0..9) {
121                 my $f = "tmp/test$i.$arg";
122                 if (defined $tt->{"Out$arg"}) {
123                         unlink "$rundir/$f";
124                         push @out_files, $f;
125                         push @out_checks, $tt->{"Out$arg"};
126                 }
127                 if (defined $tt->{"In$arg"}) {
128                         open X, ">$rundir/$f" or die "Unable to create $f";
129                         print X $tt->{"In$arg"}, "\n";
130                         close X;
131                 }
132         }
133         $run =~ s/\$(\d)/tmp\/test$i.$1/g;
134         print "(running $run) " if $verbose;
135         system "cd $rundir && ( $run ) $redirs";
136         if ($? % 256) {
137                 print "FAILED with status code $?\n";
138                 $errors++;
139                 next;
140         }
141         my $ec = $? / 256;
142         my $expect_ec = $tt->{'Exit'} || 0;
143         if ($ec != $expect_ec) {
144                 print "FAILED: unexpected exit code $ec\n";
145                 $errors++;
146                 next;
147         }
148
149         for (my $i=0; $i<=$#out_files; $i++) {
150                 my $ofi = $out_files[$i];
151                 open X, "<$rundir/$ofi" or die "Unable to read $ofi";
152                 my $out;
153                 {
154                         local $/ = undef;
155                         $out = <X>;
156                 }
157                 close X;
158                 $out =~ /\n$/s or $out .= "\n";
159                 if ($out ne $out_checks[$i] . "\n") {
160                         print "FAILED (see $ofi)\n";
161                         $errors++;
162                         next TEST;
163                 }
164         }
165
166         system "rm -f $rundir/tmp/test$i.*";
167         print "OK\n";
168 }
169
170 exit !!$errors;