Refactor and fix current test cases.
[matthijs/upstream/blosxom.git] / t / driver
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More qw( no_plan );
7
8 use Cwd;
9 use YAML;
10 use IO::File;
11 use File::Find;
12 use File::Copy;
13 #use File::Touch;
14 use File::Basename;
15 use Test::Differences;
16
17 my $test = basename($0);
18 $test =~ s/^\d+_?//;
19 $test =~ s/\.t$//;
20
21 my $testdir = $test;
22 $testdir = "t/$testdir" if -d "t/$testdir";
23 $testdir = cwd . "/$testdir";
24 die "cannot find root '$testdir'" unless -d $testdir;
25
26 my $blosxom_config_dir = "$testdir/config";
27 die "cannot find blosxom config dir '$blosxom_config_dir'" unless -d $blosxom_config_dir;
28 $ENV{BLOSXOM_CONFIG_DIR} = $blosxom_config_dir;
29
30 my $blosxom_cgi = "$testdir/../../blosxom.cgi";
31 die "cannot find blosxom.cgi '$blosxom_cgi'" unless -f $blosxom_cgi;
32 die "blosxom.cgi '$blosxom_cgi' is not executable" unless -x $blosxom_cgi;
33
34 my $spec = YAML::LoadFile ("$testdir/spec.yaml") 
35   or fail("$test - loading spec") and next;
36
37 touch_files("$testdir/data");
38
39 my %expected = ();
40
41 for (@{$spec->{tests}}) {
42   my ($args, $output) = @$_;
43
44   unless ($expected{$output}) {
45     my $fh = IO::File->new("$testdir/$output", 'r')
46       or die "cannot open expected output file '$output': $!";
47     {
48       local $/ = undef;
49       $expected{$output} = <$fh>;
50     }
51     $fh->close;
52   }
53
54   my $got = qx($blosxom_cgi $args);
55
56   eq_or_diff($got, $expected{$output}, "$test - got expected output for args [$args]", { style => 'Unified' });
57 }
58
59 sub touch_files {
60   find( sub {
61     if (/^(.*)\.(\d+)$/) {
62       copy($_, $1);
63       `touch -t $2 $1`;
64     }
65   },
66   shift );
67 }