lib/WWW/Mechanize/Script/Util.pm

filter: Statementstmt 44.0% 44 / 100 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 72.2% 13 / 18 Podpod 100% 5 / 5 totaltotal 38.5% 62 / 161 CC 19 SCAR 35.7
CC
19
Cov
33%
find_scripts
90.0
load_config
56.0
opt_required_all
12.0
CRAP 35.4
« lib/WWW/Mechanize/Script/Plugin/RegexMatchTest.pm ↑ Summary lib/WWW/Mechanize/Script/Plugin/TextMatchTest.pm »
1 package WWW::Mechanize::Script::Util;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw/$VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: some basic utility functions for scripts
10  
11 1 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Basename qw(fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::ConfigDir qw(config_dirs);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Hash::Merge ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Params::Util qw(_HASH _ARRAY _STRING);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 $VERSION = '0.100';
22  
23 1 @EXPORT = ();
24 1 @EXPORT_OK = qw(opt_required_all opt_required_one opt_exclusive load_config find_scripts);
25 1 %EXPORT_TAGS = ( ALL => \@EXPORT_OK );
26  
27  
28 sub opt_required_one
29 0 ▶{
Subroutine opt_required_one: not called CC=2 SCAR=17.9
Pod documented
30 0 my ( $opt_hash, @opt_names ) = @_;
31  
32 0 my @have = grep { defined( $opt_hash->{$_} ) } @opt_names;
33 0 ▶ @have and return;
Branch: 1 branch
BranchTrueFalseTotal
@have and return 0 0 0
34  
35 pod2usage( -exitval => 1,
36 0 -message => "Missing at least one of " . join( ", ", map { "--$_" } @opt_names ) );
37 }
38  
39  
40 sub opt_required_all
41 0 ▶{
Subroutine opt_required_all: not called CC=3 SCAR=24.8
Pod documented
42 0 my ( $opt_hash, @opt_names ) = @_;
43  
44 0 my @missing = grep { !defined( $opt_hash->{$_} ) } @opt_names;
45 0 ▶ @missing or return;
Branch: 1 branch
BranchTrueFalseTotal
@missing or return 0 0 0
46  
47 pod2usage(
48 -exitval => 1,
49 -message => "Missing "
50 0 ▶ . join( ", ", map { "--$_" } @missing )
Branch: 1 branch
BranchTrueFalseTotal
@missing > 1 ? : 0 0 0
51 . " argument"
52 . ( @missing > 1 ? "s" : "" )
53 );
54 }
55  
56  
57 sub opt_exclusive
58 0 ▶{
Subroutine opt_exclusive: not called CC=2 SCAR=17.9
Pod documented
59 0 my ( $opt_hash, @opt_names ) = @_;
60  
61 0 my @missing = grep { defined( $opt_hash->{$_} ) } @opt_names;
62 0 ▶ @missing < 2 and return;
Branch: 1 branch
BranchTrueFalseTotal
@missing < 2 and return 0 0 0
63  
64 0 @missing = map { "--" . $_ } @missing;
65 0 my $final_m = pop @missing;
66 0 pod2usage(
67 -exitval => 1,
68 -message => "Options "
69 . join( " and ", join( ", ", @missing ), $final_m )
70 . " are mutual exclusive"
71 );
72 }
73  
74  
75 sub load_config
76 0 ▶{
Subroutine load_config: not called CC=7 SCAR=40.3
Pod documented
77 0 my %opts;
78 0 ▶ _HASH( $_[0] ) and %opts = %{ $_[0] };
Branch: 1 branch
BranchTrueFalseTotal
&_HASH($_[0]) and %opts = %{$_[0];} 0 0 0
79 my %cfg = (
80 defaults => {
81 terse => 'failed_only',
82 save_output => 'yes', # report ...
83 show_html => 'yes', # report ...
84 },
85 request => {
86 agent => {
87 agent => (
88 0 ▶ ( defined( $opts{file} ) and $opts{file} =~ m/(?:_w$|^wap\/)/ )
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'file'} && $opts{'file'} =~ m[(?:_w$|^wap/)] ? : 0 0 0
Condition0%0/3
defined $opts{"file"} and $opts{"file"} =~ m[(?:_w$|^wap/)]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"file"} and $opts{"file"} =~ m[(?:_w$|^wap/)]
defined $opts{"file"} $opts{"file"} =~ m[(?:_w$|^wap/)]
Truth table0%0/3
A and B
A= defined $opts{"file"}B= $opts{"file"} =~ m[(?:_w$|^wap/)]
ABresult
0X0
100
111
89 ? "Nokia6210/1.0 (03.01) UP.Link/5.0.0.4 VZDE-check_wap $VERSION"
90 : "Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv: VZDE-check_web $VERSION)"
91 ),
92 accept_cookies => 'yes', # check LWP::UA param
93 show_cookie => 'yes', # check LWP::UA param
94 show_headers => 'yes', # check LWP::UA param
95 send_cookie => 'yes', # check LWP::UA param
96 },
97 },
98 );
99  
100 # find config file
101 0 my @cfg_dirs = uniq map { realpath($_) } config_dirs();
102 0 my $progname = fileparse( $0, qr/\.[^.]*$/ );
103 0 my @cfg_pattern = map { ( "check_web." . $_, $progname . "." . $_ ) } Config::Any->extensions();
104 0 my @cfg_files = File::Find::Rule->file()->name(@cfg_pattern)->maxdepth(1)->in(@cfg_dirs);
105 0 ▶ if (@cfg_files)
Branch: 1 branch
BranchTrueFalseTotal
if (@cfg_files) 0 0 0
106 {
107 0 my $merger = Hash::Merge->new('LEFT_PRECEDENT');
108 # read config file(s)
109 0 my $all_cfg = Config::Any->load_files(
110 {
111 files => [@cfg_files],
112 use_ext => 1,
113 flatten_to_hash => 1,
114 }
115 );
116  
117 0 foreach my $filename (@cfg_files)
118 {
119 0 ▶ defined( $all_cfg->{$filename} )
Branch: 1 branch
BranchTrueFalseTotal
defined $all_cfg->{$filename} or next 0 0 0
120 or next; # file not found or not parsable ...
121 # merge into default and previous loaded config ...
122 0 %cfg = %{ $merger->merge( \%cfg, $all_cfg->{$filename} ) };
123 }
124 }
125  
126 0 return %cfg;
127 }
128  
129  
130 sub find_scripts
131 0 ▶{
Subroutine find_scripts: not called CC=9 SCAR=45.0
Pod documented
132 0 my ( $cfg, @patterns ) = @_;
133 0 my @script_filenames;
134  
135 my @cfg_dirs =
136 defined( $cfg->{script_dirs} )
137 ? (
138 _ARRAY( $cfg->{script_dirs} )
139 0 ▶ ? @{ $cfg->{script_dirs} }
Branch: 1 branch
BranchTrueFalseTotal
&_STRING($cfg->{'script_dirs'}) ? : 0 0 0
140 : (
141 _STRING( $cfg->{script_dirs} )
142 ? ( $cfg->{script_dirs} )
143 0 ▶ : ( config_dirs("check_web") )
Branch: 2 branches
BranchTrueFalseTotal
defined $cfg->{'script_dirs'} ? : 0 0 0
&_ARRAY($cfg->{'script_dirs'}) ? : 0 0 0
144 )
145 )
146 : ( config_dirs("check_web") );
147 @cfg_dirs =
148 0 ▶ grep { -d $_ } map { File::Spec->file_name_is_absolute($_) ? $_ : config_dirs($_) } @cfg_dirs;
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($_) ? : 0 0 0
149 # grep { -d $_ }
150 # map { File::Spec->catdir( $_, $directories ) }
151 # config_dirs( $cfg{script_dir} // "check_web" ); # XXX basename $0
152 0 foreach my $pattern (@patterns)
153 {
154 0 ▶ if ( -f $pattern and -r $pattern )
Branch: 1 branch
BranchTrueFalseTotal
if (-f $pattern and -r $pattern) { } 0 0 0
Condition0%0/3
-f $pattern and -r $pattern
!ll&&!rl&&r
000
MC/DC0%0/2
-f $pattern and -r $pattern
-f $pattern -r $pattern
Truth table0%0/3
A and B
A= -f $patternB= -r $pattern
ABresult
0X0
100
111
155 {
156 0 push( @script_filenames, $pattern );
157 }
158 else
159 {
160 0 my ( $volume, $directories, $fn ) = File::Spec->splitpath($pattern);
161 my @script_pattern =
162 0 ▶ $fn =~ m/\.[^.]*$/ ? ($fn) : map { $fn . "." . $_ } Config::Any->extensions();
Branch: 1 branch
BranchTrueFalseTotal
$fn =~ /\.[^.]*$/ ? : 0 0 0
163 0 my @script_dirs = grep { -d $_ }
164 0 map { File::Spec->catdir( $_, $directories ) } @cfg_dirs;
165 0 push( @script_filenames,
166 File::Find::Rule->file()->name(@script_pattern)->maxdepth(1)->in(@script_dirs) );
167 }
168 }
169 0 return @script_filenames;
170 }
171  
172 1 1;
173  
174 __END__
« lib/WWW/Mechanize/Script/Plugin/RegexMatchTest.pm ↑ Summary lib/WWW/Mechanize/Script/Plugin/TextMatchTest.pm »