]> git.deb.at Git - pkg/blosxom.git/blob - t/driver
Redo path_info handling with much stricter date handling.
[pkg/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     skip "Static tests 'static_dir' directory is missing", 1 unless -d $static_dir;
73
74     File::Remove::remove(\1, "$static_dir/*");
75
76     my $errors = qx($blosxom_cgi -quiet=1 -password=$static_password);
77     is($errors, '', 'no errors reported from static run');
78     File::DirCompare->compare($static_dir, "$blosxom_config_dir/../" . $spec->{expected}, sub {
79       my ($a, $b) = @_;
80       my ($a_short, $b_short) = ($a, $b);
81       $a_short =~ s!^.*\.\./!! if $a_short;
82       $b_short =~ s!^.*\.\./!! if $b_short;
83       return if $b =~ m! /CVS$ !x;
84       if (! $b) {
85         fail("$a_short has no corresponding file");
86       } elsif (! $a) {
87         fail("$b_short has no corresponding file");
88       } else {
89         my ($got, $expected) = ('', '');
90         my $fh = IO::File->new($a, 'r') 
91           or die "cannot open static output file '$a': $!";
92         {
93           local $/ = undef;
94           $got = <$fh>;
95           $fh->close;
96         }
97         $fh = IO::File->new($b, 'r') 
98           or die "cannot open static output file '$b': $!";
99         {
100           local $/ = undef;
101           $expected = <$fh>;
102           $fh->close;
103         }
104         eq_or_diff($got, $expected, "file $a_short and $b_short match", { style => 'Unified' });
105       }
106     }, { ignore_cmp => 1 });
107
108     # Cleanup static output
109     File::Remove::remove(\1, "$static_dir/*") unless $ENV{BLOSXOM_STATIC_NO_CLEANUP};
110   }
111 }
112
113 # Dynamic mode
114 else {
115   my %expected = ();
116   for (@{$spec->{tests}}) {
117     my ($args, $output) = @$_;
118
119     unless ($expected{$output}) {
120       my $fh = IO::File->new("$testdir/$output", 'r')
121         or die "cannot open expected output file '$output': $!";
122       {
123         local $/ = undef;
124         $expected{$output} = <$fh>;
125       }
126       $fh->close;
127     }
128
129     my $got = qx($blosxom_cgi $args);
130
131     eq_or_diff($got, $expected{$output}, "$test - got expected output for args [$args]", { style => 'Unified' });
132   }
133 }
134
135 sub touch_files {
136   find( sub {
137     if (/^(.*)\.(\d+)$/) {
138       copy($_, $1);
139       `touch -t $2 $1`;
140     }
141   },
142   shift );
143 }