blib/lib/URL/Check.pm

filter: Statementstmt 92.2% 95 / 103 Branchbran 81.5% 31 / 38 Conditioncond 72.7% 8 / 11 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 14 / 14 Podpod 44.4% 4 / 9 totaltotal 85.6% 155 / 181 CC 26 SCAR 17.5
CC
26
Cov
88%
readConfig
10.2
submitReport
8.3
p_runOneUrl
7.0
CRAP 5.8
↑ Summary
1 package URL::Check;
2  
3 5 $URL::Check::VERSION = '0.09';
4 5 $URL::Check::AUTHORITY = 'cpan:ALEXMASS';
5  
6 =head1 NAME
7  
8 URL::Check - Check a list of URL and react (emails etc.) in case of failures.
9  
10 =head1 VERSION
11  
12 Version 0.09
13  
14 =cut
15  
16 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 5 ▶use LWP::Simple qw/get/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Time::HiRes qw /gettimeofday/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 DESCRIPTION
23  
24 This module is not aimed at being used directly but via the script C<url-check>.
25  
26 url-check --config=my-config.txt
27  
28 If no C<--config argument> is set, the value is taken from environment variable C<URL_CHECK_CONFIG>.
29  
30 More examples can be found in the C<t/resources/config> directory but consist
31 in default parameter (mailto etc., then each url to be tested can be followed
32 by dedicated test (time.delay, xpath etc.)
33  
34 More info can be found L<here|http://alexandre-masselot.blogspot.com/2011/10/perl-url-checker.html>
35  
36 =head1 METHODS
37  
38 =head2 readConfig([configFile])
39  
40 Read the config file. Default file name is taken from C<$URL_CHECK_FILE>
41  
42 =cut
43  
44 5 our %config;
45 5 our @report;
46  
47 sub readConfig {
48 my $configFile = shift || $ENV{URL_CHECK_CONFIG}
49 6 ▶ || die "ERROR: No config file is passed or env URL_CHECK_CONFIG is set.\n";
Subroutine readConfig: called CC=10 SCAR=23.2
Pod documented
Condition80%4/5
shift() || $ENV{'URL_CHECK_CONFIG'} || die("ERROR: No config file is passed or env URL_CHECK_CONFIG is set.\n")
l!l
51
shift() or $ENV{"URL_CHECK_CONFIG"}
l!l&&r!l&&!r
501
MC/DC50%1/2
shift() || $ENV{'URL_CHECK_CONFIG'} || die("ERROR: No config file is passed or env URL_CHECK_CONFIG is set.\n")
shift() $ENV{"URL_CHECK_CONFIG"}
Truth table66%2/3
A or B
A= shift()B= $ENV{"URL_CHECK_CONFIG"}
ABresult
1X1
011
000
50  
51 5 my $FD;
52 5 ▶ open ($FD, "<$configFile")
Branch: 1 branch
BranchTrueFalseTotal
open $FD, "<$configFile" or die "ERROR: Cannot open config file [$configFile]: $!\n" 1 4 5
53 || die "ERROR: Cannot open config file [$configFile]: $!\n";
54  
55 4 p_clearConfig();
56  
57 # $currenturl will contain at first the default map for handling error,
58 # then, when a url is encountered, it will contain the url's error handlers
59 4 my $currentUrl;
60 4 ▶ while (my $line = <$FD>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $FD)) 39 4 43
61 39 $line=~s/^#.*//; # remove comments
62 39 $line=~s/\s*$//; # end of line spaces
63 39 ▶ next unless $line =~/\S/; # skip emplty lines
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 16 23 39
64  
65 23 ▶ if ($line=~/^onerror/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^onerror/i) 4 19 23
66 4 ▶ p_addOnErrorLine($line, $currentUrl || $config{default});
Condition33%1/3
$currentUrl || $URL::Check::config{'default'}
l!l&&r!l&&!r
040
MC/DC0%0/2
$currentUrl || $URL::Check::config{'default'}
$currentUrl $URL::Check::config{'default'}
Truth table33%1/3
A || B
A= $currentUrlB= $URL::Check::config{'default'}
ABresult
1X1
011
000
67 4 next;
68 }
69  
70 19 ▶ if ($line=~/^check/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^check/i) 5 14 19
71 5 p_addCheckLine($line, $currentUrl);
72 5 next;
73 }
74  
75 14 ▶ if ($line=~/^(ftp|http|file):\/\//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^(ftp|http|file)://]i) 14 0 14
76 14 $currentUrl = p_addUrl($line);
77 14 next;
78 }
79  
80 0 die "ERROR: Cannot parse line: $line\n";
81 }
82  
83 4 close($FD);
84 }
85  
86 =head2 run()
87  
88 Run the configured tests, and store the result into the local @report
89  
90 =cut
91  
92 sub run {
93  
94 3 ▶ undef @report;
Subroutine run: called CC=2 SCAR=6.9
Pod documented
95 3 foreach my $urlConfig (@{$config{urls}}) {
96 10 push @report, p_runOneUrl($urlConfig);
97 }
98 }
99  
100 =head2 submitReport(%report)
101  
102 Print on console or send by mail the error output
103  
104 =cut
105  
106 sub submitReport {
107 2 ▶ my %report = @_;
Subroutine submitReport: called CC=4 SCAR=21.2
Pod documented
108  
109 2 ▶ if ($config{default}{onError}{console}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($URL::Check::config{"default"}{"onError"}{"console"}) 2 0 2
110 2 print "ERROR REPORT: $report{subject}\n$report{contents}\n";
111 }
112  
113 2 ▶ if ($config{default}{onError}{mailto}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($URL::Check::config{"default"}{"onError"}{"mailto"}) 0 2 2
114 0 require Mail::Sendmail;
115 0 my $hostname = `hostname`;
116 0 chomp $hostname;
117 my %mail = (
118 0 To => join(',', @{$config{default}{onError}{mailto}}),
119 Subject => "[url-check] $report{subject}",
120 From => "url.check\@$hostname",
121 Message => "$report{contents}\n",
122 );
123  
124 0 warn "sending error report by mail: $report{subject}\n";
125 0 ▶ Mail::Sendmail::sendmail(%mail) or die $Mail::Sendmail::error
Branch: 1 branch
BranchTrueFalseTotal
Mail::Sendmail::sendmail(%mail) or die $Mail::Sendmail::error 0 0 0
126 }
127 }
128  
129 =head2 errorReport()
130  
131 Build a map (subject => ..., content=> ...) with the errors after the run
132 return () if no error were detected
133  
134 =cut
135  
136 sub errorReport {
137 3 ▶ my @errors = grep {! $_->{success}} @report;
Subroutine errorReport: called CC=2 SCAR=6.9
Pod documented
138  
139 3 ▶ unless (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@errors) 1 2 3
140 1 return ();
141 }
142  
143 (
144 subject => ''.scalar(@errors).' errors reported',
145 2 contents => join("\n", (map {$_->{url}." : ".$_->{message}} @errors))
146 );
147 }
148  
149 #
150 #
151 # PRIVATE METHODS
152  
153 sub p_addOnErrorLine {
154 11 ▶ my ($line, $conf) = @_;
Subroutine p_addOnErrorLine: called CC=4 SCAR=13.9
Pod undocumented
155  
156 11 ▶ die "ERROR: Cannot parse error line: $line"
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^onerror\.(.+?)=(.+)/i 0 11 11
157 unless $line=~/^onerror\.(.+?)=(.+)/i;
158  
159 11 my ($errorCat, $params) = ($1, $2);
160  
161 11 ▶ if ($errorCat eq 'mailto') {
Branch: 1 branch
BranchTrueFalseTotal
if ($errorCat eq "mailto") 2 9 11
162 2 my @tmp = split(/,/, $params);
163 2 $conf->{onError}{mailto}=\@tmp;
164 2 return;
165 }
166  
167 9 ▶ if ($errorCat eq 'console') {
Branch: 1 branch
BranchTrueFalseTotal
if ($errorCat eq "console") 8 1 9
168 8 $conf->{onError}{console}= $params =~ /\s*(y(es)?|t(rue)?|1)\s*$/i;
169 8 return;
170 }
171  
172 1 die "ERROR: Unknown onerror type [$errorCat]\n";
173 }
174  
175 sub p_addCheckLine {
176 5 ▶ my ($line, $conf) = @_;
Subroutine p_addCheckLine: called CC=3 SCAR=11.0
Pod undocumented
177  
178 5 ▶ die "ERROR: Cannot parse error line: $line\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^check\.(.+?)=(.+)/i 0 5 5
179 unless $line=~/^check\.(.+?)=(.+)/i;
180  
181 5 my ($cat, $params) = ($1, $2);
182  
183 5 ▶ if ($cat eq 'contains') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cat eq "contains") 3 2 5
184 3 push @{ $conf->{check}{contains}}, $params ;
185 3 return;
186 }
187  
188 2 $conf->{check}{$cat} = $params
189 }
190  
191 sub p_addUrl {
192 14 ▶ my ($line) = @_;
Subroutine p_addUrl: called CC=1 SCAR=0.0
Pod undocumented
193  
194 14 my $h = {
195 url => $line,
196 check => {}
197 };
198  
199 14 push @{$config{urls}}, $h;
200 14 return $h;
201 }
202  
203 sub p_clearConfig {
204  
205 4 ▶ %config = (
Subroutine p_clearConfig: called CC=1 SCAR=0.0
Pod undocumented
206 default => { onError=>{} },
207 urls => [],
208 );
209 }
210  
211 sub p_runOneUrl {
212 10 ▶ my %urlConfig = %{$_[0]};
Subroutine p_runOneUrl: called CC=7 SCAR=19.5
Pod undocumented
213  
214 10 my $url = $urlConfig{url};
215 10 my ($sec0, $micros0) = gettimeofday;
216 10 my $content = get($url);
217  
218 10 ▶ unless ($content) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content) 2 8 10
219 return {
220 2 url => $url,
221 success => 0,
222 message => 'cannot load content'
223 }
224 }
225  
226 8 my ($sec1, $micros1) = gettimeofday;
227 8 my $dtime = int(($sec1-$sec0)*1000 + ($micros1-$micros0)/1000);
228  
229 8 ▶ if ((exists $urlConfig{check}{overtime}) && ($urlConfig{check}{overtime} < $dtime)) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $urlConfig{"check"}{"overtime"} and $urlConfig{"check"}{"overtime"} < $dtime) 1 7 8
Condition100%3/3
exists $urlConfig{"check"}{"overtime"} and $urlConfig{"check"}{"overtime"} < $dtime
!ll&&!rl&&r
611
MC/DC100%2/2
exists $urlConfig{"check"}{"overtime"} and $urlConfig{"check"}{"overtime"} < $dtime
exists $urlConfig{"check"}{"overtime"} $urlConfig{"check"}{"overtime"} < $dtime
Truth table100%3/3
A and B
A= exists $urlConfig{"check"}{"overtime"}B= $urlConfig{"check"}{"overtime"} < $dtime
ABresult
0X0
100
111
230 return {
231 1 url => $url,
232 success => 0,
233 message => "overtime > $urlConfig{check}{overtime} (${dtime}ms)",
234 }
235 }
236  
237 7 ▶ if (exists $urlConfig{check}{contains}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $urlConfig{"check"}{"contains"}) 2 5 7
238 2 my @searchFor = @{$urlConfig{check}{contains}};
239 2 foreach (@searchFor) {
240 2 ▶ if (index($content, $_) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($content, $_) == -1) 1 1 2
241 return {
242 1 url => $url,
243 success => 0,
244 message => "does not contains \"$_\"",
245 }
246 }
247 }
248 }
249  
250 return {
251 6 url => $url,
252 success => 1
253 };
254 }
255  
256 =head1 AUTHOR
257  
258 Alexandre Masselot, C<< <alexandre.masselot at gmail.com> >>
259  
260 Currently maintained by Mohammad S Anwar (MANWAR) C<< <mohammad.anwar @ yahoo.com> >>
261  
262 =head1 REPOSITORY
263  
264 L<https://github.com/manwar/URL-Check>
265  
266 =head1 BUGS
267  
268 Please report any bugs or feature requests to C<bug-url-check at rt.cpan.org>,
269 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=URL-Check>.
270 I will be notified, and then you'll automatically be notified of progress on
271 your bug as I make changes.
272  
273 =head1 SUPPORT
274  
275 You can find documentation for this module with the perldoc command.
276  
277 perldoc URL::Check
278  
279 You can also look for information at:
280  
281 =over 4
282  
283 =item * RT: CPAN's request tracker (report bugs here)
284  
285 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=URL-Check>
286  
287 =item * AnnoCPAN: Annotated CPAN documentation
288  
289 L<http://annocpan.org/dist/URL-Check>
290  
291 =item * CPAN Ratings
292  
293 L<http://cpanratings.perl.org/d/URL-Check>
294  
295 =item * Search CPAN
296  
297 L<http://search.cpan.org/dist/URL-Check/>
298  
299 =back
300  
301 =head1 LICENSE AND COPYRIGHT
302  
303 Copyright 2011 Alexandre Masselot.
304  
305 This program is free software; you can redistribute it and/or modify it under the
306 terms of either: the GNU General Public License as published by the Free Software
307 Foundation; or the Artistic License.
308  
309 See http://dev.perl.org/licenses/ for more information.
310  
311 =cut
312  
313 5 1; # End of URL::Check
↑ Summary