From 4afae456014ff01ee01e11279fc5a9b5f4f7b5bf Mon Sep 17 00:00:00 2001 From: "bbaetz%student.usyd.edu.au" <> Date: Tue, 7 May 2002 16:45:28 +0000 Subject: Bug 135543 - @Support::Templates::testitems does not list all templates Patch by ddkilzer@theracingworld.com (David D. Kilzer), r=gerv, justdave --- globals.pl | 3 +++ t/004template.t | 44 +++++++++++++++++++++---------------- t/005no_tabs.t | 27 +++++++++++++---------- t/Support/Templates.pm | 59 ++++++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 93 insertions(+), 40 deletions(-) diff --git a/globals.pl b/globals.pl index 2bbaf3b15..f6e651e3c 100644 --- a/globals.pl +++ b/globals.pl @@ -1547,6 +1547,9 @@ $::template ||= Template->new( COMPILE_DIR => 'data/', # Functions for processing text within templates in various ways. + # IMPORTANT! When adding a filter here that does not override a + # built-in filter, please also add a stub filter to checksetup.pl + # and t/004template.t. FILTERS => { # Render text in strike-through style. diff --git a/t/004template.t b/t/004template.t index a63dbd64f..4332e2e82 100644 --- a/t/004template.t +++ b/t/004template.t @@ -18,25 +18,29 @@ # Rights Reserved. # # Contributor(s): Jacob Steenhagen -# Zach Lipton +# Zach Lipton +# David D. Kilzer # ################# #Bugzilla Test 4# ####Templates#### -BEGIN { use lib "t/"; } -BEGIN { use Support::Templates; } -BEGIN { $tests = @Support::Templates::testitems * 3; } -BEGIN { use Test::More tests => $tests; } - +use diagnostics; use strict; -use Template; + +use lib 't'; + +use Support::Templates; # Bug 137589 - Disable command-line input of CGI.pm when testing use CGI qw(-no_debug); -my @testitems = @Support::Templates::testitems; +use File::Spec 0.82; +use Template; +use Test::More tests => ( scalar(@Support::Templates::referenced_files) + + scalar(@Support::Templates::actual_files) * 2); + my $include_path = $Support::Templates::include_path; # Capture the TESTERR from Test::More for printing errors. # This will handle verbosity for us automatically @@ -45,13 +49,12 @@ my $include_path = $Support::Templates::include_path; # Check to make sure all templates that are referenced in # Bugzilla exist in the proper place. -my %exists; -foreach my $file(@testitems) { - if (-e $include_path . "/" . $file) { - ok(1, "$file exists"); - $exists{$file} = 1; +foreach my $file(@Support::Templates::referenced_files) { + my $path = File::Spec->catfile($include_path, $file); + if (-e $path) { + ok(1, "$path exists"); } else { - ok(0, "$file does not exist --ERROR"); + ok(0, "$path does not exist --ERROR"); } } @@ -61,6 +64,7 @@ my $template = Template->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. FILTERS => { js => sub { return $_ } , @@ -74,8 +78,9 @@ open SAVEOUT, ">&STDOUT"; # stash the original output stream open SAVEERR, ">&STDERR"; open STDOUT, "> /dev/null"; # discard all output open STDERR, "> /dev/null"; -foreach my $file(@testitems) { - if ($exists{$file}) { +foreach my $file(@Support::Templates::actual_files) { + my $path = File::Spec->catfile($include_path, $file); + if (-e $path) { if ($template->process($file)) { ok(1, "$file syntax ok"); } @@ -85,7 +90,7 @@ foreach my $file(@testitems) { } } else { - ok(1, "$file doesn't exist, skipping test"); + ok(1, "$path doesn't exist, skipping test"); } } open STDOUT, ">&SAVEOUT"; # redirect back to original stream @@ -95,8 +100,9 @@ close SAVEERR; # check to see that all templates have a version string: -foreach my $file(@testitems) { - open(TMPL,"$include_path/$file"); +foreach my $file(@Support::Templates::actual_files) { + my $path = File::Spec->catfile($include_path, $file); + open(TMPL, $path); my $firstline = ; if ($firstline =~ /\d+\.\d+\@[\w\.-]+/) { ok(1,"$file has a version string"); diff --git a/t/005no_tabs.t b/t/005no_tabs.t index a3c9fed3a..eaf1ef241 100644 --- a/t/005no_tabs.t +++ b/t/005no_tabs.t @@ -18,34 +18,37 @@ # Rights Reserved. # # Contributor(s): Jacob Steenhagen +# David D. Kilzer # ################# #Bugzilla Test 5# #####no_tabs##### -BEGIN { use lib "t/"; } -BEGIN { use Support::Files; } -BEGIN { use Support::Templates; } -BEGIN { $tests = @Support::Files::testitems; } -BEGIN { $tests += @Support::Templates::testitems; } -BEGIN { use Test::More tests => $tests; } - +use diagnostics; use strict; +use lib 't'; + +use Support::Files; +use Support::Templates; + +use File::Spec 0.82; +use Test::More tests => ( scalar(@Support::Files::testitems) + + scalar(@Support::Templates::actual_files)); + my @testitems = @Support::Files::testitems; -my @templates = map($Support::Templates::include_path . "/" . $_, - @Support::Templates::testitems); +my @templates = map(File::Spec->catfile($Support::Templates::include_path, $_), + @Support::Templates::actual_files); push(@testitems, @templates); foreach my $file (@testitems) { open (FILE, "$file"); - my @file = ; - close (FILE); - if (grep /\t/, @file) { + if (grep /\t/, ) { ok(0, "$file contains tabs --WARNING"); } else { ok(1, "$file has no tabs"); } + close (FILE); } diff --git a/t/Support/Templates.pm b/t/Support/Templates.pm index fb5303721..07f46f700 100644 --- a/t/Support/Templates.pm +++ b/t/Support/Templates.pm @@ -18,32 +18,73 @@ # Rights Reserved. # # Contributor(s): Jacob Steenhagen +# David D. Kilzer # package Support::Templates; +use diagnostics; +use strict; + +use lib 't'; +use vars qw($include_path @referenced_files @actual_files); + use Support::Files; -$include_path = "template/en/default"; +use File::Find; +use File::Spec 0.82; + +# Note that $include_path is assumed to only contain ONE path, not +# a list of colon-separated paths. +$include_path = File::Spec->catdir('template', 'en', 'default'); +@referenced_files = (); +@actual_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(@actual_files, $filename); + } +} -# Scan Bugzilla's code looking for templates used and put them -# in the @testitems array to be used by the template.t test. +# Scan the template include path for templates then put them in +# in the @actual_files array to be used by various tests. +map(find(\&find_templates, $_), split(':', $include_path)); -my @files = @Support::Files::testitems; -my %t; +# 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 (@files) { +foreach my $file (@Support::Files::testitems) { open (FILE, $file); my @lines = ; close (FILE); foreach my $line (@lines) { if ($line =~ m/template->process\(\"(.+?)\", .+?\)/) { - $template = $1; + my $template = $1; # Ignore templates with $ in the name, since they're # probably vars, not real files next if $template =~ m/\$/; - push (@testitems, $template) unless $t{$template}; - $t{$template} = 1; + next if $seen{$template}; + push (@referenced_files, $template); + $seen{$template} = 1; } } } -- cgit v1.2.3-24-g4f1b