lib/Pod/Tidy.pm

filter: Statementstmt 94.2% 130 / 138 Branchbran 70.6% 41 / 58 Conditioncond 57.8% 11 / 19 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 20 / 20 Podpod 100% 7 / 7 totaltotal 84.5% 214 / 253 CC 40 SCAR 28.1
CC
40
Cov
85%
build_pod_queue
36.9
process_pod_queue
8.1
valid_pod_syntax
5.0
CRAP 16.6
↑ Summary lib/Pod/Wrap/Pretty.pm »
1 # Copyright (C) 2005 Joshua Hoblitt
2 #
3 # $Id: Tidy.pm,v 1.27 2009/02/17 21:49:37 jhoblitt Exp $
4  
5 package Pod::Tidy;
6  
7 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use warnings FATAL => qw( all );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 20 ▶use vars qw( $VERSION $columns );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 $VERSION = '0.10';
12  
13 20 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 20 ▶use File::Basename qw( basename dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use File::Copy qw( cp );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 20 ▶use Pod::Find qw( contains_pod );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 20 ▶use Pod::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 20 ▶use Pod::Wrap::Pretty;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 20 ▶use Text::Wrap qw($columns);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # Text::Wrap's default is 76, we are using 80 to maintain compatability with
24 # Pod::Tidy <= 0.09
25 20 $columns = 80;
26  
27 20 ▶use vars qw( $BACKUP_POSTFIX);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 # used by backup_file
29 20 $BACKUP_POSTFIX = "~";
30  
31 sub tidy_files
32 15 ▶{
Subroutine tidy_files: called CC=3 SCAR=11.0
Pod documented
33 15 my %p = @_;
34  
35 15 ▶ $columns = $p{columns} if $p{columns};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"columns"} 0 15 15
36  
37 my $queue = build_pod_queue(
38 files => $p{files},
39 ignore => $p{ignore},
40 recursive => $p{recursive},
41 verbose => $p{verbose},
42 15 );
43  
44 15 ▶ return undef unless $queue;
Branch: 1 branch
BranchTrueFalseTotal
unless $queue 4 11 15
45  
46 return process_pod_queue(
47 inplace => $p{inplace},
48 nobackup => $p{nobackup},
49 11 queue => $queue,
50 );
51 }
52  
53 sub tidy_filehandle
54 2 ▶{
Subroutine tidy_filehandle: called CC=2 SCAR=6.9
Pod documented
55 2 my $input = shift;
56  
57 2 ▶ return undef unless $input;
Branch: 1 branch
BranchTrueFalseTotal
unless $input 1 1 2
58  
59 1 my $wrapper = Pod::Wrap::Pretty->new;
60 1 $wrapper->parse_from_filehandle($input);
61  
62 1 return 1;
63 }
64  
65 sub process_pod_queue
66 17 ▶{
Subroutine process_pod_queue: called CC=8 SCAR=20.9
Pod documented
67 17 my %p = @_;
68  
69 17 my $verbose = $p{verbose};
70 17 my $inplace = $p{inplace};
71 17 my $queue = $p{queue};
72 17 my $nobackup = $p{nobackup};
73  
74 17 ▶ return undef unless defined $queue;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $queue 2 15 17
75  
76 # count the number of files processed
77 15 my $processed = 0;
78 15 my $wrapper = Pod::Wrap::Pretty->new;
79  
80 15 foreach my $filename (@{ $queue }) {
81 # all files in queue should have already been checked to be readable
82 15 ▶ open(my $src, '+<', $filename) or warn "can't open file: $!" && next;
Branch: 1 branch
BranchTrueFalseTotal
open my $src, "+<", $filename or warn "can't open file: $!" && (next) 15 0 15
Condition0%0/2
"can't open file: $!" && (next)
!ll
00
MC/DC0%0/1
"can't open file: $!" && (next)
"can't open file: $!"
Truth table0%0/2
A
A= "can't open file: $!"
Aresult
00
11
83  
84 # wait for an exclusive lock in case we want to modify the file
85 15 flock($src, LOCK_EX);
86  
87 # slurp the file into memory to avoid making a tmp file
88 15 my $doc = do { local $/; <$src> };
89  
90 # wrap the doc with a file handle
91 15 my $input = IO::String->new($doc);
92  
93 # modify in place?
94 15 ▶ if ($inplace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inplace) { } 4 11 15
95 4 my $output = IO::String->new;
96 4 $wrapper->parse_from_filehandle($input, $output);
97  
98 # leave the mtime alone if we didn't change anything
99 4 ▶ next if ${$input->string_ref} eq ${$output->string_ref};
Branch: 1 branch
BranchTrueFalseTotal
if ${$input->string_ref;} eq ${$output->string_ref;} 0 4 4
100  
101 # backup existing file
102 4 ▶ unless ($nobackup) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nobackup) 2 2 4
103 2 backup_file($filename);
104 }
105  
106 # overwrite the original file
107 4 truncate($src, 0);
108 4 seek($src, 0, 0);
109 4 print $src ${$output->string_ref};
110 } else {
111 # send the output to STDOUT
112 11 $wrapper->parse_from_filehandle($input);
113 }
114  
115 # count of files actually processed
116 # note that this number will be different for 'inplace' as unmodified
117 # files will not be counted
118 15 $processed++;
119 }
120  
121 15 return $processed;
122 }
123  
124 sub build_pod_queue
125 38 ▶{
Subroutine build_pod_queue: called CC=23 SCAR=36.1
Pod documented
126 38 my %p = @_;
127  
128 # deref once
129 38 my $verbose = $p{verbose};
130 38 my $recursive = $p{recursive};
131 38 my $ignore = $p{ignore};
132  
133 38 my @queue;
134 38 PERITEM: foreach my $item (@{ $p{files} }) {
135 # FIXME do we need to add symlink handling options?
136 58 $item = File::Spec->canonpath($item);
137  
138 58 foreach my $pattern (@{ $ignore }) {
139 # try the absolute path, then the relative path, then the 'base'
140 # path
141 39 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->rel2abs($item) =~ /$pattern/ or $item =~ /$pattern/ or base($item) =~ /$pattern/) 9 30 39
Condition66%4/6
"File::Spec"->rel2abs($item) =~ /$pattern/ or $item =~ /$pattern/ or base($item) =~ /$pattern/
l!l&&r!l&&!r
9030
"File::Spec"->rel2abs($item) =~ /$pattern/ or $item =~ /$pattern/
l!l&&r!l&&!r
9030
MC/DC33%1/3
"File::Spec"->rel2abs($item) =~ /$pattern/ or $item =~ /$pattern/ or base($item) =~ /$pattern/
"File::Spec"->rel2abs($item) =~ /$pattern/ $item =~ /$pattern/ base($item) =~ /$pattern/
Truth table50%2/4
A or B or C
A= "File::Spec"->rel2abs($item) =~ /$pattern/B= $item =~ /$pattern/C= base($item) =~ /$pattern/
ABCresult
1XX1
01X1
0011
0000
142 (File::Spec->rel2abs($item) =~ $pattern)
143 or ($item =~ $pattern)
144 or (base($item) =~ $pattern)
145 ) {
146 9 ▶ warn "$0: omitting file \`$item\': matches ignore pattern: "
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 9 9
147 . "$pattern\n" if $verbose;
148 9 next PERITEM;
149 }
150 }
151
152 # is it a file?
153 49 ▶ if (-f $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $item) 31 18 49
154 # only check if we can read the file, we don't need to be able to
155 # write to it unless we're doing an inplace edit
156 31 ▶ unless (-r $item) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $item) 0 31 31
157 0 warn "$0: omitting file \`$item\': permission denied\n";
158 0 next;
159 }
160  
161 31 ▶ unless (contains_pod($item, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (contains_pod($item, 0)) 0 31 31
162 0 ▶ warn "$0: omitting file \`$item\': does not contain Pod\n"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
163 if $verbose;
164 0 next;
165 }
166  
167 31 ▶ unless (valid_pod_syntax($item, $verbose)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (valid_pod_syntax($item, $verbose)) 15 16 31
168 15 ▶ warn "$0: omitting file \`$item\': bad Pod syntax\n"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 15 15
169 if $verbose;
170 15 next;
171 }
172  
173 16 push @queue, $item;
174  
175 16 next;
176 }
177  
178 # is it a dir?
179 18 ▶ if (-d $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $item) 18 0 18
180 18 ▶ unless (-r $item and -x $item) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $item and -x $item) 0 18 18
Condition33%1/3
-r $item and -x $item
!ll&&!rl&&r
0018
MC/DC0%0/2
-r $item and -x $item
-r $item -x $item
Truth table33%1/3
A and B
A= -r $itemB= -x $item
ABresult
0X0
100
111
181 0 warn "$0: omitting file \`$item\': permission denied\n";
182 0 next;
183 }
184  
185 # is recursion allowed?
186 18 ▶ if ($recursive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursive) { } 15 3 18
187 # It may be better to use File::Find or Pod::Find here.
188 # Initialiy I was using Pod::Find but I wanted explict control
189 # over warnings.
190 15 ▶ opendir(my $dir, $item) or warn "can't open dir: $!" && next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dir, $item or warn "can't open dir: $!" && (next) 15 0 15
Condition0%0/2
"can't open dir: $!" && (next)
!ll
00
MC/DC0%0/1
"can't open dir: $!" && (next)
"can't open dir: $!"
Truth table0%0/2
A
A= "can't open dir: $!"
Aresult
00
11
191 15 my @files = grep !/^\.{1,2}$/, readdir($dir);
192 15 @files = map { "$item/$_" } @files;
193 15 my $pod_list = build_pod_queue(
194 files => \@files,
195 verbose => $verbose,
196 recursive => $recursive,
197 ignore => $ignore,
198 );
199 15 ▶ push(@queue, @{ $pod_list }) if $pod_list;
Branch: 1 branch
BranchTrueFalseTotal
if $pod_list 12 3 15
200 } else {
201 # ignoring $item, recursion not enabled
202 3 ▶ warn "$0: omitting direcotry \`$item\': recursion is not enabled\n"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 3 3
203 if $verbose;
204 }
205 18 next;
206 }
207  
208 # it must be bogus
209 0 ▶ warn "$0: \`$item\': no such file or directory\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
210 }
211  
212 38 ▶ return scalar @queue ? \@queue : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar @queue ? : 27 11 38
213 }
214  
215 sub valid_pod_syntax
216 35 ▶{
Subroutine valid_pod_syntax: called CC=5 SCAR=16.1
Pod documented
217 35 my ($filename, $verbose) = @_;
218  
219 35 ▶ return undef unless defined $filename and -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename and -e $filename 2 33 35
Condition100%3/3
defined $filename and -e $filename
!ll&&!rl&&r
1133
MC/DC100%2/2
defined $filename and -e $filename
defined $filename -e $filename
Truth table100%3/3
A and B
A= defined $filenameB= -e $filename
ABresult
0X0
100
111
220  
221 # method for checking syntax stolen from Test::Pod
222 33 my $parser = Pod::Simple->new;
223  
224 33 ▶ $parser->complain_stderr(1) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 33 33
225 33 $parser->parse_file($filename);
226  
227 33 ▶ return $parser->any_errata_seen ? undef : 1;
Branch: 1 branch
BranchTrueFalseTotal
$parser->any_errata_seen ? : 16 17 33
228 }
229  
230 sub backup_file
231 5 ▶{
Subroutine backup_file: called CC=3 SCAR=11.0
Pod documented
232 5 my $filename = shift;
233  
234 5 ▶ return undef unless defined $filename and -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename and -e $filename 2 3 5
Condition100%3/3
defined $filename and -e $filename
!ll&&!rl&&r
113
MC/DC100%2/2
defined $filename and -e $filename
defined $filename -e $filename
Truth table100%3/3
A and B
A= defined $filenameB= -e $filename
ABresult
0X0
100
111
235 3 return cp($filename, $filename . $BACKUP_POSTFIX);
236 }
237  
238 sub base
239 30 ▶{
Subroutine base: called CC=2 SCAR=7.6
Pod documented
240 30 my $path = shift;
241  
242 30 ▶ if (my $base = basename($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $base = basename($path)) { } 30 0 30
243 30 return $base;
244 } else {
245 0 return basename(dirname($path));
246 }
247 }
248  
249 20 1;
250  
251 __END__
↑ Summary lib/Pod/Wrap/Pretty.pm »