Simplify default entries sub, removing hanging $1/$2 refs.
[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 $ENV{TZ} = 'UTC';
30
31 my $blosxom_cgi = $ENV{BLOSXOM_CGI};
32 unless ($blosxom_cgi && -f $blosxom_cgi) {
33   if (-f "$testdir/../../blosxom.cgi") {
34     $blosxom_cgi = "$testdir/../../blosxom.cgi";
35     warn "ignoring BLOSXOM_CGI setting '$ENV{BLOSXOM_CGI}' - using '$blosxom_cgi' instead"
36       if $ENV{BLOSXOM_CGI};
37   }
38   elsif ($blosxom_cgi) {
39     die "cannot find blosxom.cgi '$blosxom_cgi' - check your BLOSXOM_CGI environment variable";
40   }
41   else {
42     die "cannot find blosxom.cgi - please set the BLOSXOM_CGI environment variable";
43   }
44 }
45 die "blosxom.cgi '$blosxom_cgi' is not executable" unless -x $blosxom_cgi;
46
47 my $spec = YAML::LoadFile ("$testdir/spec.yaml") 
48   or die("$test - could not load spec");
49
50 touch_files("$testdir/data");
51
52 # Eval blosxom.conf
53 my ($static_dir, $static_password, @static_flavours);
54 if (my $fh = IO::File->new("$blosxom_config_dir/blosxom.conf", 'r')) {
55   no strict;
56   local $/ = undef;
57   eval <$fh>;
58 }
59
60 # Static mode
61 if ($static_password) {
62   eval {
63     require File::DirCompare;
64     require File::Remove;
65   };
66   SKIP: {
67     skip "Static tests require additional modules: $@", 1 if $@;
68     my $expected = $spec->{expected};
69     skip "Static tests require 'expected' directory", 1 unless $expected;
70     $expected = "$blosxom_config_dir/../$expected" unless $expected =~ m!^/!;
71     skip "Static tests 'expected' directory is missing", 1 unless -d $expected;
72     -d $static_dir or mkdir $static_dir
73       or die "mkdir on static_dir '$static_dir' failed: $!";
74
75     File::Remove::remove(\1, "$static_dir/*");
76
77     my $errors = qx($blosxom_cgi -quiet=1 -password=$static_password);
78     is($errors, '', 'no errors reported from static run');
79     File::DirCompare->compare($static_dir, "$blosxom_config_dir/../" . $spec->{expected}, sub {
80       my ($a, $b) = @_;
81       my ($a_short, $b_short) = ($a, $b);
82       $a_short =~ s!^.*\.\./!! if $a_short;
83       $b_short =~ s!^.*\.\./!! if $b_short;
84       return if $b && $b =~ m! /CVS$ !x;
85       if (! $b) {
86         fail("$a_short has no corresponding file");
87       } elsif (! $a) {
88         fail("$b_short has no corresponding file");
89       } else {
90         my ($got, $expected) = ('', '');
91         my $fh = IO::File->new($a, 'r') 
92           or die "cannot open static output file '$a': $!";
93         {
94           local $/ = undef;
95           $got = <$fh>;
96           $fh->close;
97         }
98         $fh = IO::File->new($b, 'r') 
99           or die "cannot open static output file '$b': $!";
100         {
101           local $/ = undef;
102           $expected = <$fh>;
103           $fh->close;
104         }
105         eq_or_diff($got, $expected, "file $a_short and $b_short match", { style => 'Unified' });
106       }
107     }, { ignore_cmp => 1 });
108
109     # Cleanup static output
110     File::Remove::remove(\1, "$static_dir/*") unless $ENV{BLOSXOM_STATIC_NO_CLEANUP};
111   }
112 }
113
114 # Dynamic mode
115 else {
116   my %expected = ();
117   for (@{$spec->{tests}}) {
118     my ($args, $output) = @$_;
119
120     unless ($expected{$output}) {
121       my $fh = IO::File->new("$testdir/$output", 'r')
122         or die "cannot open expected output file '$output': $!";
123       {
124         local $/ = undef;
125         $expected{$output} = <$fh>;
126       }
127       $fh->close;
128     }
129
130     my $got = qx($blosxom_cgi $args);
131
132     eq_or_diff($got, $expected{$output}, "$test - got expected output for args [$args]", { style => 'Unified' });
133   }
134 }
135
136 sub touch_files {
137   find( sub {
138     if (/^(.*)\.(\d+)$/) {
139       copy($_, $1);
140       `touch -t $2 $1`;
141     }
142   },
143   shift );
144 }