« 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | pod2usage( | ||||||||||||||||||||||||||||
| 48 | -exitval => 1, | ||||||||||||||||||||||||||||
| 49 | -message => "Missing " | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | . join( ", ", map { "--$_" } @missing ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $opts{"file"} and $opts{"file"} =~ m[(?:_w$|^wap/)]
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/)]
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 140 | : ( | ||||||||||||||||||||||||||||
| 141 | _STRING( $cfg->{script_dirs} ) | ||||||||||||||||||||||||||||
| 142 | ? ( $cfg->{script_dirs} ) | ||||||||||||||||||||||||||||
| 143 | 0 | ▶ | : ( config_dirs("check_web") ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
-f $pattern and -r $pattern
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||