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