lib/WWW/Mechanize/Script.pm

filter: Statementstmt 87.8% 116 / 132 Branchbran 40.0% 12 / 30 Conditioncond 39.1% 9 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 95.0% 19 / 20 Podpod 100% 7 / 7 totaltotal 72.4% 163 / 225 CC 29 SCAR 19.3
CC
29
Cov
74%
_gen_code_compute
17.7
get_request_value
12.0
_get_target
9.9
CRAP 6.9
« lib/WWW/Mechanize/Script/Plugin.pm ↑ Summary lib/WWW/Mechanize/Script/Plugin/StatusTest.pm »
1 package WWW::Mechanize::Script;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use File::Basename qw(fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Hash::Merge ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Module::Pluggable::Object ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Params::Util qw(_HASH);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Template ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use WWW::Mechanize ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use WWW::Mechanize::Timed ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # ABSTRACT: fetch websites and executes tests on the results
17  
18  
19 3 our $VERSION = '0.100';
20  
21  
22 sub new
23 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
24 1 my ( $class, $cfg ) = @_;
25  
26 1 my $self = bless( { cfg => { %{$cfg} } }, $class );
27  
28 1 return $self;
29 }
30  
31  
32 sub _gen_code_compute
33 6 ▶{
Subroutine _gen_code_compute: called CC=8 SCAR=28.7
34 6 my $check_cfg = $_[0];
35 6 my $compute_code;
36  
37 6 ▶ if ( defined( $check_cfg->{code_func} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $check_cfg->{"code_func"}) 0 6 6
38 {
39 0 my $compute_str = "sub { " . $check_cfg->{code_func} . " };";
40 0 $compute_code = eval $compute_str;
41 0 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 0 0
42 }
43  
44 6 ▶ if ( !defined($compute_code) and defined( $check_cfg->{code_cmp} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $compute_code and defined $check_cfg->{"code_cmp"}) 6 0 6
Condition33%1/3
not defined $compute_code and defined $check_cfg->{"code_cmp"}
!ll&&!rl&&r
006
MC/DC0%0/2
not defined $compute_code and defined $check_cfg->{"code_cmp"}
not defined $compute_code defined $check_cfg->{"code_cmp"}
Truth table33%1/3
not(A) and B
A= not defined $compute_codeB= defined $check_cfg->{"code_cmp"}
ABresult
0X0
100
111
45 {
46 my $compute_str =
47 "sub { my (\$cur,\$new) = \@_; \$cur "
48 . $check_cfg->{code_cmp}
49 6 . " \$new ? \$cur : \$new; };";
50 6 $compute_code = eval $compute_str;
51 6 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 6 6
52 }
53  
54 6 ▶ if ( !defined($compute_code) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $compute_code) 0 6 6
55 {
56 0 my $compute_str = "sub { my (\$cur,\$new) = \@_; \$cur > \$new ? \$cur : \$new; };";
57 0 $compute_code = eval $compute_str;
58 0 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 0 0
59 }
60  
61 6 return $compute_code;
62 }
63  
64  
65 sub test_plugins
66 5 ▶{
Subroutine test_plugins: called CC=2 SCAR=6.9
Pod documented
67 5 my ( $self, $test ) = @_;
68  
69 5 ▶ unless ( defined( $self->{all_plugins} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"all_plugins"}) 1 4 5
70 {
71 1 my $plugin_base = join( "::", __PACKAGE__, "Plugin" );
72 1 my $finder =
73 Module::Pluggable::Object->new(
74 require => 1,
75 search_path => [$plugin_base],
76 except => [$plugin_base],
77 inner => 0,
78 only => qr/^${plugin_base}::\p{Word}+$/,
79 );
80  
81 # filter out things that don't look like our plugins
82 my @ap =
83 5 map { $_->new( $self->{cfg}->{defaults} ) }
84 1 grep { $_->isa($plugin_base) } $finder->plugins();
85 1 $self->{all_plugins} = \@ap;
86 }
87  
88 5 my @tp = grep { $_->can_check($test) } @{ $self->{all_plugins} };
89 5 return @tp;
90 }
91  
92  
93 sub get_request_value
94 0 ▶{
Subroutine get_request_value: not called CC=3 SCAR=24.8
Pod documented
95 0 my ( $self, $request, $value_name ) = @_;
96  
97 0 ▶ $value_name or return;
Branch: 1 branch
BranchTrueFalseTotal
$value_name or return 0 0 0
98  
99 0 ▶ return $request->{$value_name} // $self->{cfg}->{default}->{request}->{$value_name};
Condition0%0/3
$request->{$value_name} // $self->{"cfg"}{"default"}{"request"}{$value_name}
l!l&&r!l&&!r
000
MC/DC0%0/2
$request->{$value_name} // $self->{"cfg"}{"default"}{"request"}{$value_name}
$request->{$value_name} $self->{"cfg"}{"default"}{"request"}{$value_name}
Truth table0%0/3
A // B
A= $request->{$value_name}B= $self->{"cfg"}{"default"}{"request"}{$value_name}
ABresult
1X1
011
000
100 }
101  
102 sub _get_target
103 6 ▶{
Subroutine _get_target: called CC=5 SCAR=22.9
104 6 my $def = shift;
105  
106 6 my $target = $def->{target};
107 6 ▶ $target //= "-";
Condition50%1/2
$target //= "-"
l!l
60
MC/DC0%0/1
$target //= "-"
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
108  
109 6 ▶ if ( $target ne "-" and $def->{append} )
Branch: 1 branch
BranchTrueFalseTotal
if ($target ne "-" and $def->{"append"}) 0 6 6
Condition33%1/3
$target ne "-" and $def->{"append"}
!ll&&!rl&&r
600
MC/DC0%0/2
$target ne "-" and $def->{"append"}
$target ne "-" $def->{"append"}
Truth table33%1/3
A and B
A= $target ne "-"B= $def->{"append"}
ABresult
0X0
100
111
110 {
111 0 my ( $name, $path, $suffix ) = fileparse($target);
112 0 ▶ -d $path or make_path($path);
Branch: 1 branch
BranchTrueFalseTotal
-d $path or make_path($path) 0 0 0
113 0 my $fh = IO::File->new( $target, ">>" );
114 0 $fh->seek( 0, SEEK_END );
115 0 $target = $fh;
116 }
117  
118 6 return $target;
119 }
120  
121  
122 sub summarize
123 1 ▶{
Subroutine summarize: called CC=5 SCAR=16.7
Pod documented
124 1 my ( $self, $code, @msgs ) = @_;
125  
126 my %vars = (
127 1 ▶ %{ _HASH( $self->{cfg}->{templating}->{vars} ) // {} },
Condition50%1/2
&_HASH($self->{"cfg"}{"templating"}{"vars"}) // {}
l!l
10
MC/DC0%0/1
&_HASH($self->{"cfg"}{"templating"}{"vars"}) // {}
&_HASH($self->{"cfg"}{"templating"}{"vars"})
Truth table50%1/2
A
A= &_HASH($self->{"cfg"}{"templating"}{"vars"})
Aresult
11
00
128 1 ▶ %{ _HASH( $self->{cfg}->{report}->{vars} ) // {} },
Condition50%1/2
&_HASH($self->{"cfg"}{"report"}{"vars"}) // {}
l!l
01
MC/DC0%0/1
&_HASH($self->{"cfg"}{"report"}{"vars"}) // {}
&_HASH($self->{"cfg"}{"report"}{"vars"})
Truth table50%1/2
A
A= &_HASH($self->{"cfg"}{"report"}{"vars"})
Aresult
11
00
129 CODE => $code,
130 MESSAGES => [@msgs]
131 );
132  
133 1 ▶ my $input = $self->{cfg}->{summary}->{source} // \$self->{cfg}->{summary}->{template};
Condition50%1/2
$self->{"cfg"}{"summary"}{"source"} // \$self->{"cfg"}{"summary"}{"template"}
l!l
01
MC/DC0%0/1
$self->{"cfg"}{"summary"}{"source"} // \$self->{"cfg"}{"summary"}{"template"}
$self->{"cfg"}{"summary"}{"source"}
Truth table50%1/2
A
A= $self->{"cfg"}{"summary"}{"source"}
Aresult
11
00
134 1 my $output = _get_target( $self->{cfg}->{summary} );
135 1 my $template = Template->new();
136 1 ▶ $template->process( $input, \%vars, $output )
Branch: 1 branch
BranchTrueFalseTotal
$template->process($input, \%vars, $output) or die $template->error 1 0 1
137 or die $template->error();
138  
139 1 return;
140 }
141  
142  
143 sub gen_report
144 5 ▶{
Subroutine gen_report: called CC=5 SCAR=16.6
Pod documented
145 5 my ( $self, $full_test, $mech, $code, @msgs ) = @_;
146 5 my $response = $mech->response();
147 my %vars = (
148 5 ▶ %{ _HASH( $self->{cfg}->{templating}->{vars} ) // {} },
Condition50%1/2
&_HASH($self->{"cfg"}{"templating"}{"vars"}) // {}
l!l
50
MC/DC0%0/1
&_HASH($self->{"cfg"}{"templating"}{"vars"}) // {}
&_HASH($self->{"cfg"}{"templating"}{"vars"})
Truth table50%1/2
A
A= &_HASH($self->{"cfg"}{"templating"}{"vars"})
Aresult
11
00
149 5 ▶ %{ _HASH( $self->{cfg}->{report}->{vars} ) // {} },
Condition50%1/2
&_HASH($self->{"cfg"}{"report"}{"vars"}) // {}
l!l
05
MC/DC0%0/1
&_HASH($self->{"cfg"}{"report"}{"vars"}) // {}
&_HASH($self->{"cfg"}{"report"}{"vars"})
Truth table50%1/2
A
A= &_HASH($self->{"cfg"}{"report"}{"vars"})
Aresult
11
00
150 CODE => $code,
151 MESSAGES => [@msgs],
152 RESPONSE => {
153 CODE => $response->code(),
154 CONTENT => $response->content(),
155 BASE => $response->base(),
156 HEADER => {
157 5 map { $_ => $response->headers()->header($_) }
158 $response->headers()->header_field_names()
159 },
160 }
161 );
162  
163 5 ▶ my $input = $self->{cfg}->{report}->{source} // \$self->{cfg}->{report}->{template};
Condition50%1/2
$self->{"cfg"}{"report"}{"source"} // \$self->{"cfg"}{"report"}{"template"}
l!l
05
MC/DC0%0/1
$self->{"cfg"}{"report"}{"source"} // \$self->{"cfg"}{"report"}{"template"}
$self->{"cfg"}{"report"}{"source"}
Truth table50%1/2
A
A= $self->{"cfg"}{"report"}{"source"}
Aresult
11
00
164 5 my $output = _get_target( $self->{cfg}->{report} );
165 5 my $template = Template->new();
166 5 ▶ $template->process( $input, \%vars, $output )
Branch: 1 branch
BranchTrueFalseTotal
$template->process($input, \%vars, $output) or die $template->error 5 0 5
167 or die $template->error();
168  
169 5 return;
170 }
171  
172  
173 sub run_script
174 1 ▶{
Subroutine run_script: called CC=3 SCAR=11.0
Pod documented
175 1 my ( $self, @script ) = @_;
176 1 my $code = 0; # XXX
177 1 my @msgs;
178 1 my $compute_code = _gen_code_compute( $self->{cfg}->{defaults}->{check} );
179  
180 1 foreach my $test (@script)
181 {
182 5 my ( $test_code, @test_msgs ) = $self->run_test($test);
183 5 $code = &{$compute_code}( $code, $test_code );
184 5 push( @msgs, @test_msgs );
185 }
186  
187 1 ▶ if ( $self->{cfg}->{summary} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cfg"}{"summary"}) 1 0 1
188 {
189 1 my $summary = $self->summarize( $code, @msgs );
190 }
191  
192 1 return ( $code, @msgs );
193 }
194  
195  
196 sub run_test
197 5 ▶{
Subroutine run_test: called CC=5 SCAR=16.3
Pod documented
198 5 my ( $self, $test ) = @_;
199  
200 5 my $merger = Hash::Merge->new('LEFT_PRECEDENT');
201 5 my $full_test = $merger->merge( $test, $self->{cfg}->{defaults} );
202  
203 5 my $mech = WWW::Mechanize::Timed->new();
204 5 foreach my $akey ( keys %{ $full_test->{request}->{agent} } )
205 {
206 # XXX clone and delete array args before
207 0 $mech->$akey( $full_test->{request}->{agent}->{$akey} );
208 }
209  
210 5 my $method = $full_test->{request}->{method};
211 defined( $test->{request}->{http_headers} )
212 0 ? $mech->$method( $full_test->{request}->{uri}, %{ $full_test->{request}->{http_headers} } )
213 5 ▶ : $mech->$method( $full_test->{request}->{uri} );
Branch: 1 branch
BranchTrueFalseTotal
defined $test->{'request'}{'http_headers'} ? : 0 5 5
214  
215 5 $full_test->{compute_code} = _gen_code_compute( $full_test->{check} );
216  
217 5 my $code = 0;
218 5 my @msgs;
219 5 foreach my $tp ( $self->test_plugins($full_test) )
220 {
221 4 my ( $plug_code, @plug_msgs ) = $tp->check_response( $full_test, $mech );
222 4 $code = &{ $full_test->{compute_code} }( $code, $plug_code );
223 4 push( @msgs, @plug_msgs );
224 }
225  
226 5 ▶ if ( $self->{cfg}->{report} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cfg"}{"report"}) 5 0 5
227 {
228 5 $self->gen_report( $full_test, $mech, $code, @msgs );
229 }
230  
231 5 return ( $code, @msgs );
232 }
233  
234 3 1;
235  
236 __END__
« lib/WWW/Mechanize/Script/Plugin.pm ↑ Summary lib/WWW/Mechanize/Script/Plugin/StatusTest.pm »