File Coverage

blib/lib/Test/Pod/Coverage/TrustMe.pm
Criterion Covered Total %
statement 81 92 88.0
branch 14 26 53.8
condition 11 29 37.9
subroutine 12 12 100.0
pod 3 3 100.0
total 121 162 74.6


line stmt bran cond sub pod time code
1             package Test::Pod::Coverage::TrustMe;
2 1     1   405 use strict;
  1         6  
  1         26  
3 1     1   4 use warnings;
  1         2  
  1         42  
4              
5             our $VERSION = '0.001_001';
6             $VERSION =~ tr/_//d;
7              
8 1     1   5 use File::Spec ();
  1         1  
  1         11  
9 1     1   11 use Cwd ();
  1         2  
  1         28  
10 1     1   588 use Test::Builder ();
  1         52070  
  1         26  
11              
12 1     1   8 use Exporter ();
  1         2  
  1         983  
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         3 my $try = 5;
24 1         4 while ($try--) {
25 1         6 my $blib = File::Spec->catdir($dir, 'blib');
26 1 50 33     45 if (
      33        
27             -d $blib
28             && -d File::Spec->catdir($blib, 'arch')
29             && -d File::Spec->catdir($blib, 'lib')
30             ) {
31 1         6 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   13 my $dir = File::Spec->curdir;
41 1         2 my $try = 5;
42 1         3 while ($try--) {
43 1         10 my $lib = File::Spec->catdir($dir, 'lib');
44 1 50       18 if (-d $lib) {
45 1         32 my @parts = File::Spec->splitdir(Cwd::realpath($lib));
46 1 50 33     16 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         5 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       4 if (my $lib = _lib()) {
65 1         18 $find{Cwd::realpath($lib)}++;
66             }
67 1 50       15 if (my $blib = _blib()) {
68 1         27 $find{Cwd::realpath(File::Spec->catdir($blib, 'arch'))}++;
69 1         35 $find{Cwd::realpath(File::Spec->catdir($blib, 'lib'))}++;
70             }
71              
72 1         374 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       6 @dirs = _base_dirs
79             if !@dirs;
80              
81 1         4 my %searched;
82             my @modules;
83 1         0 my %modules;
84              
85 1         4 my @search = map [$_], @dirs;
86 1         5 while (my $search = shift @search) {
87 18         53 my ($dir, @pre) = @$search;
88             next
89 18 100       1007 if $searched{Cwd::realpath($dir)}++;
90 16 50       389 opendir my $dh, $dir or die;
91 16         407 my @found = File::Spec->no_upwards(readdir $dh);
92 16         154 closedir $dh;
93              
94 16   66     154 my @mods = grep /\.pm\z/ && -f File::Spec->catfile($dir, $_), @found;
95 16         42 s/\.pm\z// for @mods;
96             push @modules,
97 16         41 grep !$modules{$_}++,
98             map join('::', @pre, $_),
99             grep !/\W/,
100             @mods;
101              
102 16         400 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         7 return @modules;
111             }
112              
113             sub pod_coverage_ok {
114 3     3 1 6 my $module = shift;
115 3 50       13 my $opts = ref $_[0] eq 'HASH' ? shift : {};
116 3   33     17 my $msg = shift || "Pod coverage on $module";
117              
118 3         13 my %opts = (
119             %$opts,
120             package => $module,
121             );
122              
123 3   50     15 my $class = delete $opts{coverage_class} || 'Pod::Coverage::TrustMe';
124 3         19 (my $mod = "$class.pm") =~ s{::}{/}g;
125 3         481 require $mod;
126              
127 3         24 my $cover = $class->new(%opts);
128              
129 3   33     9 our $Test ||= Test::Builder->new;
130 3         9 local $Test::Builder::Level = $Test::Builder::Level + 1;
131              
132 3         4 my $ok;
133 3         10 my $rating = $cover->coverage;
134 3 50       10 if (!defined $rating) {
135 0         0 my $why = $cover->why_unrated;
136 0         0 $ok = $Test->ok( defined $cover->symbols, $msg );
137 0         0 $Test->diag( "$module: ". $cover->why_unrated );
138             }
139             else {
140 3         79 $ok = $Test->is_eq((map sprintf('%3.0f%%', $_ * 100), $rating, 1), $msg);
141 3 50       2081 if (!$ok) {
142 0         0 $Test->diag(join('',
143             "Naked subroutines:\n",
144             map " $_\n", $cover->uncovered,
145             ));
146             }
147             }
148 3         73 return $ok;
149             }
150              
151             sub all_pod_coverage_ok {
152 1     1 1 72 my @modules = all_modules();
153              
154 1   33     12 our $Test ||= Test::Builder->new;
155 1         15 local $Test::Builder::Level = $Test::Builder::Level + 1;
156 1         2 my $ok = 1;
157              
158 1 50       3 if ( @modules ) {
159 1         7 $Test->plan( tests => scalar @modules );
160              
161 1         756 for my $module ( @modules ) {
162 3 50       14 pod_coverage_ok( $module, @_ ) or $ok = 0;
163             }
164             }
165             else {
166 0         0 $Test->plan( tests => 1 );
167 0         0 $Test->ok( 1, "No modules found." );
168             }
169              
170 1         135 return $ok;
171             }
172              
173             1;
174             __END__