Initial checkin of a slightly modified version of Bugzilla's test suite to check for compile errors and cross-site issues in template files. Customizations and fixes to follow in subsequent checkins.

This commit is contained in:
zach%zachlipton.com 2005-11-10 22:58:51 +00:00
Родитель 65c4e9f3c3
Коммит 033b2c08fd
9 изменённых файлов: 885 добавлений и 0 удалений

43
webtools/litmus/runtests.pl Executable file
Просмотреть файл

@ -0,0 +1,43 @@
#!/usr/bin/perl -w
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Mike Norton.
# Portions created by the Initial Developer are Copyright (C) 2002
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Zach Lipton <zach@zachlipton.com>
#
# Make it harder for us to do dangerous things in Perl.
use diagnostics;
use strict;
use Test::Harness qw(&runtests $verbose);
$verbose = 0;
my $onlytest = "";
foreach (@ARGV) {
if (/^(?:-v|--verbose)$/) {
$verbose = 1;
}
else {
$onlytest = sprintf("%0.3d",$_);
}
}
runtests(glob("t/$onlytest*.t"));

Просмотреть файл

@ -0,0 +1,100 @@
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code are the Bugzilla Tests.
#
# The Initial Developer of the Original Code is Zach Lipton
# Portions created by Zach Lipton are
# Copyright (C) 2001 Zach Lipton. All
# Rights Reserved.
#
# Contributor(s): Zach Lipton <zach@zachlipton.com>
#################
#Bugzilla Test 1#
###Compilation###
use strict;
use lib 't';
use Support::Files;
use Test::More tests => scalar(@Support::Files::testitems);
# Need this to get the available driver information
use DBI;
my @DBI_drivers = DBI->available_drivers;
# Bugzilla requires Perl 5.6.1 now. Checksetup will tell you this if you run it, but
# it tests it in a polite/passive way that won't make it fail at compile time. We'll
# slip in a compile-time failure if it's missing here so a tinderbox on 5.00503 won't
# pass and mistakenly let people think Bugzilla works on 5.00503
require 5.006_001;
# Capture the TESTOUT from Test::More or Test::Builder for printing errors.
# This will handle verbosity for us automatically.
my $fh;
{
local $^W = 0; # Don't complain about non-existent filehandles
if (-e \*Test::More::TESTOUT) {
$fh = \*Test::More::TESTOUT;
} elsif (-e \*Test::Builder::TESTOUT) {
$fh = \*Test::Builder::TESTOUT;
} else {
$fh = \*STDOUT;
}
}
my @testitems = @Support::Files::testitems;
my $perlapp = "\"$^X\"";
# Test the scripts by compiling them
foreach my $file (@testitems) {
$file =~ s/\s.*$//; # nuke everything after the first space (#comment)
next if (!$file); # skip null entries
# Check that we have a DBI module to support the DB, if this is a database
# module (but not Schema)
if ($file =~ m#Bugzilla/DB/([^/]+)\.pm$# && $file ne "Bugzilla/DB/Schema.pm") {
if (!grep(lc($_) =~ /$1/i, @DBI_drivers)) {
ok(1,$file." - Skipping, as the DBD module not installed");
next;
}
}
open (FILE,$file);
my $bang = <FILE>;
close (FILE);
my $T = "";
if ($bang =~ m/#!\S*perl\s+-.*T/) {
$T = "T";
}
my $command = "$perlapp -c$T $file 2>&1";
my $loginfo=`$command`;
#print '@@'.$loginfo.'##';
if ($loginfo =~ /syntax ok$/im) {
if ($loginfo ne "$file syntax OK\n") {
ok(0,$file." --WARNING");
print $fh $loginfo;
} else {
ok(1,$file);
}
} else {
ok(0,$file." --ERROR");
print $fh $loginfo;
}
}
exit 0;

Просмотреть файл

