2 # This is a testing framework.
4 # In case you're wondering about the name, it comes from the
5 # names of the three packages: FIleutils, SH-utils, TExtutils.
9 use vars
qw($VERSION @ISA @EXPORT);
12 use File::Compare qw(compare);
15 ($VERSION = '$Revision: 1.6 $ ') =~ tr/[0-9].//cd;
16 @EXPORT = qw
(run_tests
);
18 my $debug = $ENV{DEBUG
};
20 my @Types = qw
(IN OUT ERR EXIT PRE POST
);
21 my %Types = map {$_ => 1} @Types;
22 my %Zero_one_type = map {$_ => 1} qw
(OUT ERR EXIT PRE POST
);
23 my $srcdir = $ENV{srcdir
};
26 # A file spec: a scalar or a reference to a single-keyed hash
28 # 'contents' contents only (file name is derived from test name)
29 # {filename => 'contents'} filename and contents
30 # {filename => undef} filename only -- $(srcdir)/filename must exist
32 # FIXME: If there is more than one input file, the you can't specify REDIRECT.
35 # I/O spec: a hash ref with the following properties
37 # - one key/value pair
38 # - the key must be one of these strings: IN, OUT, ERR, EXIT
39 # - the value must be a file spec
40 # {OUT => 'data'} put data in a temp file and compare it to stdout from cmd
41 # {OUT => {'filename'=>undef}} compare contents of existing filename to
43 # {OUT => {'filename'=>[$CTOR, $DTOR]}} $CTOR and $DTOR are references to
44 # functions, each which is passed the single argument `filename'.
45 # $CTOR must create `filename'.
46 # DTOR may be omitted in which case `sub{unlink @_[0]}' is used.
47 # FIXME: implement this
48 # Ditto for `ERR', but compare with stderr
49 # {EXIT => N} expect exit status of cmd to be N
51 # There may be many input file specs. File names from the input specs
52 # are concatenated in order on the command line.
53 # There may be at most one of the OUT-, ERR-, and EXIT-keyed specs.
54 # If the OUT-(or ERR)-keyed hash ref is omitted, then expect no output
55 # on stdout (or stderr).
56 # If the EXIT-keyed one is omitted, then expect the exit status to be zero.
58 # FIXME: Make sure that no junkfile is also listed as a
59 # non-junkfile (i.e. with undef for contents)
64 $string =~ s/\'/\'\\\'\'/g;
68 sub _create_file
($$$$)
70 my ($program_name, $test_name, $file_name, $data) = @_;
72 if (defined $file_name)
78 $file = "$test_name.$Global_count";
82 warn "creating file `$file' with contents `$data'\n" if $debug;
84 # The test spec gave a string.
85 # Write it to a temp file and return tempfile name.
86 my $fh = new FileHandle
"> $file";
87 die "$program_name: $file: $!\n" if ! $fh;
89 $fh->close || die "$program_name: $file: $!\n";
94 # FIXME: cleanup on interrupt
95 # FIXME: extract `do_1_test' function
97 # FIXME: having to include $program_name here is an expedient kludge.
98 # Library code doesn't `die'.
101 my ($program_name, $prog, $t_spec, $save_temps, $verbose) = @_;
103 # Warn about empty t_spec.
106 # Remove all temp files upon interrupt.
109 # Verify that test names are distinct.
110 my $found_duplicate = 0;
113 foreach $t (@
$t_spec)
115 my $test_name = $t->[0];
116 if ($seen{$test_name})
118 warn "$program_name: $test_name: duplicate test name\n";
119 $found_duplicate = 1;
121 $seen{$test_name} = 1;
123 return 1 if $found_duplicate;
125 # FIXME check exit status
126 system ($prog, '--version') if $verbose;
130 foreach $t (@
$t_spec)
132 my $test_name = shift @
$t;
136 # FIXME: maybe don't reset this.
141 foreach $io_spec (@
$t)
145 push @args, $io_spec;
149 die "$program_name: $test_name: invalid test spec\n"
150 if ref $io_spec ne 'HASH';
152 my $n = keys %$io_spec;
153 die "$program_name: $test_name: spec has $n elements --"
156 my ($type, $val) = each %$io_spec;
157 die "$program_name: $test_name: invalid key `$type' in test spec\n"
160 # Make sure there's no more than one of OUT, ERR, EXIT.
161 die "$program_name: $test_name: more than one $type spec\n"
162 if $Zero_one_type{$type} and $seen_type{$type}++;
164 if ($type eq 'PRE' or $type eq 'POST')
166 $expect->{$type} = $val;
172 die "$program_name: $test_name: invalid EXIT code\n"
174 # FIXME: make sure $data is numeric
175 $expect->{EXIT
} = $val;
179 my $file_spec = $val;
180 my ($file_name, $contents);
183 ($file_name, $contents) = (undef, $file_spec);
185 elsif (ref $file_spec eq 'HASH')
187 my $n = keys %$file_spec;
188 die "$program_name: $test_name: $type spec has $n elements --"
191 ($file_name, $contents) = each %$file_spec;
195 die "$program_name: $test_name: invalid RHS in $type-spec\n"
198 my $is_junk_file = (! defined $file_name
199 || ($type eq 'IN' && defined $contents));
200 my $file = _create_file
($program_name, $test_name,
201 $file_name, $contents);
204 push @args, _shell_quote
$file;
208 $expect->{$type} = $file;
213 push @junk_files, $file
217 # FIXME: put $srcdir in here somewhere
218 warn "$program_name: $test_name: specified file `$file' does"
220 if ! -f
"$srcdir/$file";
224 # Expect an exit status of zero if it's not specified.
225 $expect->{EXIT
} ||= 0;
227 # Allow ERR to be omitted -- in that case, expect no error output.
229 foreach $eo (qw
(OUT ERR
))
231 if (!exists $expect->{$eo})
233 $expect->{$eo} = _create_file
($program_name, $test_name,
235 push @junk_files, $expect->{$eo};
239 # FIXME: Does it ever make sense to specify a filename *and* contents
240 # in OUT or ERR spec?
242 warn "$test_name...\n" if $verbose;
243 &{$expect->{PRE
}} if $expect->{PRE
};
245 $tmp{OUT
} = "$test_name.O";
246 $tmp{ERR
} = "$test_name.E";
247 push @junk_files, $tmp{OUT
}, $tmp{ERR
};
248 my @cmd = ($prog, @args, "> $tmp{OUT}", "2> $tmp{ERR}");
249 my $cmd_str = join ' ', @cmd;
250 warn "Running command: `$cmd_str'\n" if $debug;
251 my $rc = 0xffff & system $cmd_str;
254 warn "$program_name: test $test_name failed: command failed:\n"
255 . " `$cmd_str': $!\n";
259 $rc >>= 8 if $rc > 0x80;
260 if ($expect->{EXIT
} != $rc)
262 warn "$program_name: test $test_name failed: exit status mismatch:"
263 . " expected $expect->{EXIT}, got $rc\n";
268 foreach $eo (qw
(OUT ERR
))
270 my $eo_lower = lc $eo;
271 if (compare
($expect->{$eo}, $tmp{$eo}))
273 warn "$program_name: test $test_name: std$eo_lower mismatch,"
274 . " comparing $expect->{$eo} and $tmp{$eo}\n";
280 &{$expect->{POST
}} if $expect->{POST
};
284 # FIXME: maybe unlink files inside the big foreach loop?
285 unlink @junk_files if ! $save_temps;