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