@ -0,0 +1,65 @@
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code are the Bugzilla Tests.
#
# The Initial Developer of the Original Code is Zach Lipton
# Portions created by Zach Lipton are
# Copyright (C) 2001 Zach Lipton. All
# Rights Reserved.
#
# Contributor(s): Zach Lipton <zach@zachlipton.com>
#################
#Bugzilla Test 3#
###Safesystem####
use strict;
use lib 't';
use Support::Files;
use Test::More tests => scalar(@Support::Files::testitems);
# Capture the TESTOUT from Test::More or Test::Builder for printing errors.
# This will handle verbosity for us automatically.
my $fh;
{
local $^W = 0; # Don't complain about non-existent filehandles
if (-e \*Test::More::TESTOUT) {
$fh = \*Test::More::TESTOUT;
} elsif (-e \*Test::Builder::TESTOUT) {
$fh = \*Test::Builder::TESTOUT;
} else {
$fh = \*STDOUT;
}
}
my @testitems = @Support::Files::testitems;
my $perlapp = "\"$^X\"";
foreach my $file (@testitems) {
$file =~ s/\s.*$//; # nuke everything after the first space (#comment)
next if (!$file); # skip null entries
my $command = "$perlapp -c -It -MSupport::Systemexec $file 2>&1";
my $loginfo=`$command`;
if ($loginfo =~ /arguments for Support::Systemexec::(system|exec)/im) {
ok(0,"$file DOES NOT use proper system or exec calls");
print $fh $loginfo;
} else {
ok(1,"$file uses proper system and exec calls");
}
}
exit 0;

Просмотреть файл

@ -0,0 +1,55 @@
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code are the Bugzilla tests.
#
# The Initial Developer of the Original Code is Jacob Steenhagen.
# Portions created by Jacob Steenhagen are
# Copyright (C) 2001 Jacob Steenhagen. All
# Rights Reserved.
#
# Contributor(s): Jacob Steenhagen <jake@bugzilla.org>
# David D. Kilzer <ddkilzer@kilzer.net>
#
#################
#Bugzilla Test 5#
#####no_tabs#####
use strict;
use lib 't';
use Support::Files;
use Support::Templates;
use File::Spec;
use Test::More tests => ( scalar(@Support::Files::testitems)
+ $Support::Templates::num_actual_files);
my @testitems = @Support::Files::testitems;
for my $path (@Support::Templates::include_paths) {
push(@testitems, map(File::Spec->catfile($path, $_),
Support::Templates::find_actual_files($path)));
}
foreach my $file (@testitems) {
open (FILE, "$file");
if (grep /\t/, <FILE>) {
ok(0, "$file contains tabs --WARNING");
} else {
ok(1, "$file has no tabs");
}
close (FILE);
}
exit 0;

Просмотреть файл

@ -0,0 +1,151 @@
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code are the Bugzilla tests.
#
# The Initial Developer of the Original Code is Jacob Steenhagen.
# Portions created by Jacob Steenhagen are
# Copyright (C) 2001 Jacob Steenhagen. All
# Rights Reserved.
#
# Contributor(s): Jacob Steenhagen <jake@bugzilla.org>
# Zach Lipton <zach@zachlipton.com>
# David D. Kilzer <ddkilzer@kilzer.net>
# Tobias Burnus <burnus@net-b.de>
#
#################
#Bugzilla Test 4#
####Templates####
use strict;
use lib 't';
use Support::Templates;
# Bug 137589 - Disable command-line input of CGI.pm when testing
use CGI qw(-no_debug);
use File::Spec;
use Template;
use Test::More tests => ( scalar(@referenced_files) * scalar(@languages)
+ $num_actual_files * 2 );
# Capture the TESTOUT from Test::More or Test::Builder for printing errors.
# This will handle verbosity for us automatically.
my $fh;
{
local $^W = 0; # Don't complain about non-existent filehandles
if (-e \*Test::More::TESTOUT) {
$fh = \*Test::More::TESTOUT;
} elsif (-e \*Test::Builder::TESTOUT) {
$fh = \*Test::Builder::TESTOUT;
} else {
$fh = \*STDOUT;
}
}
# Checks whether one of the passed files exists
sub existOnce {
foreach my $file (@_) {
return $file if -e $file;
}
return 0;
}
# Check to make sure all templates that are referenced in
# Bugzilla exist in the proper place.
foreach my $lang (@languages) {
foreach my $file (@referenced_files) {
my @path = map(File::Spec->catfile($_, $file),
split(':', $include_path{$lang}));
if (my $path = existOnce(@path)) {
ok(1, "$path exists");
} else {
ok(0, "$file cannot be located --ERROR");
print $fh "Looked in:\n " . join("\n ", @path);
}
}
}
foreach my $include_path (@include_paths) {
# Processes all the templates to make sure they have good syntax
my $provider = Template::Provider->new(
{
INCLUDE_PATH => $include_path ,
# Need to define filters used in the codebase, they don't
# actually have to function in this test, just be defined.
# See globals.pl for the actual codebase definitions.
# Initialize templates (f.e. by loading plugins like Hook).
PRE_PROCESS => "global/initialize.none.tmpl",
FILTERS =>
{
html_linebreak => sub { return $_; },
no_break => sub { return $_; } ,
js => sub { return $_ } ,
base64 => sub { return $_ } ,
inactive => [ sub { return sub { return $_; } }, 1] ,
closed => [ sub { return sub { return $_; } }, 1] ,
obsolete => [ sub { return sub { return $_; } }, 1] ,
url_quote => sub { return $_ } ,
css_class_quote => sub { return $_ } ,
xml => sub { return $_ } ,
quoteUrls => sub { return $_ } ,
bug_link => [ sub { return sub { return $_; } }, 1] ,
csv => sub { return $_ } ,
unitconvert => sub { return $_ },
time => sub { return $_ } ,
wrap_comment => sub { return $_ },
none => sub { return $_ } ,
ics => [ sub { return sub { return $_; } }, 1] ,
},
}
);
foreach my $file (@{$actual_files{$include_path}}) {
my $path = File::Spec->catfile($include_path, $file);
if (-e $path) {
my ($data, $err) = $provider->fetch($file);
if (!$err) {
ok(1, "$file syntax ok");
}
else {
ok(0, "$file has bad syntax --ERROR");
print $fh $data . "\n";
}
}
else {
ok(1, "$path doesn't exist, skipping test");
}
}
# check to see that all templates have a version string:
foreach my $file (@{$actual_files{$include_path}}) {
my $path = File::Spec->catfile($include_path, $file);
open(TMPL, $path);
my $firstline = <TMPL>;
if ($firstline =~ /\d+\.\d+\@[\w\.-]+/) {
ok(1,"$file has a version string");
} else {
ok(0,"$file does not have a version string --ERROR");
}
close(TMPL);
}
}
exit 0;

