File Coverage

blib/lib/Test/HTML/Tidy/Recursive.pm
Criterion Covered Total %
statement 35 65 53.8
branch 3 6 50.0
condition 2 3 66.6
subroutine 9 15 60.0
pod 6 6 100.0
total 55 95 57.8


line stmt bran cond sub pod time code
1             package Test::HTML::Tidy::Recursive;
2             $Test::HTML::Tidy::Recursive::VERSION = '0.6.2';
3 1     1   712 use strict;
  1         3  
  1         30  
4 1     1   5 use warnings;
  1         2  
  1         23  
5 1     1   18 use 5.008;
  1         4  
6              
7 1     1   6 use Test::More;
  1         2  
  1         11  
8              
9 1     1   767 use HTML::T5 qw/ TIDY_INFO TIDY_WARNING /;
  1         3373  
  1         57  
10 1     1   521 use File::Find::Object ();
  1         9706  
  1         29  
11 1     1   863 use Path::Tiny qw/ path /;
  1         11484  
  1         63  
12              
13 1     1   8 use MooX qw/ late /;
  1         3  
  1         9  
14              
15             has filename_re => (
16             is => 'ro',
17             default => sub {
18             return qr/\.x?html\z/;
19             }
20             );
21              
22             has targets => ( is => 'ro', isa => 'ArrayRef', required => 1 );
23              
24             has filename_filter => (
25             is => 'ro',
26             default => sub {
27             return sub { return 1; }
28             }
29             );
30              
31             has _tidy => ( is => 'rw' );
32             has _error_count => ( is => 'rw', isa => 'Int', default => 0 );
33              
34             sub report_error
35             {
36 0     0 1 0 my ( $self, $args ) = @_;
37              
38 0         0 $self->_error_count( 1 + $self->_error_count );
39 0         0 diag( $args->{message} );
40              
41 0         0 return;
42             }
43              
44             sub calc_tidy
45             {
46 0     0 1 0 my $self = shift;
47              
48 0         0 my $tidy = HTML::T5->new( { output_xhtml => 1, } );
49 0         0 $tidy->ignore( type => TIDY_WARNING, type => TIDY_INFO );
50              
51 0         0 return $tidy;
52             }
53              
54             sub run
55             {
56 0     0 1 0 my $self = shift;
57 0         0 plan tests => 1;
58             local $SIG{__WARN__} = sub {
59 0     0   0 my $w = shift;
60 0 0       0 if ( $w !~ /\AUse of uninitialized/ )
61             {
62 0         0 die $w;
63             }
64 0         0 return;
65 0         0 };
66              
67 0         0 $self->_tidy( $self->calc_tidy );
68 0         0 $self->traverse;
69 0         0 $self->_tidy('NULL');
70              
71             # TEST
72 0         0 return is( $self->_error_count, 0, "No errors" );
73             }
74              
75             sub check_using_tidy
76             {
77 0     0 1 0 my ( $self, $args ) = @_;
78              
79 0         0 my $fn = $args->{filename};
80              
81 0         0 $self->_tidy->parse( $fn, ( path($fn)->slurp_raw() ) );
82              
83 0         0 for my $message ( $self->_tidy->messages )
84             {
85 0         0 $self->report_error(
86             {
87             message => scalar $message->as_string
88             }
89             );
90             }
91              
92 0         0 $self->_tidy->clear_messages();
93              
94 0         0 return;
95             }
96              
97             sub check_file
98             {
99 0     0 1 0 my ( $self, $args ) = @_;
100              
101 0         0 $self->check_using_tidy($args);
102              
103 0         0 return;
104             }
105              
106             sub traverse
107             {
108 1     1 1 47 my ($self) = @_;
109 1         24 $self->_error_count(0);
110 1         34 my $filename_re = $self->filename_re;
111 1         6 my $filter = $self->filename_filter;
112              
113 1         9 foreach my $target ( @{ $self->targets } )
  1         7  
114             {
115 1         13 my $ffo = File::Find::Object->new( {}, $target );
116 1         267 while ( my $item = $ffo->next_obj() )
117             {
118 3         33207 my $fn = $item->path();
119 3 50 66     10 if ( not $filter->($fn) )
    100          
120             {
121 0         0 $ffo->prune;
122             }
123             elsif ( $item->is_file() and $fn =~ $filename_re )
124             {
125 2         14 $self->check_file( { filename => $fn } );
126             }
127             }
128             }
129              
130 1         1458 return;
131             }
132              
133             1;
134              
135             __END__
136              
137             =pod
138              
139             =encoding UTF-8
140              
141             =head1 NAME
142              
143             Test::HTML::Tidy::Recursive - recursively check files in a directory using
144             HTML::T5 .
145              
146             =head1 VERSION
147              
148             version 0.6.2
149              
150             =head1 SYNOPSIS
151              
152             use Test::HTML::Tidy::Recursive;
153              
154             Test::HTML::Tidy::Recursive->new({
155             targets => ['./dest-html', './dest-html-production'],
156             })->run;
157              
158             Or with over-riding the defaults:
159              
160             use Test::HTML::Tidy::Recursive;
161              
162             Test::HTML::Tidy::Recursive->new({
163             filename_re => qr/\.x?html?\z/i,
164             filename_filter => sub { return shift !~ m#MathJax#; },
165             targets => ['./dest-html', './dest-html-production'],
166             })->run;
167              
168             =head1 DESCRIPTION
169              
170             This module acts as test module which runs L<HTML::T5> on some directory
171             trees containing HTML/XHTML files and checks that they are all valid.
172              
173             It was extracted from a bunch of nearly duplicate test scripts in some of
174             my (= Shlomi Fish) web sites, as an attempt to avoid duplicate code and
175             functionality.
176              
177             =head1 METHODS
178              
179             =head2 calc_tidy
180              
181             Calculates the L<HTML::T5> object - can be overriden.
182              
183             =head2 filename_filter
184              
185             A parameter with a callback to filter the files. Defaults to accept all files.
186              
187             =head2 filename_re
188              
189             A regex for which filenames are checked. Defaults to files ending in ".html"
190             or ".xhtml".
191              
192             =head2 run
193              
194             The method that runs the program.
195              
196             =head2 $obj->check_file({filename => $path_string})
197              
198             Override this method in subclasses to check a file in a different way.
199              
200             =head2 $obj->check_using_tidy({filename => $path_string})
201              
202             Actually check a file using tidy. Used by check_file() by default,
203             but can also be called there in subclasses.
204              
205             =head2 $obj->report_error({message => $string});
206              
207             Reports the error and increment the error count.
208              
209             =head2 $obj->traverse()
210              
211             The method that gets called by run() to do the actual traversal of the tree
212             without actually checking for no errors. Useful for testing and debugging.
213              
214             =head2 targets
215              
216             A parameter that accepts an array reference of targets as strings.
217              
218             =head1 SEE ALSO
219              
220             L<HTML::T5> .
221              
222             =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
223              
224             =head1 SUPPORT
225              
226             =head2 Websites
227              
228             The following websites have more information about this module, and may be of help to you. As always,
229             in addition to those websites please use your favorite search engine to discover more resources.
230              
231             =over 4
232              
233             =item *
234              
235             MetaCPAN
236              
237             A modern, open-source CPAN search engine, useful to view POD in HTML format.
238              
239             L<https://metacpan.org/release/Test-HTML-Tidy-Recursive>
240              
241             =item *
242              
243             RT: CPAN's Bug Tracker
244              
245             The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
246              
247             L<https://rt.cpan.org/Public/Dist/Display.html?Name=Test-HTML-Tidy-Recursive>
248              
249             =item *
250              
251             CPANTS
252              
253             The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
254              
255             L<http://cpants.cpanauthors.org/dist/Test-HTML-Tidy-Recursive>
256              
257             =item *
258              
259             CPAN Testers
260              
261             The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
262              
263             L<http://www.cpantesters.org/distro/T/Test-HTML-Tidy-Recursive>
264              
265             =item *
266              
267             CPAN Testers Matrix
268              
269             The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
270              
271             L<http://matrix.cpantesters.org/?dist=Test-HTML-Tidy-Recursive>
272              
273             =item *
274              
275             CPAN Testers Dependencies
276              
277             The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
278              
279             L<http://deps.cpantesters.org/?module=Test::HTML::Tidy::Recursive>
280              
281             =back
282              
283             =head2 Bugs / Feature Requests
284              
285             Please report any bugs or feature requests by email to C<bug-test-html-tidy-recursive at rt.cpan.org>, or through
286             the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Test-HTML-Tidy-Recursive>. You will be automatically notified of any
287             progress on the request by the system.
288              
289             =head2 Source Code
290              
291             The code is open to the world, and available for you to hack on. Please feel free to browse it and play
292             with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
293             from your repository :)
294              
295             L<https://github.com/shlomif/perl-Test-HTML-Tidy-Recursive>
296              
297             git clone https://github.com/shlomif/perl-Test-HTML-Tidy-Recursive
298              
299             =head1 AUTHOR
300              
301             Shlomi Fish <shlomif@cpan.org>
302              
303             =head1 BUGS
304              
305             Please report any bugs or feature requests on the bugtracker website
306             L<https://github.com/shlomif/test-html-tidy-recursive/issues>
307              
308             When submitting a bug or request, please include a test-file or a
309             patch to an existing test-file that illustrates the bug or desired
310             feature.
311              
312             =head1 COPYRIGHT AND LICENSE
313              
314             This software is Copyright (c) 2016 by Shlomi Fish.
315              
316             This is free software, licensed under:
317              
318             The MIT (X11) License
319              
320             =cut