File Coverage

blib/lib/Test/Pod/Coverage/TrustMe.pm
Criterion Covered Total %
statement 84 97 86.6
branch 16 30 53.3
condition 11 29 37.9
subroutine 12 12 100.0
pod 3 3 100.0
total 126 171 73.6


line stmt bran cond sub pod time code
1             package Test::Pod::Coverage::TrustMe;
2 1     1   489 use strict;
  1         8  
  1         30  
3 1     1   5 use warnings;
  1         2  
  1         48  
4              
5             our $VERSION = '0.001002';
6             $VERSION =~ tr/_//d;
7              
8 1     1   6 use File::Spec ();
  1         2  
  1         12  
9 1     1   14 use Cwd ();
  1         4  
  1         33  
10 1     1   776 use Test::Builder ();
  1         60842  
  1         30  
11              
12 1     1   8 use Exporter ();
  1         2  
  1         1276  
13             *import = \&Exporter::import;
14              
15             our @EXPORT = qw(
16             all_modules
17             pod_coverage_ok
18             all_pod_coverage_ok
19             );
20              
21             sub _blib {
22 1     1   4 my $dir = File::Spec->curdir;
23 1         2 my $try = 5;
24 1         4 while ($try--) {
25 1         7 my $blib = File::Spec->catdir($dir, 'blib');
26 1 50 33     58 if (
      33        
27             -d $blib
28             && -d File::Spec->catdir($blib, 'arch')
29             && -d File::Spec->catdir($blib, 'lib')
30             ) {
31 1         7 return $blib;
32             }
33              
34 0         0 $dir = File::Spec->catdir($dir, File::Spec->updir);
35             }
36 0         0 return undef;
37             }
38              
39             sub _lib {
40 1     1   10 my $dir = File::Spec->curdir;
41 1         2 my $try = 5;
42 1         4 while ($try--) {
43 1         9 my $lib = File::Spec->catdir($dir, 'lib');
44 1 50       22 if (-d $lib) {
45 1         35 my @parts = File::Spec->splitdir(Cwd::realpath($lib));
46 1 50 33     26 if (
      33        
      33        
47             @parts >= 2
48             && $parts[-1] eq 'lib'
49             && ($parts[-2] eq 't' || $parts[-2] eq 'xt')
50             ) {
51 0         0 next;
52             }
53              
54 1         6 return $lib;
55             }
56              
57 0         0 $dir = File::Spec->catdir($dir, File::Spec->updir);
58             }
59 0         0 return undef;
60             }
61              
62             sub _base_dirs {
63 1     1   2 my %find;
64 1 50       2 if (my $lib = _lib()) {
65 1         21 $find{Cwd::realpath($lib)}++;
66             }
67 1 50       4 if (my $blib = _blib()) {
68 1         30 $find{Cwd::realpath(File::Spec->catdir($blib, 'arch'))}++;
69 1         30 $find{Cwd::realpath(File::Spec->catdir($blib, 'lib'))}++;
70             }
71              
72 1         422 my @dirs = grep $find{Cwd::realpath($_)}, @INC;
73 1         6 return @dirs;
74             }
75              
76             sub all_modules {
77 1     1 1 3 my @dirs = @_;
78 1 50       4 @dirs = _base_dirs
79             if !@dirs;
80              
81 1         4 my %searched;
82             my @modules;
83 1         0 my %modules;
84              
85 1         5 my @search = map [$_], @dirs;
86 1         5 while (my $search = shift @search) {
87 18         65 my ($dir, @pre) = @$search;
88             next
89 18 100       1178 if $searched{Cwd::realpath($dir)}++;
90 16 50       492 opendir my $dh, $dir or die;
91 16         481 my @found = File::Spec->no_upwards(readdir $dh);
92 16         172 closedir $dh;
93              
94 16   66     176 my @mods = grep /\.pm\z/ && -f File::Spec->catfile($dir, $_), @found;
95 16         50 s/\.pm\z// for @mods;
96             push @modules,
97 16         49 grep !$modules{$_}++,
98             map join('::', @pre, $_),
99             grep !/\W/,
100             @mods;
101              
102 16         508 unshift @search,
103             map [ $_->[0], @pre, $_->[1] ],
104             grep -d $_->[0],
105             map [ File::Spec->catdir($dir, $_) => $_ ],
106             grep !/\W/,
107             @found;
108             }
109              
110 1         9 return @modules;
111             }
112              
113             sub pod_coverage_ok {
114 3     3 1 7 my $module = shift;
115 3 50       13 my %opts = ref $_[0] eq 'HASH' ? %{ +shift } : ();
  0         0  
116 3   33     18 my $msg = shift || "Pod coverage on $module";
117              
118 3         9 $opts{package} = $module;
119              
120 3   50     13 my $class = delete $opts{coverage_class} || 'Pod::Coverage::TrustMe';
121 3         19 (my $mod = "$class.pm") =~ s{::}{/}g;
122 3         537 require $mod;
123              
124 3         23 my $cover = $class->new(%opts);
125              
126 3   33     10 our $Test ||= Test::Builder->new;
127 3         9 local $Test::Builder::Level = $Test::Builder::Level + 1;
128              
129 3         4 my $ok;
130 3         11 my $rating = $cover->coverage;
131 3 50       10 if (!defined $rating) {
132 0         0 my $why = $cover->why_unrated;
133 0         0 $ok = $Test->ok( defined $cover->symbols, $msg );
134 0         0 $Test->diag( "$module: ". $cover->why_unrated );
135             }
136             else {
137 3         63 $ok = $Test->is_eq((map sprintf('%3.0f%%', $_ * 100), $rating, 1), $msg);
138 3 50       2246 if (!$ok) {
139 0         0 $Test->diag(join('',
140             "Naked subroutines:\n",
141             map " $_\n", $cover->uncovered,
142             ));
143             }
144             }
145 3         72 return $ok;
146             }
147              
148             sub all_pod_coverage_ok {
149 1 50   1 1 83 my %opts = ref $_[0] eq 'HASH' ? %{ +shift } : ();
  0         0  
150 1         3 my $dirs = delete $opts{dirs};
151 1 50       3 my @modules = all_modules(@{ $dirs || [] });
  1         7  
152              
153 1   33     12 our $Test ||= Test::Builder->new;
154 1         15 local $Test::Builder::Level = $Test::Builder::Level + 1;
155 1         2 my $ok = 1;
156              
157 1 50       3 if ( @modules ) {
158 1         5 $Test->plan( tests => scalar @modules );
159              
160 1         776 for my $module ( @modules ) {
161 3 50       12 pod_coverage_ok( $module, @_ ) or $ok = 0;
162             }
163             }
164             else {
165 0         0 $Test->plan( tests => 1 );
166 0         0 $Test->ok( 1, "No modules found." );
167             }
168              
169 1         148 return $ok;
170             }
171              
172             1;
173             __END__