Просмотреть файл

@ -0,0 +1,232 @@
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code are the Bugzilla tests.
#
# The Initial Developer of the Original Code is Jacob Steenhagen.
# Portions created by Jacob Steenhagen are
# Copyright (C) 2001 Jacob Steenhagen. All
# Rights Reserved.
#
# Contributor(s): Gervase Markham <gerv@gerv.net>
#################
#Bugzilla Test 8#
#####filter######
# This test scans all our templates for every directive. Having eliminated
# those which cannot possibly cause XSS problems, it then checks the rest
# against the safe list stored in the filterexceptions.pl file.
# Sample exploit code: '>"><script>alert('Oh dear...')</script>
use strict;
use lib 't';
use vars qw(%safe);
use Support::Templates;
use File::Spec;
use Test::More tests => $Support::Templates::num_actual_files;
use Cwd;
# Undefine the record separator so we can read in whole files at once
my $oldrecsep = $/;
my $topdir = cwd;
$/ = undef;
foreach my $path (@Support::Templates::include_paths) {
$path =~ s|\\|/|g if $^O eq 'MSWin32'; # convert \ to / in path if on windows
$path =~ m|template/([^/]+)/([^/]+)|;
my $lang = $1;
my $flavor = $2;
chdir $topdir; # absolute path
my @testitems = Support::Templates::find_actual_files($path);
chdir $topdir; # absolute path
next unless @testitems;
# Some people require this, others don't. No-one knows why.
chdir $path; # relative path
# We load a %safe list of acceptable exceptions.
if (!-r "filterexceptions.pl") {
ok(0, "$path has templates but no filterexceptions.pl file. --ERROR");
next;
}
else {
do "filterexceptions.pl";
if ($^O eq 'MSWin32') {
# filterexceptions.pl uses / separated paths, while
# find_actual_files returns \ separated ones on Windows.
# Here, we convert the filter exception hash to use \.
foreach my $file (keys %safe) {
my $orig_file = $file;
$file =~ s|/|\\|g;
if ($file ne $orig_file) {
$safe{$file} = $safe{$orig_file};
delete $safe{$orig_file};
}
}
}
}
# We preprocess the %safe hash of lists into a hash of hashes. This allows
# us to flag which members were not found, and report that as a warning,
# thereby keeping the lists clean.
foreach my $file (keys %safe) {
my $list = $safe{$file};
$safe{$file} = {};
foreach my $directive (@$list) {
$safe{$file}{$directive} = 0;
}
}
foreach my $file (@testitems) {
# There are some files we don't check, because there is no need to
# filter their contents due to their content-type.
if ($file =~ /\.(txt|png)\.tmpl$/) {
ok(1, "($lang/$flavor) $file is filter-safe");
next;
}
# Read the entire file into a string
open (FILE, "<$file") || die "Can't open $file: $!\n";
my $slurp = <FILE>;
close (FILE);
my @unfiltered;
# /g means we execute this loop for every match
# /s means we ignore linefeeds in the regexp matches
while ($slurp =~ /\[%(.*?)%\]/gs) {
my $directive = $1;
my @lineno = ($` =~ m/\n/gs);
my $lineno = scalar(@lineno) + 1;
if (!directive_ok($file, $directive)) {
# This intentionally makes no effort to eliminate duplicates; to do
# so would merely make it more likely that the user would not
# escape all instances when attempting to correct an error.
push(@unfiltered, "$lineno:$directive");
}
}
my $fullpath = File::Spec->catfile($path, $file);
if (@unfiltered) {
my $uflist = join("\n ", @unfiltered);
ok(0, "($lang/$flavor) $fullpath has unfiltered directives:\n $uflist\n--ERROR");
}
else {
# Find any members of the exclusion list which were not found
my @notfound;
foreach my $directive (keys %{$safe{$file}}) {
push(@notfound, $directive) if ($safe{$file}{$directive} == 0);
}
if (@notfound) {
my $nflist = join("\n ", @notfound);
ok(0, "($lang/$flavor) $fullpath - filterexceptions.pl has extra members:\n $nflist\n" .
"--WARNING");
}
else {
# Don't use the full path here - it's too long and unwieldy.
ok(1, "($lang/$flavor) $file is filter-safe");
}
}
}
}
sub directive_ok {
my ($file, $directive) = @_;
# Comments
return 1 if $directive =~ /^[+-]?#/;
# Remove any leading/trailing + or - and whitespace.
$directive =~ s/^[+-]?\s*//;
$directive =~ s/\s*[+-]?$//;
# Empty directives are ok; they are usually line break helpers
return 1 if $directive eq '';
# Exclude those on the nofilter list
if (defined($safe{$file}{$directive})) {
$safe{$file}{$directive}++;
return 1;
};
# Directives
return 1 if $directive =~ /^(IF|END|UNLESS|FOREACH|PROCESS|INCLUDE|
BLOCK|USE|ELSE|NEXT|LAST|DEFAULT|FLUSH|
ELSIF|SET|SWITCH|CASE|WHILE|RETURN|STOP|
TRY|CATCH|FINAL|THROW|CLEAR)/x;
# ? :
if ($directive =~ /.+\?(.+):(.+)/) {
return 1 if directive_ok($file, $1) && directive_ok($file, $2);
}
# + - * /
return 1 if $directive =~ /[+\-*\/]/;
# Numbers
return 1 if $directive =~ /^[0-9]+$/;
# Simple assignments
return 1 if $directive =~ /^[\w\.\$]+\s+=\s+/;
# Conditional literals with either sort of quotes
# There must be no $ in the string for it to be a literal
return 1 if $directive =~ /^(["'])[^\$]*[^\\]\1/;
return 1 if $directive =~ /^(["'])\1/;
# Special values always used for numbers
return 1 if $directive =~ /^[ijkn]$/;
return 1 if $directive =~ /^count$/;
# Params
return 1 if $directive =~ /^Param\(/;
# Hooks
return 1 if $directive =~ /^Hook.process\(/;
# Other functions guaranteed to return OK output
return 1 if $directive =~ /^(time2str|GetBugLink|url)\(/;
# Safe Template Toolkit virtual methods
return 1 if $directive =~ /\.(length$|size$|push\()/;
# Special Template Toolkit loop variable
return 1 if $directive =~ /^loop\.(index|count)$/;
# Branding terms
return 1 if $directive =~ /^terms\./;
# Things which are already filtered
# Note: If a single directive prints two things, and only one is
# filtered, we may not catch that case.
return 1 if $directive =~ /FILTER\ (html|csv|js|base64|url_quote|css_class_quote|
ics|quoteUrls|time|uri|xml|lower|
obsolete|inactive|closed|unitconvert|
none)\b/x;
return 0;
}
$/ = $oldrecsep;
exit 0;

Просмотреть файл

@ -0,0 +1,79 @@
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code are the Bugzilla Tests.
#
# The Initial Developer of the Original Code is Zach Lipton
# Portions created by Zach Lipton are
# Copyright (C) 2001 Zach Lipton. All
# Rights Reserved.
#
# Contributor(s): Zach Lipton <zach@zachlipton.com>
# Joel Peshkin <bugreport@peshkin.net>
package Support::Files;
use File::Find;
# exclude_deps is a hash of arrays listing the files to be excluded
# if a module is not available
#
@additional_files = ();
%exclude_deps = (
# 'XML::Parser' => ['importxml.pl'],
);
@files = glob('*');
find(sub { push(@files, $File::Find::name) if $_ =~ /\.pm$/;}, 'Bugzilla');
sub have_pkg {
my ($pkg) = @_;
my ($msg, $vnum, $vstr);
no strict 'refs';
eval { my $p; ($p = $pkg . ".pm") =~ s!::!/!g; require $p; };
return !($@);
}
@exclude_files = ();
foreach $dep (keys(%exclude_deps)) {
if (!have_pkg($dep)) {
push @exclude_files, @{$exclude_deps{$dep}};
}
}
sub isTestingFile {
my ($file) = @_;
my $exclude;
foreach $exclude (@exclude_files) {
if ($file eq $exclude) { return undef; } # get rid of excluded files.
}
if ($file =~ /\.cgi$|\.pl$|\.pm$/) {
return 1;
}
my $additional;
foreach $additional (@additional_files) {
if ($file eq $additional) { return 1; }
}
return undef;
}
foreach $currentfile (@files) {
if (isTestingFile($currentfile)) {
push(@testitems,$currentfile);
}
}
1;

Просмотреть файл

@ -0,0 +1,14 @@
# -*- Mode: perl; indent-tabs-mode: nil -*-
package Support::Systemexec;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(system exec);
@EXPORT_OK = qw();
sub system($$@) {
1;
}
sub exec($$@) {
1;
}
1;

Просмотреть файл

@ -0,0 +1,146 @@
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code are the Bugzilla tests.
#
# The Initial Developer of the Original Code is Jacob Steenhagen.
# Portions created by Jacob Steenhagen are
# Copyright (C) 2001 Jacob Steenhagen. All
# Rights Reserved.
#
# Contributor(s): Jacob Steenhagen <jake@bugzilla.org>
# David D. Kilzer <ddkilzer@kilzer.net>
# Tobias Burnus <burnus@net-b.de>
#
package Support::Templates;
use strict;
use lib 't';
use base qw(Exporter);
@Support::Templates::EXPORT =
qw(@languages @include_paths %include_path @referenced_files
%actual_files $num_actual_files);
use vars qw(@languages @include_paths %include_path @referenced_files
%actual_files $num_actual_files);
use Support::Files;
use File::Find;
use File::Spec;
# The available template languages
@languages = ();
# The colon separated includepath per language
%include_path = ();
# All include paths
@include_paths = ();
# Files which are referenced in the cgi files
@referenced_files = ();
# All files sorted by include_path
%actual_files = ();
# total number of actual_files
$num_actual_files = 0;
# Scan for the template available languages and include paths
{
opendir(DIR, "template") || die "Can't open 'template': $!";
my @files = grep { /^[a-z-]+$/i } readdir(DIR);
closedir DIR;
foreach my $langdir (@files) {
next if($langdir =~ /^CVS$/i);
my $path = File::Spec->catdir('template', $langdir, 'custom');
my @dirs = ();
push(@dirs, $path) if(-d $path);
$path = File::Spec->catdir('template', $langdir, 'extension');
push(@dirs, $path) if(-d $path);
$path = File::Spec->catdir('template', $langdir, 'default');
push(@dirs, $path) if(-d $path);
next if(scalar(@dirs) == 0);
push(@languages, $langdir);
push(@include_paths, @dirs);
$include_path{$langdir} = join(":",@dirs);
}
}
my @files;
# Local subroutine used with File::Find
sub find_templates {
# Prune CVS directories
if (-d $_ && $_ eq 'CVS') {
$File::Find::prune = 1;
return;
}
# Only include files ending in '.tmpl'
if (-f $_ && $_ =~ m/\.tmpl$/i) {
my $filename;
my $local_dir = File::Spec->abs2rel($File::Find::dir,
$File::Find::topdir);
if ($local_dir) {
$filename = File::Spec->catfile($local_dir, $_);
} else {
$filename = $_;
}
push(@files, $filename);
}
}
# Scan the given template include path for templates
sub find_actual_files {
my $include_path = $_[0];
@files = ();
find(\&find_templates, $include_path);
return @files;
}
foreach my $include_path (@include_paths) {
$actual_files{$include_path} = [ find_actual_files($include_path) ];
$num_actual_files += scalar(@{$actual_files{$include_path}});
}
# Scan Bugzilla's perl code looking for templates used and put them
# in the @referenced_files array to be used by the 004template.t test.
my %seen;
foreach my $file (@Support::Files::testitems) {
open (FILE, $file);
my @lines = <FILE>;
close (FILE);
foreach my $line (@lines) {
if ($line =~ m/template->process\(\"(.+?)\", .+?\)/) {
my $template = $1;
# Ignore templates with $ in the name, since they're
# probably vars, not real files
next if $template =~ m/\$/;
next if $seen{$template};
push (@referenced_files, $template);
$seen{$template} = 1;
}
}
}
1;