File Coverage

blib/lib/Test2/Tools/Pod.pm
Criterion Covered Total %
statement 69 69 100.0
branch 22 22 100.0
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 102 102 100.0


line stmt bran cond sub pod time code
1 5     5   1214660 use v5.42;
  5         20  
2              
3             package Test2::Tools::Pod v0.1.0;
4              
5 5     5   31 use Exporter 'import';
  5         13  
  5         206  
6 5     5   3208 use Path::Iterator::Rule;
  5         79492  
  5         220  
7 5     5   2152 use Path::Tiny;
  5         35895  
  5         441  
8 5     5   3516 use Pod::Simple;
  5         245947  
  5         237  
9 5     5   45 use Test2::API 'context';
  5         10  
  5         4464  
10              
11             our @EXPORT = qw/ pod_ok all_pod_ok /;
12             our @Ignore = qw/ .git .hg /;
13              
14 18         56 sub pod_ok ($file, $name = undef, $should_skip = true)
  18         29  
  18         33  
15 18     18 1 310903 {
  18         33  
16 18         43 my $ctx = context;
17 18 100       1355 $name = "POD syntax ok for $file" unless defined $name;
18              
19 18 100       353 unless (-f $file) {
20 2         14 $ctx->fail_and_release($name);
21 2         485 return false;
22             }
23              
24 16         172 my $parser = Pod::Simple->new;
25 16         639 $parser->output_string( \ my $null );
26 16         882 $parser->parse_file($file);
27              
28 16 100       27806 unless ($parser->content_seen) {
29             # no POD in this file
30 4 100       40 $ctx->skip($name, 'no POD found') if $should_skip;
31 4         462 $ctx->release;
32 4         142 return;
33             }
34              
35 12 100       105 if ($parser->any_errata_seen) {
36             # problems
37 3         19 my %errors = $parser->errata_seen->%*;
38              
39             # %errors:
40             # => [
41             # "",
42             # ...
43             # ],
44             # ...
45             # We need it flattened:
46             # "$file:: "
47             # "$file:: "
48             # ...
49             $ctx->fail_and_release(
50             $name,
51             map {
52 4         7 my $line = $_;
53 4         8 map { "$file:$line: $_" } $errors{$_}->@*
  5         24  
54             }
55 3         25 sort { $a <=> $b } keys %errors
  1         12  
56             );
57 3         787 return false;
58             } else {
59             # syntax OK
60 9         80 $ctx->pass_and_release($name);
61 9         1136 return true;
62             }
63             }
64              
65             sub all_pod_ok (@locations)
66 6     6 1 608013 {
  6         19  
  6         10  
67 6 100       117 @locations = -d 'blib' ? 'blib' : 'lib' unless @locations;
    100          
68              
69 6         22 my @candidates = _POD_candidates(@locations);
70 6         748 my $tested = 0;
71 6         33 my $ctx = context;
72              
73 6         7977 for my $file (@candidates) {
74 7         31 my $result = pod_ok($file, undef, false);
75 7 100       59 ++$tested if defined $result;
76             }
77              
78 6 100       18 unless ($tested) {
79             # no file with POD was found, emit a skip event
80 2         12 $ctx->skip('POD syntax ok', 'no POD files found');
81             }
82              
83 6         889 $ctx->release;
84 6         204 return $tested;
85             }
86              
87             sub _POD_candidates (@locations)
88 13     13   270130 {
  13         33  
  13         26  
89 13 100       48 return () unless @locations;
90              
91 12         103 my $r = Path::Iterator::Rule->new;
92 12 100       192 $r->skip_dirs(@Ignore) if @Ignore;
93 12         2668 $r->file;
94 12         421 $r->or(
95             $r->new->name(qr/\.(?:pl|pm|pod|psgi|t)$/i),
96             $r->new->shebang(qr/#!.*\bperl\b/),
97             );
98              
99 12         1953 my %seen;
100 12         44 grep { not $seen{$_}++ } $r->all(@locations);
  18         21391  
101             }
102              
103             1;
104              
105             __END__