File Coverage

lib/Test/Perl/Critic/XTFiles.pm
Criterion Covered Total %
statement 61 61 100.0
branch 14 14 100.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 0 1 0.0
total 87 88 98.8


line stmt bran cond sub pod time code
1             package Test::Perl::Critic::XTFiles;
2              
3 6     6   1912800 use 5.006;
  6         62  
4 6     6   32 use strict;
  6         13  
  6         119  
5 6     6   39 use warnings;
  6         11  
  6         851  
6              
7             our $VERSION = '0.001';
8              
9             use Class::Tiny 1 {
10 1         911 critic => sub { Perl::Critic->new(); },
11 3         1706692 critic_module => sub { shift->critic(); },
12 1         732 critic_script => sub { shift->critic(); },
13 1         680 critic_test => sub { shift->critic(); },
14 6     6   1537 };
  6         5545  
  6         81  
15              
16 6     6   7174 use Perl::Critic ();
  6         4781734  
  6         175  
17 6     6   55 use Perl::Critic::Violation ();
  6         16  
  6         93  
18 6     6   33 use Test::Builder ();
  6         58  
  6         95  
19 6     6   2810 use Test::XTFiles ();
  6         84049  
  6         2242  
20              
21             my $TEST = Test::Builder->new;
22              
23             # - Do not use subtests because subtests cannot be tested with
24             # Test::Builder:Tester.
25             # - Do not use a plan because a method that sets a plan cannot be tested
26             # with Test::Builder:Tester.
27              
28             sub all_files_ok {
29 7     7 0 23930 my ($self) = @_;
30              
31             # ignore pod files
32 7 100 100     52 my @files = grep { $_->is_module || $_->is_test || $_->is_script } Test::XTFiles->new->files;
  7         27177  
33              
34 7 100       187 if ( !@files ) {
35 1         18 $TEST->skip_all("No files found\n");
36 1         11 return 1;
37             }
38              
39 6         88 my $rc = 1;
40              
41 6         19 for my $file (@files) {
42              
43 6 100       24 my $critic =
    100          
44             $file->is_test ? $self->critic_test
45             : $file->is_script ? $self->critic_script
46             : $self->critic_module;
47              
48 6         209 my $critic_error;
49             my $critic_status;
50 6         0 my $critic_ok;
51 6         0 my @violations;
52              
53             {
54 6         12 local $@; ## no critic (Variables::RequireInitializationForLocalVars)
  6         12  
55              
56 6         44 $critic_status = eval {
57 6         101 @violations = $critic->critique( $file->name );
58 5         82 $critic_ok = !@violations;
59 5         11 1;
60             };
61              
62 6         32 $critic_error = $@;
63             }
64              
65 6         30 $TEST->ok( $critic_ok, qq{Perl::Critic for "$file"} );
66              
67 6 100       4190 if ( !$critic_status ) {
    100          
68              
69             # exception from Perl::Critic
70 1         4 $TEST->diag("\n");
71 1         258 $TEST->diag(qq{Perl::Critic had errors in "$file":});
72 1         271 $TEST->diag(qq{\t$critic_error});
73 1         235 $rc = 0;
74             }
75             elsif ( !$critic_ok ) {
76              
77             # Perl::Critic reported policy violations
78 2         7 $TEST->diag("\n");
79 2         520 my $verbose = $critic->config->verbose();
80 2         152 Perl::Critic::Violation::set_format($verbose);
81 2         74 for my $violation (@violations) {
82 4         488 $TEST->diag(" $violation");
83             }
84              
85 2         480 $rc = 0;
86             }
87             }
88              
89 6         29 $TEST->done_testing;
90              
91 6 100       37 return 1 if $rc;
92 3         8 return;
93             }
94              
95             1;
96              
97             __END__
98              
99             =pod
100              
101             =encoding UTF-8
102              
103             =head1 NAME
104              
105             Test::Perl::Critic::XTFiles - Perl::Critic test with XT::Files interface
106              
107             =head1 VERSION
108              
109             Version 0.001
110              
111             =head1 SYNOPSIS
112              
113             use Test::Perl::Critic::XTFiles;
114             Test::Perl::Critic::XTFiles->new->all_files_ok;
115              
116             use Perl::Critic;
117             use Test::Perl::Critic::XTFiles;
118             Test::Perl::Critic::XTFiles->new(
119             critic => Perl::Critic->new( -profile => 'xt/author/perlcritic.rc' ),
120             )->all_files_ok;
121              
122             =head1 DESCRIPTION
123              
124             Tests all the files supplied from L<XT::Files> with L<Perl::Critic>. The
125             output, and behavior, should be the same as from L<Test::Perl::Critic>.
126              
127             =head1 USAGE
128              
129             =head2 new( [ ARGS ] )
130              
131             Returns a new C<Test::Perl::Critic::XTFiles> instance. C<new> takes an
132             optional hash or list with its arguments.
133              
134             Test::Perl::Critic::XTFiles->new(
135             critic => Perl::Critic->new( -profile => '.perltidyrc' ),
136             critic_test => Perl::Critic->new( -profile => '.perltidyrc-tests' ),
137             );
138              
139             The following arguments are supported:
140              
141             =head3 critic, critic_module, critic_script, critic_test (optional)
142              
143             Sets the default L<Perl::Critic> object and the objects used to test
144             module, script or test files. See the method with the same name for further
145             explanation.
146              
147             =head2 all_file_ok
148              
149             Calls the C<files> method of L<Test::XTFiles> to get all the files to
150             be tested. All files are tested with the L<Perl::Critic> object configured
151             for their type.
152              
153             It calls C<done_testing> or C<skip_all> so you can't have already called
154             C<plan>.
155              
156             C<all_files_ok> returns something I<true> if all files test ok and I<false>
157             otherwise.
158              
159             Please see L<XT::Files> for how to configure the files to be checked.
160              
161             =head2 critic
162              
163             Returns, and optionally sets, the L<Perl::Critic> default object. This is
164             only used to initialize the other C<critic_*> methods. On first access this
165             is initialized to C<Perl::Critic-E<gt>new()>.
166              
167             =head2 critic_module( [ARGS] )
168              
169             Returns, and optionally sets, the L<Perl::Critic> object used to test module
170             files. On first access this is initialized to C<$self-E<gt>critic()>.
171              
172             =head2 critic_script( [ARGS] )
173              
174             Returns, and optionally sets, the L<Perl::Critic> object used to test script
175             files. On first access this is initialized to C<$self-E<gt>critic()>.
176              
177             =head2 critic_test( [ARGS] )
178              
179             Returns, and optionally sets, the L<Perl::Critic> object used to test test
180             files. On first access this is initialized to C<$self-E<gt>critic()>.
181              
182             =head1 EXAMPLES
183              
184             =head2 Example 1 Default usage
185              
186             Check all the files returned by L<XT::Files> with L<Perl::Critic>.
187              
188             use 5.006;
189             use strict;
190             use warnings;
191              
192             use Test::Perl::Critic::XTFiles;
193              
194             Test::Perl::Critic::XTFiles->new->all_files_ok;
195              
196             =head2 Example 2 Check non-default directories or files
197              
198             Use the same test file as in Example 1 and create a F<.xtfilesrc> config
199             file in the root directory of your distribution.
200              
201             [Dirs]
202             module = lib
203             module = tools
204             module = corpus/hello
205              
206             [Files]
207             module = corpus/world.pm
208              
209             =head2 Example 3 Use a different Perl::Critic config file for script files
210              
211             use 5.006;
212             use strict;
213             use warnings;
214              
215             use Perl::Critic;
216             use Test::Perl::Critic::XTFiles;
217              
218             Test::Perl::Critic::XTFiles->new(
219             critic_script => Perl::Critic->new( -profile => '.perlcriticrc-scripts' ),
220             )->all_files_ok;
221              
222             =head1 SEE ALSO
223              
224             L<Test::More>, L<Perl::Critic>, L<XT::Files>
225              
226             =head1 SUPPORT
227              
228             =head2 Bugs / Feature Requests
229              
230             Please report any bugs or feature requests through the issue tracker
231             at L<https://github.com/skirmess/Test-Perl-Critic-XTFiles/issues>.
232             You will be notified automatically of any progress on your issue.
233              
234             =head2 Source Code
235              
236             This is open source software. The code repository is available for
237             public review and contribution under the terms of the license.
238              
239             L<https://github.com/skirmess/Test-Perl-Critic-XTFiles>
240              
241             git clone https://github.com/skirmess/Test-Perl-Critic-XTFiles.git
242              
243             =head1 AUTHOR
244              
245             Sven Kirmess <sven.kirmess@kzone.ch>
246              
247             =head1 COPYRIGHT AND LICENSE
248              
249             This software is Copyright (c) 2019 by Sven Kirmess.
250              
251             This is free software, licensed under:
252              
253             The (two-clause) FreeBSD License
254              
255             =cut
256              
257             # vim: ts=4 sts=4 sw=4 et: syntax=perl