1#============================================================= -*-perl-*-
2#
3# t/compile5.t
4#
5# Test that the compiled template files written by compile4.t can be 
6# loaded and used.  Similar to compile2.t but using COMPILE_DIR as well
7# as COMPILE_EXT.
8#
9# Written by Andy Wardley <abw@kfs.org>
10#
11# Copyright (C) 1996-2000 Andy Wardley.  All Rights Reserved.
12# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
13#
14# This is free software; you can redistribute it and/or modify it
15# under the same terms as Perl itself.
16#
17# $Id$
18#
19#========================================================================
20
21use strict;
22use warnings;
23use lib qw( ./lib ../lib );
24use Template::Test;
25use Cwd qw( abs_path );
26use File::Path;
27
28my @dir   = -d 't' ? qw(t test) : qw(test);
29my $dir   = abs_path( File::Spec->catfile(@dir) );
30my $tdir  = abs_path( File::Spec->catfile(@dir, 'tmp'));
31my $cdir  = File::Spec->catfile($tdir, 'cache');
32my $zero  = File::Spec->catfile($dir, qw(src divisionbyzero));
33print "zero: $zero\n";
34
35#my $dir   = abs_path( -d 't' ? 't/test' : 'test' );
36#my $cdir  = abs_path("$dir/tmp") . "/cache";
37#my $zero  = "$cdir/src/divisionbyzero";
38my $ttcfg = {
39    POST_CHOMP   => 1,
40    INCLUDE_PATH => "$dir/src",
41    COMPILE_DIR  => "$cdir/",    # note trailing slash - should be handled OK
42    COMPILE_EXT  => '.ttc',
43    ABSOLUTE     => 1,
44    CONSTANTS    => {
45      dir  => $dir,
46      zero => $zero,
47    },
48};
49
50#print "
51
52# check compiled template files exist
53my $fixdir = $dir;
54$fixdir =~ s[:][]g if $^O eq 'MSWin32';
55my ($foo, $bar, $blam) = map { "$cdir/$fixdir/src/$_.ttc" } 
56                           qw( foo complex blam );
57$blam =~ s[/+][/]g;
58
59ok( -f $foo, 'cached foo' );
60ok( -f $bar, 'cached bar' );
61ok( -f $blam, 'cached blam' );
62
63# we're going to hack on the compiled 'foo' file to change some key text.
64# this way we can tell that the template was loaded from the compiled
65# version and not the source.
66
67my @foo_times = (stat $foo)[8,9];
68
69open(FOO, $foo) || die "$foo: $!\n";
70local $/ = undef;
71my $content = <FOO>;
72close(FOO);
73
74$content =~ s/the foo file/the newly hacked foo file/;
75open(FOO, "> $foo") || die "$foo: $!\n";
76print FOO $content;
77close(FOO);
78
79# and set back
80utime( @foo_times, $foo );
81
82
83# same again for 'blam'
84
85my @blam_times = (stat $blam)[8,9];
86
87open(BLAM, $blam) || die "$blam: $!\n";
88local $/ = undef;
89$content = <BLAM>;
90close(BLAM);
91$content =~ s/blam/wam-bam/g;
92open(BLAM, "> $blam") || die "$blam: $!\n";
93print BLAM $content;
94close(BLAM);
95
96# and set back
97utime( @blam_times, $blam );
98
99test_expect(\*DATA, $ttcfg, { root => abs_path($dir) } );
100
101exit;
102
103# cleanup cache directory
104rmtree($cdir) if -d $cdir;
105
106
107__DATA__
108-- test --
109[% INCLUDE foo a = 'any value' %]
110-- expect --
111This is the newly hacked foo file, a is any value
112
113-- test --
114[% META author => 'billg' version => 6.66  %]
115[% INCLUDE complex %]
116-- expect --
117This is the header, title: Yet Another Template Test
118This is a more complex file which includes some BLOCK definitions
119This is the footer, author: billg, version: 6.66
120- 3 - 2 - 1 
121
122-- test --
123[% INCLUDE "$root/src/blam" %]
124-- expect --
125This is the wam-bam file
126-- test --
127[%- # second pass, reads the compiled code from cache -%]
128[% INCLUDE divisionbyzero -%]
129-- expect --
130-- process --
131undef error - Illegal division by zero at [% constants.zero %] line 1, <DATA> chunk 1.
132