Updates to blosxom-plugins-medium.spec.
[blosxom-plugins.git] / t / driver
blob52fc128f36725c8f67270a7491d5ecf7fd10ebaa
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 use Test::More qw( no_plan );
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;
17 my $test = basename($0);
18 $test =~ s/^\d+_?//;
19 $test =~ s/\.t$//;
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;
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';
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};
38 elsif ($blosxom_cgi) {
39 die "cannot find blosxom.cgi '$blosxom_cgi' - check your BLOSXOM_CGI environment variable";
41 else {
42 die "cannot find blosxom.cgi - please set the BLOSXOM_CGI environment variable";
45 die "blosxom.cgi '$blosxom_cgi' is not executable" unless -x $blosxom_cgi;
47 my $spec = YAML::LoadFile ("$testdir/spec.yaml")
48 or die("$test - could not load spec");
50 touch_files("$testdir/data");
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>;
60 # Static mode
61 if ($static_password) {
62 eval {
63 require File::DirCompare;
64 require File::Remove;
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;
74 File::Remove::remove(\1, "$static_dir/*");
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': $!";
93 local $/ = undef;
94 $got = <$fh>;
95 $fh->close;
97 $fh = IO::File->new($b, 'r')
98 or die "cannot open static output file '$b': $!";
100 local $/ = undef;
101 $expected = <$fh>;
102 $fh->close;
104 eq_or_diff($got, $expected, "file $a_short and $b_short match", { style => 'Unified' });
106 }, { ignore_cmp => 1 });
108 # Cleanup static output
109 File::Remove::remove(\1, "$static_dir/*") unless $ENV{BLOSXOM_STATIC_NO_CLEANUP};
113 # Dynamic mode
114 else {
115 my %expected = ();
116 for (@{$spec->{tests}}) {
117 my ($args, $output) = @$_;
119 unless ($expected{$output}) {
120 my $fh = IO::File->new("$testdir/$output", 'r')
121 or die "cannot open expected output file '$output': $!";
123 local $/ = undef;
124 $expected{$output} = <$fh>;
126 $fh->close;
129 my $got = qx($blosxom_cgi $args);
131 eq_or_diff($got, $expected{$output}, "$test - got expected output for args [$args]", { style => 'Unified' });
135 sub touch_files {
136 find( sub {
137 if (/^(.*)\.(\d+)$/) {
138 copy($_, $1);
139 `touch -t $2 $1`;
142 shift );