lib/HTML/Mason/Tests.pm

filter: Statementstmt 81.2% 239 / 294 Branchbran 57.3% 102 / 178 Conditioncond 42.4% 28 / 66 MC/DCmcdc 5.2% 2 / 38 Subroutinesub 94.7% 36 / 38 Podpod 66.6% 8 / 12 totaltotal 66.2% 415 / 626 CC 104 SCAR 29.1
CC
104
Cov
69%
check_result
63.6
_run_tests
41.9
add_test
21.1
CRAP 18.4
« lib/HTML/Mason/ApacheHandler.pm ↑ Summary lib/HTML/Mason/Resolver/File.pm »
1 package HTML::Mason::Tests;
2 27 $HTML::Mason::Tests::VERSION = '1.60';
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 27 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 27 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 27 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 27 ▶use HTML::Mason;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use HTML::Mason::Compiler::ToObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 27 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 27 ▶use Test::Builder ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 27 ▶use vars qw($VERBOSE $DEBUG @SHARED);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 27 my $Test = Test::Builder->new;
21  
22 27 ▶$VERBOSE = $ENV{MASON_DEBUG} || $ENV{MASON_VERBOSE} || $ENV{TEST_VERBOSE};
Condition33%2/6
$ENV{'MASON_DEBUG'} || $ENV{'MASON_VERBOSE'} || $ENV{'TEST_VERBOSE'}
l!l&&r!l&&!r
0027
$ENV{"MASON_DEBUG"} or $ENV{"MASON_VERBOSE"}
l!l&&r!l&&!r
0027
MC/DC0%0/3
$ENV{'MASON_DEBUG'} || $ENV{'MASON_VERBOSE'} || $ENV{'TEST_VERBOSE'}
$ENV{"MASON_DEBUG"} $ENV{"MASON_VERBOSE"} $ENV{'TEST_VERBOSE'}
Truth table25%1/4
A or B || C
A= $ENV{"MASON_DEBUG"}B= $ENV{"MASON_VERBOSE"}C= $ENV{'TEST_VERBOSE'}
ABCresult
1XX1
01X1
0011
0000
23 27 $DEBUG = $ENV{MASON_DEBUG};
24  
25 27 $| = 1;
26  
27 27 @SHARED = ( { path => '/shared/check_error',
28 component => <<'EOF',
29 <% ($error) ? "Error: $error" : "No error!?" %>
30 <%init>
31 if ($error) {
32 my @lines = split("\n",$error);
33 $error = join("\n",@lines[0..$lines-1]);
34 $error =~ s{\s+at .*}{}g;
35 }
36 </%init>
37 <%args>
38 $error
39 $lines=>1
40 </%args>
41 EOF
42 },
43 { path => '/shared/display_comp_obj',
44 component => <<'EOF',
45 Declared args:
46 % my %decl = %{$comp->declared_args};
47 % foreach (sort keys %decl) {
48 <% $_ %><% (defined($decl{$_}->{default})) ? "=>".$decl{$_}->{default} : "" %>
49 % }
50  
51 I am <% $comp->is_subcomp ? '' : 'not ' %>a subcomponent.
52 I am <% $comp->is_method ? '' : 'not ' %>a method.
53 I am <% $comp->is_file_based ? '' : 'not ' %>file-based.
54 % if (defined($comp->name)) {
55 My short name is <% $comp->name =~ /anon/ ? '[anon something]' : $comp->name %>.
56 % }
57 % if ($comp->is_subcomp and defined($comp->owner)) {
58 My parent component is <% $comp->owner->title %>.
59 % }
60 % if (defined($comp->dir_path)) {
61 My directory is <% $comp->dir_path %>.
62 % }
63 % my @subkeys = sort keys(%{$comp->subcomps});
64 I have <% scalar(@subkeys) %> subcomponent(s).
65 % if (@subkeys) {
66 Including one called <% $comp->subcomps($subkeys[0])->name %>.
67 % }
68 My title is <% $comp->title =~ /anon/ ? '[anon something]' : $comp->title %>.
69  
70 % if (defined($comp->path)) {
71 My path is <% $comp->path %>.
72 % }
73 % if (defined($comp->comp_id)) {
74 My comp_id is <% $comp->comp_id =~ /anon/ ? '[anon something]' : $comp->comp_id %>.
75 % }
76 <%args>
77 $comp
78 </%args>
79 EOF
80 },
81 { path => '/shared/display_req_obj',
82 component => <<'EOF',
83 My depth is <% $m->depth %>.
84  
85 I <% $m->is_subrequest ? 'am' : 'am not' %> a subrequest.
86  
87 The top-level component is <% $m->request_comp->title %>.
88  
89 My stack looks like:
90 -----
91 % foreach my $comp ($m->callers) {
92 <% $comp->title %>
93 % }
94 -----
95  
96 EOF
97 },
98 );
99  
100 #
101 # Get command options here so that we read tests_class before user
102 # calls new().
103 #
104 27 my %cmd_options;
105 GetOptions( 'create' => \$cmd_options{create},
106 'tests-to-run=s' => \$cmd_options{tests_to_run},
107 'tests-to-skip=s' => \$cmd_options{tests_to_skip},
108 'tests-class=s' => \$cmd_options{tests_class},
109 27 );
110  
111 #
112 # Allow options to be passed in the environment as well.
113 #
114 $cmd_options{tests_to_run} = $ENV{MASON_TESTS_TO_RUN}
115 27 ▶ if !defined($cmd_options{tests_to_run}) and defined($ENV{MASON_TESTS_TO_RUN});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cmd_options{"tests_to_run"} and defined $ENV{"MASON_TESTS_TO_RUN"} 0 27 27
Condition33%1/3
not defined $cmd_options{"tests_to_run"} and defined $ENV{"MASON_TESTS_TO_RUN"}
!ll&&!rl&&r
0270
MC/DC0%0/2
not defined $cmd_options{"tests_to_run"} and defined $ENV{"MASON_TESTS_TO_RUN"}
not defined $cmd_options{"tests_to_run"} defined $ENV{"MASON_TESTS_TO_RUN"}
Truth table33%1/3
not(A) and B
A= not defined $cmd_options{"tests_to_run"}B= defined $ENV{"MASON_TESTS_TO_RUN"}
ABresult
0X0
100
111
116 $cmd_options{tests_to_skip} = $ENV{MASON_TESTS_TO_SKIP}
117 27 ▶ if !defined($cmd_options{tests_to_skip}) and defined($ENV{MASON_TESTS_TO_SKIP});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cmd_options{"tests_to_skip"} and defined $ENV{"MASON_TESTS_TO_SKIP"} 0 27 27
Condition33%1/3
not defined $cmd_options{"tests_to_skip"} and defined $ENV{"MASON_TESTS_TO_SKIP"}
!ll&&!rl&&r
0270
MC/DC0%0/2
not defined $cmd_options{"tests_to_skip"} and defined $ENV{"MASON_TESTS_TO_SKIP"}
not defined $cmd_options{"tests_to_skip"} defined $ENV{"MASON_TESTS_TO_SKIP"}
Truth table33%1/3
not(A) and B
A= not defined $cmd_options{"tests_to_skip"}B= defined $ENV{"MASON_TESTS_TO_SKIP"}
ABresult
0X0
100
111
118 $cmd_options{tests_class} = $ENV{MASON_TESTS_CLASS}
119 27 ▶ if !defined($cmd_options{tests_class}) and defined($ENV{MASON_TESTS_CLASS});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cmd_options{"tests_class"} and defined $ENV{"MASON_TESTS_CLASS"} 0 27 27
Condition33%1/3
not defined $cmd_options{"tests_class"} and defined $ENV{"MASON_TESTS_CLASS"}
!ll&&!rl&&r
0270
MC/DC0%0/2
not defined $cmd_options{"tests_class"} and defined $ENV{"MASON_TESTS_CLASS"}
not defined $cmd_options{"tests_class"} defined $ENV{"MASON_TESTS_CLASS"}
Truth table33%1/3
not(A) and B
A= not defined $cmd_options{"tests_class"}B= defined $ENV{"MASON_TESTS_CLASS"}
ABresult
0X0
100
111
120  
121 # If user specifies tests_class, load that package; otherwise,
122 # default it to this package.
123 27 ▶if (defined($cmd_options{tests_class})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmd_options{'tests_class'}) { } 0 27 27
124 0 eval "use $cmd_options{tests_class}";
125 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
126 } else {
127 27 $cmd_options{tests_class} = __PACKAGE__;
128 }
129  
130 27 my %tests_to_run;
131 27 ▶if ($cmd_options{tests_to_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd_options{"tests_to_run"}) 0 27 27
132 0 for ($cmd_options{tests_to_run}) { s/^\s+//; s/\s+$// }
133 0 my @tests_to_run = split(/\s*,\s*/, $cmd_options{tests_to_run});
134 0 ▶ if (grep { /[^0-9]/ } @tests_to_run) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/[^0-9]/;} @tests_to_run) { } 0 0 0
135 0 @tests_to_run = sort { $a cmp $b } @tests_to_run;
136 } else {
137 0 @tests_to_run = sort { $a <=> $b } @tests_to_run;
138 }
139 0 %tests_to_run = map { ($_, 1) } @tests_to_run;
140 0 ▶ $Test->diag(sprintf("Running only test%s %s\n", @tests_to_run == 1 ? "" : "s", join(", ", @tests_to_run)))
Branch: 1 branch
BranchTrueFalseTotal
@tests_to_run == 1 ? : 0 0 0
141 }
142  
143 27 my %tests_to_skip;
144 27 ▶if ($cmd_options{tests_to_skip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd_options{"tests_to_skip"}) 0 27 27
145 0 for ($cmd_options{tests_to_skip}) { s/^\s+//; s/\s+$// }
146 0 my @tests_to_skip = split(/\s*,\s*/, $cmd_options{tests_to_skip});
147 0 %tests_to_skip = map { ($_, 1) } @tests_to_skip;
148 0 ▶ $Test->diag(printf ("Skipping test%s %s\n", @tests_to_skip == 1 ? "" : "s", join(", ", @tests_to_skip)));
Branch: 1 branch
BranchTrueFalseTotal
@tests_to_skip == 1 ? : 0 0 0
149 }
150  
151 sub new
152 27 ▶{
Subroutine new: called CC=4 SCAR=14.0
Pod documented
153 27 my $class = shift;
154 27 my %p = (@_, %cmd_options);
155  
156 die "No group name provided\n"
157 27 ▶ unless exists $p{name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"name"} 0 27 27
158  
159 die "No description for test group provided\n"
160 27 ▶ unless exists $p{description};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"description"} 0 27 27
161  
162 $p{pre_test_cleanup} = 1
163 27 ▶ unless exists $p{pre_test_cleanup};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"pre_test_cleanup"} 26 1 27
164  
165 27 return bless {
166 %p,
167 support => [],
168 tests => [],
169 }, $class;
170 }
171  
172 # Returns the tests class to use for class methods - defaults to this package.
173 sub tests_class
174 35 ▶{
Subroutine tests_class: called CC=1 SCAR=0.0
Pod undocumented
175 35 return $cmd_options{tests_class};
176 }
177  
178 sub add_support
179 161 ▶{
Subroutine add_support: called CC=3 SCAR=11.2
Pod documented
180 161 my $self = shift;
181 161 my %p = @_;
182  
183 die "'support' key array member contains no 'path' key\n"
184 161 ▶ unless exists $p{path};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"path"} 0 161 161
185  
186 die "'support' key array member contains no 'component' key\n"
187 161 ▶ unless exists $p{component};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"component"} 0 161 161
188  
189 161 push @{ $self->{support} }, \%p;
190 }
191  
192 sub add_test
193 409 ▶{
Subroutine add_test: called CC=18 SCAR=30.5
Pod documented
194 409 my $self = shift;
195 409 my %p = @_;
196  
197 die "no name provided for test\n"
198 409 ▶ unless exists $p{name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"name"} 0 409 409
199  
200 409 ▶ unless ( exists $p{path} )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $p{"path"}) 375 34 409
201 {
202 375 ▶ $p{path} = $p{call_path} || $p{name};
Condition66%2/3
$p{'call_path'} || $p{'name'}
l!l&&r!l&&!r
253500
MC/DC0%0/2
$p{'call_path'} || $p{'name'}
$p{'call_path'} $p{'name'}
Truth table66%2/3
A || B
A= $p{'call_path'}B= $p{'name'}
ABresult
1X1
011
000
203 }
204  
205 409 my $call_path = "/$self->{name}";
206 409 ▶ if ( exists $p{call_path} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p{'call_path'}) { } 59 350 409
207 {
208 59 ▶ $call_path .= '/' unless substr( $p{call_path}, 0, 1 ) eq '/';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($p{"call_path"}, 0, 1) eq "/" 17 42 59
209 59 $call_path .= $p{call_path};
210 }
211 else
212 {
213 350 $call_path .= '/' . $p{name};
214 }
215 409 $p{call_path} = $call_path;
216  
217 409 ▶ if ( ref($p{call_args}) eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $p{'call_args'} eq 'HASH') { } 5 404 409
elsif (not exists $p{'call_args'}) { } 401 3 404
218 {
219 5 my @lst = %{$p{call_args}};
220 5 $p{call_args} = \@lst;
221 }
222 elsif ( !exists($p{call_args}) ) {
223 401 $p{call_args} = [];
224 }
225  
226 die "'$p{name}' test has no description\n"
227 409 ▶ unless exists $p{description};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"description"} 0 409 409
228  
229 die "'$p{name}' test has no component\n"
230 409 ▶ unless exists $p{component} || $p{skip_component};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"component"} or $p{"skip_component"} 0 409 409
Condition66%2/3
exists $p{"component"} or $p{"skip_component"}
l!l&&r!l&&!r
390190
MC/DC0%0/2
exists $p{"component"} or $p{"skip_component"}
exists $p{"component"} $p{"skip_component"}
Truth table66%2/3
A or B
A= exists $p{"component"}B= $p{"skip_component"}
ABresult
1X1
011
000
231  
232 die "'$p{name}' test has no 'expect' or 'expect_error' key\n"
233 409 ▶ unless exists $p{expect} || exists $p{expect_error} || $p{skip_expect} || $self->{create};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"expect"} or exists $p{"expect_error"} or $p{"skip_expect"} or $self->{"create"} 0 409 409
Condition44%4/9
exists $p{"expect"} or exists $p{"expect_error"} or $p{"skip_expect"} or $self->{"create"}
l!l&&r!l&&!r
40900
exists $p{"expect"} or exists $p{"expect_error"} or $p{"skip_expect"}
l!l&&r!l&&!r
40900
exists $p{"expect"} or exists $p{"expect_error"}
l!l&&r!l&&!r
333760
MC/DC0%0/4
exists $p{"expect"} or exists $p{"expect_error"} or $p{"skip_expect"} or $self->{"create"}
exists $p{"expect"} exists $p{"expect_error"} $p{"skip_expect"} $self->{"create"}
Truth table40%2/5
A or B or C or D
A= exists $p{"expect"}B= exists $p{"expect_error"}C= $p{"skip_expect"}D= $self->{"create"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
234  
235 409 foreach ( qw( interp_params ) )
236 {
237 die "$_ must be a hash reference"
238 409 ▶ if exists $p{$_} && ! UNIVERSAL::isa( $p{$_}, 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{$_} and not UNIVERSAL::isa($p{$_}, "HASH") 0 409 409
Condition66%2/3
exists $p{$_} and not UNIVERSAL::isa($p{$_}, "HASH")
!ll&&!rl&&r
2731360
MC/DC0%0/2
exists $p{$_} and not UNIVERSAL::isa($p{$_}, "HASH")
exists $p{$_} not UNIVERSAL::isa($p{$_}, "HASH")
Truth table66%2/3
A and not(B)
A= exists $p{$_}B= not UNIVERSAL::isa($p{$_}, "HASH")
ABresult
0X0
100
111
239 }
240  
241 409 push @{ $self->{tests} }, \%p;
242 }
243  
244 sub run
245 26 ▶{
Subroutine run: called CC=7 SCAR=20.9
Pod documented
246 26 my $self = shift;
247  
248 die "No tests exist in this group"
249 26 ▶ unless @{ $self->{tests} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"tests"};} 0 26 26
250  
251 26 ▶ if ($DEBUG)
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Mason::Tests::DEBUG) 0 26 26
252 {
253 0 ▶ $Test->diag( "Will " . ( $self->{create} ? '' : 'not ' ) . "create 'expect' files\n" );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'create'} ? : 0 0 0
254 }
255  
256 eval
257 26 {
258 # 1 indicates to be silent on missing directories
259 26 ▶ $self->_cleanup(1) if $self->{pre_test_cleanup};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pre_test_cleanup"} 25 1 26
260 26 $self->_make_dirs;
261 26 $self->_write_shared_comps;
262 26 $self->_write_support_comps;
263 26 $self->_run_tests;
264 };
265  
266 26 ▶ $self->_cleanup unless $ENV{MASON_NO_CLEANUP};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"MASON_NO_CLEANUP"} 26 0 26
267  
268 26 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 26 26
269 }
270  
271 sub _make_dirs
272 26 ▶{
Subroutine _make_dirs: called CC=7 SCAR=20.4
273 26 my $self = shift;
274  
275 26 my $comp_root = $self->comp_root;
276 26 my $data_dir = $self->data_dir;
277  
278 26 ▶ unless ( -d $self->comp_root )
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->comp_root) 26 0 26
279 {
280 26 ▶ $Test->diag( "Making comp_root directory: $comp_root\n" ) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Tests::DEBUG 0 26 26
281 26 ▶ mkpath( $self->comp_root, 0, 0755 )
Branch: 1 branch
BranchTrueFalseTotal
mkpath($self->comp_root, 0, 493) or die "Unable to make base test directory '${comp_root}': $!" 26 0 26
282 or die "Unable to make base test directory '$comp_root': $!";
283 }
284  
285 26 ▶ unless ( -d $self->data_dir )
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->data_dir) 25 1 26
286 {
287 25 ▶ $Test->diag( "Making data_dir directory: $data_dir\n" ) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Tests::DEBUG 0 25 25
288 25 ▶ mkpath( $self->data_dir, 0, 0755 )
Branch: 1 branch
BranchTrueFalseTotal
mkpath($self->data_dir, 0, 493) or die "Unable to make base test directory '${data_dir}': $!" 25 0 25
289 or die "Unable to make base test directory '$data_dir': $!";
290 }
291 }
292  
293 sub base_path
294 1686 ▶{
Subroutine base_path: called CC=3 SCAR=11.0
Pod documented
295 1686 my $proto = shift;
296  
297 1686 ▶ if (ref $proto)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) { } 1682 4 1686
298 {
299 1682 ▶ $proto->{base_path} ||= File::Spec->catdir( cwd(), 'mason_tests', $$ );
Condition66%2/3
$proto->{"base_path"} ||= "File::Spec"->catdir(cwd(), "mason_tests", $$)
l!l&&r!l&&!r
1656260
MC/DC0%0/2
$proto->{"base_path"} ||= "File::Spec"->catdir(cwd(), "mason_tests", $$)
$proto->{"base_path"} "File::Spec"->catdir(cwd(), "mason_tests", $$)
Truth table66%2/3
A ||= B
A= $proto->{"base_path"}B= "File::Spec"->catdir(cwd(), "mason_tests", $$)
ABresult
1X1
011
000
300 1682 return $proto->{base_path};
301 }
302 else
303 {
304 4 return File::Spec->catdir( cwd(), 'mason_tests', $$ );
305 }
306 }
307  
308 sub comp_root
309 1159 ▶{
Subroutine comp_root: called CC=1 SCAR=0.0
Pod documented
310 1159 my $proto = shift;
311  
312 1159 return File::Spec->catdir( $proto->base_path, 'comps' );
313 }
314  
315 sub data_dir
316 474 ▶{
Subroutine data_dir: called CC=1 SCAR=0.0
Pod documented
317 474 my $proto = shift;
318  
319 474 return File::Spec->catdir( $proto->base_path, 'data' );
320 }
321  
322 sub _write_shared_comps
323 26 ▶{
Subroutine _write_shared_comps: called CC=3 SCAR=11.0
324 26 my $self = shift;
325  
326 26 ▶ return unless @SHARED;
Branch: 1 branch
BranchTrueFalseTotal
unless @HTML::Mason::Tests::SHARED 0 26 26
327  
328 26 foreach my $comp ( @SHARED )
329 {
330 78 my @path = split m(/), $comp->{path};
331 78 my $file = pop @path;
332  
333 78 my $dir = File::Spec->catdir( $self->comp_root, @path );
334  
335 78 $self->write_comp( $comp->{path}, $dir, $file, $comp->{component} );
336 }
337 }
338  
339 sub _write_support_comps
340 26 ▶{
Subroutine _write_support_comps: called CC=4 SCAR=13.9
341 26 my $self = shift;
342  
343 26 ▶ unless ( @{ $self->{support} } )
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"support"};}) 5 21 26
344 {
345 5 ▶ $Test->diag( "No support comps to create\n" ) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Tests::DEBUG 0 5 5
346 5 return;
347 }
348  
349 21 foreach my $supp ( @{ $self->{support} } )
350 {
351 161 my @path = split m(/), $supp->{path};
352 161 my $file = pop @path;
353  
354 161 my $dir = File::Spec->catdir( $self->comp_root, $self->{name}, @path );
355  
356 161 $self->write_comp( $supp->{path}, $dir, $file, $supp->{component} );
357 }
358 }
359  
360 sub _write_test_comp
361 390 ▶{
Subroutine _write_test_comp: called CC=4 SCAR=14.0
362 390 my $self = shift;
363 390 my $test = $self->{current_test};
364  
365 390 my @path = split m(/), $test->{path};
366 390 my $file = pop @path;
367  
368 390 my $dir = File::Spec->catdir( $self->comp_root, $self->{name}, @path );
369 390 ▶ unless ( -d $dir )
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 8 382 390
370 {
371 8 ▶ $Test->diag( "Making dir: $dir\n" ) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Tests::DEBUG 0 8 8
372 8 ▶ mkpath( $dir, 0, 0755 )
Branch: 1 branch
BranchTrueFalseTotal
mkpath($dir, 0, 493) or die "Unable to create directory '${dir}': $!" 8 0 8
373 or die "Unable to create directory '$dir': $!";
374 }
375  
376 390 $self->write_comp( $test->{path}, $dir, $file, $test->{component} );
377 }
378  
379 sub write_comp
380 631 ▶{
Subroutine write_comp: called CC=8 SCAR=22.0
Pod undocumented
381 631 my $self = shift;
382 631 my ($path, $dir, $file, $component) = @_;
383  
384 631 ▶ unless (-d $dir)
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 88 543 631
385 {
386 88 ▶ $Test->diag( "Making dir: $dir\n" ) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Tests::DEBUG 0 88 88
387 88 ▶ mkpath( $dir, 0, 0755 )
Branch: 1 branch
BranchTrueFalseTotal
mkpath($dir, 0, 493) or die "Unable to create directory '${dir}': $!" 88 0 88
388 or die "Unable to create directory '$dir': $!";
389 }
390  
391 631 my $real_file = File::Spec->catfile( $dir, $file );
392  
393 631 ▶ $Test->diag( "Making component $path at $real_file\n" )
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Tests::DEBUG 0 631 631
394 if $DEBUG;
395  
396 631 ▶ open my $fh, ">$real_file"
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">$real_file" or die "Unable to write to '${real_file}': $!" 631 0 631
397 or die "Unable to write to '$real_file': $!";
398 631 ▶ print $fh $component
Branch: 1 branch
BranchTrueFalseTotal
print $fh $component or die "Unable to write to '${real_file}': $!" 631 0 631
399 or die "Unable to write to '$real_file': $!";
400 631 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Unable to write to '${real_file}': $!" 631 0 631
401 or die "Unable to write to '$real_file': $!";
402 }
403  
404 sub _run_tests
405 26 ▶{
Subroutine _run_tests: called CC=12 SCAR=37.3
406 26 my $self = shift;
407  
408 26 my $count = scalar @{ $self->{tests} };
409 26 $Test->plan( tests => $count );
410  
411 26 ▶ if ($VERBOSE)
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Mason::Tests::VERBOSE) 0 26 26
412 {
413 0 $Test->diag( "Running $self->{name} tests ($count tests): $self->{description}\n" );
414 }
415  
416 26 my $x = 1;
417 26 foreach my $test ( @{ $self->{tests} } )
418 {
419 409 $self->{current_test} = $test;
420  
421 #
422 # If tests_to_run or tests_to_skip were specified in the
423 # environment or command line, check them to see whether to
424 # run the test.
425 #
426 409 ▶ if (%tests_to_run or %tests_to_skip) {
Branch: 1 branch
BranchTrueFalseTotal
if (%tests_to_run or %tests_to_skip) 0 409 409
Condition33%1/3
%tests_to_run or %tests_to_skip
l!l&&r!l&&!r
00409
MC/DC0%0/2
%tests_to_run or %tests_to_skip
%tests_to_run %tests_to_skip
Truth table33%1/3
A or B
A= %tests_to_runB= %tests_to_skip
ABresult
1X1
011
000
427  
428 # Look for any of the specs [test_file_name:](test_number|test_name|*)
429 0 my $wildcard_name = join(":", $self->{name}, "*");
430 0 my $full_name = join(":", $self->{name}, $test->{name});
431 0 my $full_number = join(":", $self->{name}, $x);
432 0 my @all_specs = ($x, $test->{name}, $full_name, $full_number, $wildcard_name);
433  
434 # If our test isn't mentioned in %tests_to_run or is
435 # mentioned in %tests_to_skip, skip it.
436 #
437 0 ▶ if ((%tests_to_run and !(grep { $tests_to_run{$_} } @all_specs))
Branch: 1 branch
BranchTrueFalseTotal
if (%tests_to_run and not grep({$tests_to_run{$_};} @all_specs) or %tests_to_skip and grep {$tests_to_skip{$_};} @all_specs) 0 0 0
Condition0%0/9
%tests_to_run and not grep({$tests_to_run{$_};} @all_specs) or %tests_to_skip and grep {$tests_to_skip{$_};} @all_specs
l!l&&r!l&&!r
000
%tests_to_run and not grep({$tests_to_run{$_};} @all_specs)
!ll&&!rl&&r
000
%tests_to_skip and grep {$tests_to_skip{$_};} @all_specs
!ll&&!rl&&r
000
MC/DC0%0/4
%tests_to_run and not grep({$tests_to_run{$_};} @all_specs) or %tests_to_skip and grep {$tests_to_skip{$_};} @all_specs
%tests_to_run not grep({$tests_to_run{$_};} @all_specs) %tests_to_skip grep {$tests_to_skip{$_};} @all_specs
Truth table0%0/7
A and not(B) or C and D
A= %tests_to_runB= not grep({$tests_to_run{$_};} @all_specs)C= %tests_to_skipD= grep {$tests_to_skip{$_};} @all_specs
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
438 0 or (%tests_to_skip and (grep { $tests_to_skip{$_} } @all_specs))) {
439  
440 # Use presence of PERL_DL_NONLAZY to decide if we are
441 # running inside "make test", and if so, actually
442 # print the appropriate skip response to comply with the
443 # Test::Harness standard. If the user is running the
444 # test by hand, this would just be clutter.
445 #
446 # Checking PERL_DL_NONLAZY is a hack but I don't
447 # know of a better detection method.
448 #
449 0 ▶ $self->_skip if ($ENV{PERL_DL_NONLAZY});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PERL_DL_NONLAZY"} 0 0 0
450 0 $x++;
451 0 next;
452 }
453 }
454 409 ▶ $Test->diag( "Running $test->{name} (#$x): $test->{description}\n" ) if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Tests::VERBOSE 0 409 409
455 409 ▶ $self->_make_component unless $test->{skip_component};
Branch: 1 branch
BranchTrueFalseTotal
unless $test->{"skip_component"} 390 19 409
456 409 $self->_run_test;
457 409 $x++;
458 }
459 }
460  
461 sub _make_component
462 390 ▶{
Subroutine _make_component: called CC=1 SCAR=0.0
463 390 my $self = shift;
464 390 my $test = $self->{current_test};
465 390 $self->_write_test_comp;
466 }
467  
468 sub _make_main_interp
469 402 ▶{
Subroutine _make_main_interp: called CC=6 SCAR=20.8
470 402 my $self = shift;
471 402 my $test = $self->{current_test};
472 402 ▶ return $test->{interp} if $test->{interp};
Branch: 1 branch
BranchTrueFalseTotal
if $test->{"interp"} 16 386 402
473  
474 my %interp_params = ( exists $test->{interp_params} ?
475 386 ▶ %{ $test->{interp_params} } :
Branch: 1 branch
BranchTrueFalseTotal
exists $test->{'interp_params'} ? : 135 251 386
476 () );
477  
478 386 ▶ if ($DEBUG && %interp_params)
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Mason::Tests::DEBUG and %interp_params) 0 386 386
Condition33%1/3
$HTML::Mason::Tests::DEBUG and %interp_params
!ll&&!rl&&r
38600
MC/DC0%0/2
$HTML::Mason::Tests::DEBUG and %interp_params
$HTML::Mason::Tests::DEBUG %interp_params
Truth table33%1/3
A and B
A= $HTML::Mason::Tests::DEBUGB= %interp_params
ABresult
0X0
100
111
479 {
480 0 $Test->diag( "Interp params:\n" );
481 0 ▶ while ( my ($k, $v) = each %interp_params)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %interp_params) 0 0 0
482 {
483 0 $Test->diag( " $k => $v\n" );
484 }
485 }
486  
487 386 return $self->_make_interp ( comp_root => $self->comp_root,
488 data_dir => $self->data_dir,
489 %interp_params );
490 }
491  
492 sub _make_interp
493 396 ▶{
Subroutine _make_interp: called CC=1 SCAR=0.0
494 396 my ($class, %interp_params) = @_;
495  
496 396 return HTML::Mason::Interp->new( %interp_params );
497 }
498  
499 sub _run_test
500 402 ▶{
Subroutine _run_test: called CC=2 SCAR=6.9
501 402 my $self = shift;
502 402 my $test = $self->{current_test};
503  
504 402 $self->{buffer} = '';
505 402 my $interp = $self->_make_main_interp;
506 402 ▶ $interp->out_method( sub { for (@_) { $self->{buffer} .= $_ if defined $_ } } );
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 583 0 583
507  
508 402 my $warnings = '';
509 402 ▶ local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
Subroutine __ANON__: called CC=2 SCAR=6.9
510 402 eval {
511 # Run pre_code if test has it - pass in interp
512 402 ▶ if ($test->{pre_code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->{"pre_code"}) 18 384 402
513 18 $test->{pre_code}->($interp);
514 }
515 399 $self->_execute($interp);
516 };
517  
518 402 return $self->check_result($@, $warnings);
519 }
520  
521 sub _execute
522 399 ▶{
Subroutine _execute: called CC=3 SCAR=11.0
523 399 my ($self, $interp) = @_;
524 399 my $test = $self->{current_test};
525  
526 399 ▶ $Test->diag( "Calling $test->{name} test with path: $test->{call_path}\n" ) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Tests::DEBUG 0 399 399
527 399 ▶ $test->{pretest_code}->() if $test->{pretest_code};
Branch: 1 branch
BranchTrueFalseTotal
if $test->{"pretest_code"} 1 398 399
528 399 $interp->exec( $test->{call_path}, @{$test->{call_args}} );
529 }
530  
531 409 ▶sub check_result {
Subroutine check_result: called CC=20 SCAR=41.5
Pod undocumented
532 409 my ($self, $error, $warnings) = @_;
533 409 my $test = $self->{current_test};
534  
535 local $HTML::Mason::Tests::TODO = $self->{current_test}{todo}
536 409 ▶ if exists $self->{current_test}{todo};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"current_test"}{"todo"} 0 409 409
537 409 ▶ $Test->todo if exists $self->{current_test}{todo};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"current_test"}{"todo"} 0 409 409
538  
539 409 ▶ if ($error)
Branch: 2 branches
BranchTrueFalseTotal
if ($error) { } 76 333 409
elsif ($test->{'expect_error'}) { } 0 333 333
540 {
541 76 ▶ if ( $test->{expect_error} )
Branch: 1 branch
BranchTrueFalseTotal
if ($test->{'expect_error'}) { } 76 0 76
542 {
543 76 ▶ if ( $error =~ /$test->{expect_error}/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($error =~ /$test->{'expect_error'}/) { } 76 0 76
544 {
545 76 return $self->_success
546 }
547 else
548 {
549 0 ▶ if ($VERBOSE)
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Mason::Tests::VERBOSE) 0 0 0
550 {
551 0 $Test->diag( "Got error:\n$error\n...but expected something matching:\n$test->{expect_error}\n" );
552 }
553 0 return $self->_fail;
554 }
555 }
556 else
557 {
558 0 ▶ $Test->diag( "Unexpected error running $test->{name}:\n$error" ) if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Tests::VERBOSE 0 0 0
559 0 return $self->_fail;
560 }
561  
562 }
563 elsif ( $test->{expect_error} )
564 {
565 0 ▶ $Test->diag( "Expected an error matching '$test->{expect_error}' but no error occurred - got successful output:\n$self->{buffer}\n" ) if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Tests::VERBOSE 0 0 0
566 0 return $self->_fail;
567 }
568  
569 333 ▶ if ($self->{create})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"create"}) 0 333 333
570 {
571 0 $Test->diag( "Results for $test->{name}:\n$self->{buffer}\n" );
572 0 return;
573 }
574  
575 my $success =
576 ( $test->{skip_expect} ?
577 1 :
578 $self->check_output( actual => $self->{buffer}, expect => $test->{expect} )
579 333 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$test->{'skip_expect'} ? : 0 333 333
580  
581 333 ▶ if ( $test->{expect_warnings} )
Branch: 1 branch
BranchTrueFalseTotal
if ($test->{"expect_warnings"}) 5 328 333
582 {
583 5 ▶ unless ( $warnings =~ /$test->{expect_warnings}/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($warnings =~ /$test->{"expect_warnings"}/) 0 5 5
584 {
585 0 $Test->diag( "Got warnings:\n$warnings\n...but expected something matching:\n$test->{expect_warnings}\n" );
586 0 $success = 0;
587 }
588 }
589  
590 333 ▶ $Test->diag( "Got warnings: $warnings" ) if $warnings && ( ! $test->{expect_warnings} || $VERBOSE );
Branch: 1 branch
BranchTrueFalseTotal
if $warnings and !$test->{"expect_warnings"} || $HTML::Mason::Tests::VERBOSE 0 333 333
Condition50%3/6
$warnings and !$test->{"expect_warnings"} || $HTML::Mason::Tests::VERBOSE
!ll&&!rl&&r
32850
not $test->{"expect_warnings"} or $HTML::Mason::Tests::VERBOSE
l!l&&r!l&&!r
005
MC/DC0%0/3
$warnings and !$test->{"expect_warnings"} || $HTML::Mason::Tests::VERBOSE
$warnings not $test->{"expect_warnings"} $HTML::Mason::Tests::VERBOSE
Truth table50%2/4
A and not(B) or C
A= $warningsB= not $test->{"expect_warnings"}C= $HTML::Mason::Tests::VERBOSE
ABCresult
0XX0
1000
11X1
1011
591 333 ▶ $success = 0 if $test->{no_warnings} && $warnings;
Branch: 1 branch
BranchTrueFalseTotal
if $test->{"no_warnings"} and $warnings 0 333 333
Condition66%2/3
$test->{"no_warnings"} and $warnings
!ll&&!rl&&r
33210
MC/DC0%0/2
$test->{"no_warnings"} and $warnings
$test->{"no_warnings"} $warnings
Truth table66%2/3
A and B
A= $test->{"no_warnings"}B= $warnings
ABresult
0X0
100
111
592  
593 333 ▶ $success ? $self->_success : $self->_fail;
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 333 0 333
594 }
595  
596 sub check_output
597 333 ▶{
Subroutine check_output: called CC=5 SCAR=16.7
Pod documented
598 333 my ($self, %p) = @_;
599  
600 333 my $same;
601  
602 # Allow a regex for $p{expect}
603 333 ▶ if (ref $p{expect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p{'expect'}) { } 11 322 333
604 11 $same = ($p{actual} =~ /$p{expect}/);
605  
606 } else {
607 # Whitespace at end can vary. (Or rather, it is varying in the tests, and
608 # should be made not to vary, but I don't have time to fix it yet.)
609  
610 322 for ($p{actual}, $p{expect}) { s/\s+$// }
611 322 $same = ($p{actual} eq $p{expect});
612 }
613  
614 333 ▶ if (!$same and $VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $same and $HTML::Mason::Tests::VERBOSE) 0 333 333
Condition33%1/3
not $same and $HTML::Mason::Tests::VERBOSE
!ll&&!rl&&r
33300
MC/DC0%0/2
not $same and $HTML::Mason::Tests::VERBOSE
not $same $HTML::Mason::Tests::VERBOSE
Truth table33%1/3
not(A) and B
A= not $sameB= $HTML::Mason::Tests::VERBOSE
ABresult
0X0
100
111
615 0 $Test->diag( "Got ...\n-----\n$p{actual}\n-----\n ... but expected ...\n-----\n$p{expect}\n-----\n" );
616 }
617 333 return $same;
618 }
619  
620 sub _fail
621 0 ▶{
Subroutine _fail: not called CC=1 SCAR=6.9
622 0 my $self = shift;
623 0 my $test = $self->{current_test};
624  
625 0 $Test->ok( 0, $test->{name} );
626 }
627  
628 sub _success
629 409 ▶{
Subroutine _success: called CC=1 SCAR=0.0
630 409 my $self = shift;
631 409 my $test = $self->{current_test};
632  
633 409 $Test->ok( 1, $test->{name} );
634 }
635  
636 sub _skip
637 0 ▶{
Subroutine _skip: not called CC=1 SCAR=6.9
638 0 my $self = shift;
639 0 my $test = $self->{current_test};
640  
641 0 $Test->skip;
642 }
643  
644 #
645 # We use our own rm_tree, rather than File::Path::rmtree, so that we
646 # can silently fail to entirely remove directories. On some systems
647 # .nfs files prevent total removal of directories but should not
648 # otherwise interfere with tests.
649 #
650 1837 ▶sub rm_tree {
Subroutine rm_tree: called CC=8 SCAR=20.8
Pod undocumented
651 1837 my ($path, $debug, $silent) = @_;
652 1837 $path =~ s#/$##;
653 1837 ▶ if (-d $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $path) { } 593 1244 1837
elsif (-f $path) { } 1220 24 1244
654 593 local *DIR;
655 593 ▶ opendir DIR, $path or warn "Can't open $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $path or warn "Can't open ${path}: $!" 593 0 593
656 593 ▶ while (defined(my $file = readdir DIR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readdir DIR)) 2972 593 3565
657 2972 ▶ next if $file eq '.' or $file eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "." or $file eq ".." 1186 1786 2972
Condition100%3/3
$file eq "." or $file eq ".."
l!l&&r!l&&!r
5935931786
MC/DC100%2/2
$file eq "." or $file eq ".."
$file eq "." $file eq ".."
Truth table100%3/3
A or B
A= $file eq "."B= $file eq ".."
ABresult
1X1
011
000
658 1786 rm_tree("$path/$file");
659 }
660 593 closedir DIR;
661 593 rmdir $path;
662 } elsif (-f $path) {
663 1220 unlink $path;
664 } else {
665 24 ▶ $Test->diag( "Can't find $path to remove" )
Branch: 1 branch
BranchTrueFalseTotal
unless $silent 0 24 24
666 unless $silent;
667 }
668 }
669  
670 sub _cleanup
671 51 ▶{
Subroutine _cleanup: called CC=1 SCAR=0.0
672 51 my $self = shift;
673  
674 51 rm_tree( $self->base_path, $DEBUG, @_ );
675 }
676  
677 27 1;
678  
679 __END__
« lib/HTML/Mason/ApacheHandler.pm ↑ Summary lib/HTML/Mason/Resolver/File.pm »