blib/lib/Test/Pod/CoverageChange.pm

filter: Statementstmt 98.1% 107 / 109 Branchbran 84.3% 27 / 32 Conditioncond 67.6% 23 / 34 MC/DCmcdc 36.8% 7 / 19 Subroutinesub 95.2% 20 / 21 Podpod 100% 1 / 1 totaltotal 85.6% 185 / 216 CC 36 SCAR 21.2
CC
36
Cov
90%
_check_allowed_naked_packages
15.2
_check_pod_coverage
11.4
_check_pod_syntax
9.1
CRAP 8.3
↑ Summary
1 package Test::Pod::CoverageChange;
2 # ABSTRACT: Test Perl files for POD coverage and syntax changes
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '0.005';
8 5 our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY
9  
10 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =encoding utf8
13  
14 =head1 NAME
15  
16 Test::Pod::CoverageChange - Test Perl files for POD coverage and syntax changes
17  
18 =head1 SYNOPSIS
19  
20 use Test::Pod::CoverageChange qw(pod_coverage_syntax_ok);
21  
22 pod_coverage_syntax_ok('lib', {
23 MyModule::Bar => 3, ## expected to have 3 naked subs
24 MyModule::Foo => 10, ## expected to have 10 naked subs
25 MyModule::Baz => 1, ## expected to have 1 naked subs
26 MyModule::Qux => 5, ## expected to have 5 naked subs
27 }, [
28 We::Ignore::ThisModule,
29 We::Also::Ignore::This::Module
30 ],[
31 'a_sub_name_to_ignore'
32 qr/regexes are also acceptable/
33 ]);
34  
35 =head1 DESCRIPTION
36  
37 C<Test::Pod::CoverageChange> is a helper combining L<Test::Pod::Coverage> and
38 L<Pod::Checker> to test for both POD coverage and syntax changes for a module
39 distribution at once, via a single call to L</pod_coverage_syntax_ok>.
40  
41 Possible results
42  
43 =over 4
44  
45 =item * B<passes> if the file has no POD syntax or coverage error.
46  
47 =item * B<fails> if latest changes increased/decreased numbers of naked subs for the packages that have allowed naked subs.
48  
49 =item * B<fails> if a package allowed to have naked subs has 100% POD coverage.
50  
51 =item * B<fails> if a file in a given path has POD syntax error or has no POD.
52  
53 =back
54  
55 Ignores packages that passed as ignored package in the c<$ignored_package> argument into the pod_coverage_syntax_ok sub.
56  
57 =cut
58  
59 5 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 5 ▶use Pod::Checker;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 5 ▶use Pod::Coverage;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 5 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 5 ▶use Test::Pod::Coverage;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 5 ▶use Module::Path qw(module_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
65 5 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
66 5 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
68 5 POD_SYNTAX_IS_OK => 0,
69 FILE_HAS_NO_POD => -1,
70 5 };
71  
72 5 ▶use Exporter qw(import export_to_level);
Subroutine BEGIN: called CC=1 SCAR=0.0
73 5 our @EXPORT_OK = qw(pod_coverage_syntax_ok);
74  
75 =head2 pod_coverage_syntax_ok
76  
77 Checks all the modules under a given directory against POD coverage and POD syntax
78  
79 =over 4
80  
81 =item * C<$path> - path or arrayref of directories to check (recursively)
82  
83 example: ['lib', 'other directory'] | 'lib'
84  
85 =item * C<$allowed_naked_packages> - hashref of number of allowed naked subs, keyed by package name (optional)
86  
87 example: {Package1 => 2, Package2 => 1, Package3 => 10}
88  
89 =item * C<$ignored_packages> - arrayref of packages that will be ignored in the checks (optional)
90  
91 example: ['MyPackage1', 'MyPackage2', 'MyPackage3']
92  
93 =item * C<$ignored_subs> - arrayref of subnames or regexes that will be ignored in the checks (optional)
94  
95 example: ['a_sub_name', qr/a regex/]
96  
97 =back
98  
99 =cut
100  
101 11 ▶sub pod_coverage_syntax_ok {
Subroutine pod_coverage_syntax_ok: called CC=5 SCAR=16.3
Pod documented
102 11 my %args = @_;
103 11 my %default_values = (path => 'lib', allowed_naked_packages => {}, ignored_packages => [], ignored_subs => []);
104  
105 11 %args = (%default_values, %args);
106  
107 11 ▶ my $path = $args{path} // 'lib';
Condition50%1/2
$args{"path"} // "lib"
l!l
110
MC/DC0%0/1
$args{"path"} // "lib"
$args{"path"}
Truth table50%1/2
A
A= $args{"path"}
Aresult
11
00
108 11 ▶ my $ignored_packages = $args{ignored_packages} // [];
Condition50%1/2
$args{"ignored_packages"} // []
l!l
110
MC/DC0%0/1
$args{"ignored_packages"} // []
$args{"ignored_packages"}
Truth table50%1/2
A
A= $args{"ignored_packages"}
Aresult
11
00
109  
110 11 ▶ $path = [$path] unless ref $path eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path eq "ARRAY" 9 2 11
111 11 ▶ $ignored_packages = [$ignored_packages] unless ref $ignored_packages eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ignored_packages eq "ARRAY" 0 11 11
112  
113 11 _check_pod_coverage($path, $args{allowed_naked_packages}, $ignored_packages, $args{ignored_subs});
114 11 _check_pod_syntax($path, $ignored_packages);
115  
116 11 return undef;
117 }
118  
119 =head2 _check_pod_coverage
120  
121 Checks POD coverage for all the modules that exist under the given directory.
122 Passes the C<$allowed_naked_packages> to L<Test::Pod::CoverageChange/_check_allowed_naked_packages>.
123 Ignores the packages in the C<$ignored_packages> parameter.
124  
125 =over 4
126  
127 =item * C<$path> - path or arrayref of directories to check (recursively)
128  
129 =item * C<$allowed_naked_packages> - hashref of number of allowed naked subs, keyed by package name (optional)
130  
131 =item * C<$ignored_packages> - arrayref of packages that will be ignored in the checks (optional)
132  
133 =back
134  
135 =cut
136  
137 11 ▶sub _check_pod_coverage {
Subroutine _check_pod_coverage: called CC=10 SCAR=24.3
138 11 ▶ my $path = shift // 'lib';
Condition50%1/2
shift() // "lib"
l!l
110
MC/DC0%0/1
shift() // "lib"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
139 11 ▶ my $allowed_naked_packages = shift // {};
Condition50%1/2
shift() // {}
l!l
110
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
140 11 ▶ my $ignored_packages = shift // [];
Condition50%1/2
shift() // []
l!l
110
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
141 11 ▶ my $ignored_subs = shift // [];
Condition50%1/2
shift() // []
l!l
110
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
142  
143 11 ▶ _check_allowed_naked_packages($allowed_naked_packages, $ignored_packages) if keys %$allowed_naked_packages;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$allowed_naked_packages 6 5 11
144  
145 # Check for newly added packages PODs
146 11 my @ignored_packages = (keys %$allowed_naked_packages, @$ignored_packages);
147 11 foreach my $package (Test::Pod::Coverage::all_modules(@$path)) {
148 15 ▶ next if any { $_ eq $package } @ignored_packages;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $package; } , @ignored_packages) 9 6 15
149 6 ▶ if(!pod_coverage_ok($package, {trustme => [qw(DOES META)], private => [], also_private => $ignored_subs})
Branch: 1 branch
BranchTrueFalseTotal
if (not pod_coverage_ok($package, {"trustme", ["DOES", "META"], "private", [], "also_private", $ignored_subs}) and _package_is_object_pad($package)) 0 6 6
Condition66%2/3
not pod_coverage_ok($package, {"trustme", ["DOES", "META"], "private", [], "also_private", $ignored_subs}) and _package_is_object_pad($package)
!ll&&!rl&&r
420
MC/DC0%0/2
not pod_coverage_ok($package, {"trustme", ["DOES", "META"], "private", [], "also_private", $ignored_subs}) and _package_is_object_pad($package)
not pod_coverage_ok($package, {"trustme", ["DOES", "META"], "private", [], "also_private", $ignored_subs}) _package_is_object_pad($package)
Truth table66%2/3
not(A) and B
A= not pod_coverage_ok($package, {"trustme", ["DOES", "META"], "private", [], "also_private", $ignored_subs})B= _package_is_object_pad($package)
ABresult
0X0
100
111
150 && _package_is_object_pad($package)){
151 0 diag("Package $package is an Object::Pad class, Do you miss PODs of auto-generated methods?"
152 . 'it will generate method "new" and getter and setter of member fields like:
153 has $XXXX :reader; #=> will create a method "XXXX"
154 has $YYYY :writer: #=> will create a method "set_YYYY"');
155 }
156  
157 }
158  
159 11 return undef;
160 }
161  
162 2 ▶sub _package_is_object_pad{
Subroutine _package_is_object_pad: called CC=1 SCAR=0.0
163 2 my $package = shift;
164 2 my $file = module_path($package);
165 2 return path($file)->slurp_utf8 =~ /Object::Pad/;
166 }
167  
168 =head2 _check_pod_syntax
169  
170 Check POD syntax for all the modules that exist under the given directory.
171  
172 =over 4
173  
174 =item * C<$path> - path or arrayref of directories to check (recursively)
175  
176 =item * C<$ignored_packages> - arrayref of packages that will be ignored in the checks (optional)
177  
178 =back
179  
180 =cut
181  
182 11 ▶sub _check_pod_syntax {
Subroutine _check_pod_syntax: called CC=9 SCAR=22.1
183 11 ▶ my $path = shift // 'lib';
Condition50%1/2
shift() // "lib"
l!l
110
MC/DC0%0/1
shift() // "lib"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
184 11 ▶ my $ignored_packages = shift // [];
Condition50%1/2
shift() // []
l!l
110
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
185  
186 11 my $Test_Builder = Test::More->builder;
187  
188 11 my @ignored_packages_full_path = ();
189 11 for (@$ignored_packages) {
190 1 my $file_path = module_path($_);
191 1 ▶ push @ignored_packages_full_path, $file_path if defined $file_path;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file_path 1 0 1
192 }
193  
194 11 my @files_path = File::Find::Rule->file()->name('*.p[m|l]')->in(@$path);
195  
196 11 for my $file_path (@files_path) {
197 15 ▶ next if any { /\Q$file_path/ } @ignored_packages_full_path;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { /\Q$file_path\E/; } , @ignored_packages_full_path) 1 14 15
198  
199 14 my $check_result = podchecker($file_path);
200 14 ▶ if ($check_result == POD_SYNTAX_IS_OK) {
Branch: 2 branches
BranchTrueFalseTotal
if ($check_result == 0) { } 7 7 14
elsif ($check_result == -1) { } 6 1 7
201 7 $Test_Builder->ok(1, sprintf("Pod structure is OK in the file %s.", $file_path));
202 } elsif ($check_result == FILE_HAS_NO_POD) {
203 6 $Test_Builder->todo_skip(sprintf("There is no POD in the file %s.", $file_path));
204 } else {
205 1 $Test_Builder->ok(0, sprintf("There are %d errors in the POD structure in the %s.", $check_result, $file_path));
206 }
207 }
208  
209 11 return undef;
210 }
211  
212 =head2 _check_allowed_naked_packages
213  
214 Checks passed allowed_naked_packages against existing package files.
215  
216 =over 4
217  
218 =item * C<$allowed_naked_packages> - hashref of number of allowed naked subs, keyed by package name (optional)
219  
220 =item * C<$ignored_packages> - a list of packages that will be ignored in our checks, supports arrayref (optional)
221  
222 =back
223  
224 Possible results
225  
226 =over 4
227  
228 =item * B<todo fail> if the numbers of existing naked subs are equal to passed value.
229  
230 =item * B<fails> if the number of existing naked subs are not equal to the passed value.
231  
232 =item * B<fails> if a package has 100% POD coverage and it passed as a L<$allowed_naked_package>.
233  
234 =back
235  
236 =cut
237  
238 6 ▶sub _check_allowed_naked_packages {
Subroutine _check_allowed_naked_packages: called CC=15 SCAR=27.2
239 6 ▶ my $allowed_naked_packages = shift // {};
Condition50%1/2
shift() // {}
l!l
60
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
240 6 ▶ my $ignored_packages = shift // [];
Condition50%1/2
shift() // []
l!l
60
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
241  
242 6 my $Test_Builder = Test::More->builder;
243 6 my $caller_test_file = (caller(2))[1];
244  
245 # Check for the currently naked packages POD.
246 6 foreach my $package (sort keys %$allowed_naked_packages) {
247 0 ▶ next if any { /^\Q$package\E$/ } @$ignored_packages;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { /^\Q$package\E$/; } , @$ignored_packages) 0 8 8
248  
249 8 my $pc = Pod::Coverage->new(
250 package => $package,
251 private => []);
252  
253 # Require before we run the coverage to pick up any errors loading that
254 # are getting hidden by the coverage check.
255 # Do not move to after $pc usage otherwise $@ will reflect the failure
256 # from coverage which won't be the same.
257 8 eval qq{ require $package };
258 8 ▶ die if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
259  
260 8 ▶ my $fully_covered = defined $pc->coverage && $pc->coverage == 1;
Condition100%3/3
defined $pc->coverage && $pc->coverage == 1
!ll&&!rl&&r
521
MC/DC100%2/2
defined $pc->coverage && $pc->coverage == 1
defined $pc->coverage $pc->coverage == 1
Truth table100%3/3
A && B
A= defined $pc->coverageB= $pc->coverage == 1
ABresult
0X0
100
111
261 8 ▶ my $coverage_percentage = defined $pc->coverage ? $pc->coverage * 100 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $pc->coverage ? : 3 5 8
262 8 my $max_expected_naked_subs = $allowed_naked_packages->{$package};
263 8 ▶ my $naked_subs_count = scalar $pc->naked // scalar $pc->_get_syms($package);
Condition100%2/2
scalar $pc->naked // scalar $pc->_get_syms($package)
l!l
35
MC/DC100%1/1
scalar $pc->naked // scalar $pc->_get_syms($package)
scalar $pc->naked
Truth table100%2/2
A
A= scalar $pc->naked
Aresult
11
00
264  
265 8 ▶ if (!$fully_covered) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fully_covered) 7 1 8
266 7 $Test_Builder->todo_skip(sprintf("You have %.2f%% POD coverage for the module '%s'.", $coverage_percentage, $package));
267 }
268  
269 8 ▶ if (!$fully_covered && $naked_subs_count < $max_expected_naked_subs) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $fully_covered and $naked_subs_count < $max_expected_naked_subs) { } 1 7 8
elsif (not $fully_covered and $naked_subs_count > $max_expected_naked_subs) { } 2 6 8
Condition100%3/3
not $fully_covered and $naked_subs_count < $max_expected_naked_subs
!ll&&!rl&&r
161
MC/DC100%2/2
not $fully_covered and $naked_subs_count < $max_expected_naked_subs
not $fully_covered $naked_subs_count < $max_expected_naked_subs
Truth table100%3/3
not(A) and B
A= not $fully_coveredB= $naked_subs_count < $max_expected_naked_subs
ABresult
0X0
100
111
270 1 $Test_Builder->ok(
271 0,
272 sprintf(
273 "Your last changes decreased the number of naked subs in the %s package.
274 Change the %s => %s in the %s file please.", $package, $package, $naked_subs_count, $caller_test_file
275 ));
276 1 next;
277 ▶ } elsif (!$fully_covered && $naked_subs_count > $max_expected_naked_subs) {
Condition100%3/3
not $fully_covered and $naked_subs_count > $max_expected_naked_subs
!ll&&!rl&&r
142
MC/DC100%2/2
not $fully_covered and $naked_subs_count > $max_expected_naked_subs
not $fully_covered $naked_subs_count > $max_expected_naked_subs
Truth table100%3/3
not(A) and B
A= not $fully_coveredB= $naked_subs_count > $max_expected_naked_subs
ABresult
0X0
100
111
278 2 $Test_Builder->ok(0, sprintf("Your last changes increased the number of naked subs in the %s package from %s to %s. Please add pod for your new subs.",
279 $package, $max_expected_naked_subs, $naked_subs_count));
280 2 next;
281 }
282  
283 5 ▶ if ($fully_covered) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fully_covered) 1 4 5
284 1 $Test_Builder->ok(
285 0,
286 sprintf(
287 '%s modules has 100%% POD coverage. Please remove it from the %s file $allowed_naked_packages variable to fix this error.',
288 $package, $caller_test_file
289 ));
290 }
291 }
292  
293 6 return undef;
294 }
295  
296 5 1;
↑ Summary