]> mj.ucw.cz Git - libucw.git/blob - build/tester
Added one more check.
[libucw.git] / build / tester
1 #!/usr/bin/perl
2 # A simple unit testing script
3 # (c) 2004 Martin Mares <mj@ucw.cz>
4
5 my @tests = ();
6 my $tt;
7 my $append_to;
8
9 while (<>) {
10         /^#/ && next;
11         if (/^\s*$/) {
12                 $tt = undef;
13                 $append_to = undef;
14         } elsif (defined($append_to) && /^\s+(.*)$/) {
15                 $$append_to .= "\n$1";
16         } elsif (my ($n,$v) = /^(\w+):\s+(.*)$/) {
17                 if (!$tt) {
18                         $tt = {};
19                         push @tests, $tt;
20                 }
21                 ($tt->{$n}) && die "$n already defined";
22                 $tt->{$n} = $v;
23                 $append_to = \($tt->{$n});
24         } else {
25                 die "Test script syntax error";
26         }
27 }
28
29 my $i = 0;
30 my $errors = 0;
31 foreach $tt (@tests) {
32         $i++;
33         print "Test $i: ";
34         my $run = $tt->{'Run'} or die "Don't know what to run";
35         my ($ifi, $ofi);
36         if (defined $tt->{'In'}) {
37                 $ifi = "run/tmp/test$i.in";
38                 open X, ">$ifi" or die "Unable to create $ifi";
39                 print X $tt->{'In'}, "\n";
40                 close X;
41                 $run .= " <$ifi";
42         } else {
43                 $run .= " </dev/null";
44         }
45         if (defined $tt->{'Out'}) {
46                 $ofi = "run/tmp/test$i.out";
47                 unlink $ofi;
48                 $run .= " >$ofi";
49         } else {
50                 $run .= " >/dev/null";
51         }
52         `$run`;
53         if ($?) {
54                 print "FAILED with exit code $?\n";
55                 $errors++;
56                 next;
57         }
58         if (defined $tt->{'Out'}) {
59                 open X, "<$ofi" or die "Unable to read $ofi";
60                 my $out;
61                 {
62                         local $/ = undef;
63                         $out = <X>;
64                 }
65                 close X;
66                 if ($out ne $tt->{'Out'} . "\n") {
67                         print "FAILED (see $ofi)\n";
68                         $errors++;
69                         next;
70                 }
71         }
72         unlink $ifi if $ifi;
73         unlink $ofi if $ofi;
74         print "OK\n";
75 }
76
77 exit !!$errors;