blib/lib/TAP/Parser/Source.pm

filter: Statementstmt 53.8% 105 / 195 Branchbran 41.4% 63 / 152 Conditioncond 33.3% 6 / 18 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 58.6% 17 / 29 Podpod 100% 20 / 20 totaltotal 50.2% 214 / 426 CC 83 SCAR 46.2
CC
83
Cov
48%
assemble_meta
185.6
assemble_meta
185.6
_read_shebang
8.7
CRAP 101.9
↑ Summary blib/lib/TAP/Harness/Env.pm »
1 package TAP::Parser::Source;
2  
3 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 38 ▶use File::Basename qw( fileparse );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 38 ▶use base 'TAP::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 38 ▶use constant BLK_SIZE => 512;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 TAP::Parser::Source - a TAP source & meta data about it
14  
15 =head1 VERSION
16  
17 Version 3.52
18  
19 =cut
20  
21 38 our $VERSION = '3.52';
22  
23 =head1 SYNOPSIS
24  
25 use TAP::Parser::Source;
26 my $source = TAP::Parser::Source->new;
27 $source->raw( \'reference to raw TAP source' )
28 ->config( \%config )
29 ->merge( $boolean )
30 ->switches( \@switches )
31 ->test_args( \@args )
32 ->assemble_meta;
33  
34 do { ... } if $source->meta->{is_file};
35 # see assemble_meta for a full list of data available
36  
37 =head1 DESCRIPTION
38  
39 A TAP I<source> is something that produces a stream of TAP for the parser to
40 consume, such as an executable file, a text file, an archive, an IO handle, a
41 database, etc. C<TAP::Parser::Source>s encapsulate these I<raw> sources, and
42 provide some useful meta data about them. They are used by
43 L<TAP::Parser::SourceHandler>s, which do whatever is required to produce &
44 capture a stream of TAP from the I<raw> source, and package it up in a
45 L<TAP::Parser::Iterator> for the parser to consume.
46  
47 Unless you're writing a new L<TAP::Parser::SourceHandler>, a plugin or
48 subclassing L<TAP::Parser>, you probably won't need to use this module directly.
49  
50 =head1 METHODS
51  
52 =head2 Class Methods
53  
54 =head3 C<new>
55  
56 my $source = TAP::Parser::Source->new;
57  
58 Returns a new C<TAP::Parser::Source> object.
59  
60 =cut
61  
62 # new() implementation supplied by TAP::Object
63  
64 362 ▶sub _initialize {
Subroutine _initialize: called CC=1 SCAR=1.2
65 0 ▶ my ($self) = @_;
Subroutine _initialize: not called CC=1 SCAR=1.2
66 0 $self->meta( {} );
67 0 $self->config( {} );
68 0 return $self;
69 }
70  
71 ##############################################################################
72  
73 =head2 Instance Methods
74  
75 =head3 C<raw>
76  
77 my $raw = $source->raw;
78 $source->raw( $some_value );
79  
80 Chaining getter/setter for the raw TAP source. This is a reference, as it may
81 contain large amounts of data (eg: raw TAP).
82  
83 =head3 C<meta>
84  
85 my $meta = $source->meta;
86 $source->meta({ %some_value });
87  
88 Chaining getter/setter for meta data about the source. This defaults to an
89 empty hashref. See L</assemble_meta> for more info.
90  
91 =head3 C<has_meta>
92  
93 True if the source has meta data.
94  
95 =head3 C<config>
96  
97 my $config = $source->config;
98 $source->config({ %some_value });
99  
100 Chaining getter/setter for the source's configuration, if any has been provided
101 by the user. How it's used is up to you. This defaults to an empty hashref.
102 See L</config_for> for more info.
103  
104 =head3 C<merge>
105  
106 my $merge = $source->merge;
107 $source->config( $bool );
108  
109 Chaining getter/setter for the flag that dictates whether STDOUT and STDERR
110 should be merged (where appropriate). Defaults to undef.
111  
112 =head3 C<switches>
113  
114 my $switches = $source->switches;
115 $source->config([ @switches ]);
116  
117 Chaining getter/setter for the list of command-line switches that should be
118 passed to the source (where appropriate). Defaults to undef.
119  
120 =head3 C<test_args>
121  
122 my $test_args = $source->test_args;
123 $source->config([ @test_args ]);
124  
125 Chaining getter/setter for the list of command-line arguments that should be
126 passed to the source (where appropriate). Defaults to undef.
127  
128 =cut
129  
130 2375 ▶sub raw {
Subroutine raw: called CC=2 SCAR=9.2
Pod documented
131 0 ▶ my $self = shift;
Subroutine raw: not called CC=2 SCAR=9.2
Pod documented
132 0 ▶ return $self->{raw} unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 2045 330 2375
unless @_ 0 0 0
133 0 $self->{raw} = shift;
134 0 return $self;
135 }
136  
137 3400 ▶sub meta {
Subroutine meta: called CC=2 SCAR=9.2
Pod documented
138 0 ▶ my $self = shift;
Subroutine meta: not called CC=2 SCAR=9.2
Pod documented
139 0 ▶ return $self->{meta} unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 3022 378 3400
unless @_ 0 0 0
140 0 $self->{meta} = shift;
141 0 return $self;
142 }
143  
144 326 ▶sub has_meta {
Subroutine has_meta: called CC=2 SCAR=10.9
Pod documented
145 0 ▶ return scalar %{ shift->meta } ? 1 : 0;
Subroutine has_meta: not called CC=2 SCAR=10.9
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
scalar %{(shift())->meta;} ? : 0 326 326
scalar %{(shift())->meta;} ? : 0 0 0
146 }
147  
148 1311 ▶sub config {
Subroutine config: called CC=2 SCAR=9.2
Pod documented
149 0 ▶ my $self = shift;
Subroutine config: not called CC=2 SCAR=9.2
Pod documented
150 0 ▶ return $self->{config} unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 643 668 1311
unless @_ 0 0 0
151 0 $self->{config} = shift;
152 0 return $self;
153 }
154  
155 515 ▶sub merge {
Subroutine merge: called CC=2 SCAR=9.2
Pod documented
156 0 ▶ my $self = shift;
Subroutine merge: not called CC=2 SCAR=9.2
Pod documented
157 0 ▶ return $self->{merge} unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 224 291 515
unless @_ 0 0 0
158 0 $self->{merge} = shift;
159 0 return $self;
160 }
161  
162 502 ▶sub switches {
Subroutine switches: called CC=2 SCAR=9.2
Pod documented
163 0 ▶ my $self = shift;
Subroutine switches: not called CC=2 SCAR=9.2
Pod documented
164 0 ▶ return $self->{switches} unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 211 291 502
unless @_ 0 0 0
165 0 $self->{switches} = shift;
166 0 return $self;
167 }
168  
169 516 ▶sub test_args {
Subroutine test_args: called CC=2 SCAR=9.2
Pod documented
170 0 ▶ my $self = shift;
Subroutine test_args: not called CC=2 SCAR=9.2
Pod documented
171 0 ▶ return $self->{test_args} unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 224 292 516
unless @_ 0 0 0
172 0 $self->{test_args} = shift;
173 0 return $self;
174 }
175  
176 =head3 C<assemble_meta>
177  
178 my $meta = $source->assemble_meta;
179  
180 Gathers meta data about the L</raw> source, stashes it in L</meta> and returns
181 it as a hashref. This is done so that the L<TAP::Parser::SourceHandler>s don't
182 have to repeat common checks. Currently this includes:
183  
184 is_scalar => $bool,
185 is_hash => $bool,
186 is_array => $bool,
187  
188 # for scalars:
189 length => $n
190 has_newlines => $bool
191  
192 # only done if the scalar looks like a filename
193 is_file => $bool,
194 is_dir => $bool,
195 is_symlink => $bool,
196 file => {
197 # only done if the scalar looks like a filename
198 basename => $string, # including ext
199 dir => $string,
200 ext => $string,
201 lc_ext => $string,
202 # system checks
203 exists => $bool,
204 stat => [ ... ], # perldoc -f stat
205 empty => $bool,
206 size => $n,
207 text => $bool,
208 binary => $bool,
209 read => $bool,
210 write => $bool,
211 execute => $bool,
212 setuid => $bool,
213 setgid => $bool,
214 sticky => $bool,
215 is_file => $bool,
216 is_dir => $bool,
217 is_symlink => $bool,
218 # only done if the file's a symlink
219 lstat => [ ... ], # perldoc -f lstat
220 # only done if the file's a readable text file
221 shebang => $first_line,
222 }
223  
224 # for arrays:
225 size => $n,
226  
227 =cut
228  
229 326 ▶sub assemble_meta {
Subroutine assemble_meta: called CC=30 SCAR=52.2
Pod documented
230 0 ▶ my ($self) = @_;
Subroutine assemble_meta: not called CC=30 SCAR=52.2
Pod documented
231  
232 0 ▶ return $self->meta if $self->has_meta;
Branch: 2 branches
BranchTrueFalseTotal
if $self->has_meta 0 326 326
if $self->has_meta 0 0 0
233  
234 0 my $meta = $self->meta;
235 0 my $raw = $self->raw;
236  
237 # rudimentary is object test - if it's blessed it'll
238 # inherit from UNIVERSAL
239 0 ▶ $meta->{is_object} = UNIVERSAL::isa( $raw, 'UNIVERSAL' ) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
UNIVERSAL::isa($raw, 'UNIVERSAL') ? : 4 322 326
UNIVERSAL::isa($raw, 'UNIVERSAL') ? : 0 0 0
240  
241 0 ▶ if ( $meta->{is_object} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($meta->{'is_object'}) { } 4 322 326
if ($meta->{'is_object'}) { } 0 0 0
242 0 $meta->{class} = ref($raw);
243 }
244 else {
245 0 my $ref = lc( ref($raw) );
246 0 $meta->{"is_$ref"} = 1;
247 }
248  
249 0 ▶ if ( $meta->{is_scalar} ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($meta->{'is_scalar'}) { } 296 30 326
elsif ($meta->{'is_array'}) { } 7 23 30
elsif ($meta->{'is_hash'}) { } 13 10 23
if ($meta->{'is_scalar'}) { } 0 0 0
elsif ($meta->{'is_array'}) { } 0 0 0
elsif ($meta->{'is_hash'}) { } 0 0 0
250 0 my $source = $$raw;
251 0 $meta->{length} = length($$raw);
252 0 ▶ $meta->{has_newlines} = $$raw =~ /\n/ ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$$raw =~ /\n/ ? : 66 230 296
$$raw =~ /\n/ ? : 0 0 0
253  
254 # only do file checks if it looks like a filename
255 0 ▶ if ( !$meta->{has_newlines} and $meta->{length} < 1024 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $meta->{"has_newlines"} and $meta->{"length"} < 1024) 230 66 296
if (not $meta->{"has_newlines"} and $meta->{"length"} < 1024) 0 0 0
Condition33%2/6
not $meta->{"has_newlines"} and $meta->{"length"} < 1024
!ll&&!rl&&r
660230
not $meta->{"has_newlines"} and $meta->{"length"} < 1024
!ll&&!rl&&r
000
MC/DC50%1/2
not $meta->{"has_newlines"} and $meta->{"length"} < 1024
not $meta->{"has_newlines"} $meta->{"length"} < 1024
MC/DC0%0/2
not $meta->{"has_newlines"} and $meta->{"length"} < 1024
not $meta->{"has_newlines"} $meta->{"length"} < 1024
Truth table66%2/3
not(A) and B
A= not $meta->{"has_newlines"}B= $meta->{"length"} < 1024
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not $meta->{"has_newlines"}B= $meta->{"length"} < 1024
ABresult
0X0
100
111
256 0 my $file = {};
257 0 ▶ $file->{exists} = -e $source ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
-e $source ? : 226 4 230
-e $source ? : 0 0 0
258 0 ▶ if ( $file->{exists} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file->{"exists"}) 226 4 230
if ($file->{"exists"}) 0 0 0
259 0 $meta->{file} = $file;
260  
261 # avoid extra system calls (see `perldoc -f -X`)
262 0 $file->{stat} = [ stat(_) ];
263 0 ▶ $file->{empty} = -z _ ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
-z _ ? : 0 226 226
-z _ ? : 0 0 0
264 0 $file->{size} = -s _;
265 0 ▶ $file->{text} = -T _ ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
-T _ ? : 225 1 226
-T _ ? : 0 0 0
266 0 ▶ $file->{binary} = -B _ ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
-B _ ? : 1 225 226
-B _ ? : 0 0 0
267 0 ▶ $file->{read} = -r _ ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
-r _ ? : 226 0 226
-r _ ? : 0 0 0
268 0 ▶ $file->{write} = -w _ ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
-w _ ? : 226 0 226
-w _ ? : 0 0 0
269 0 ▶ $file->{execute} = -x _ ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
-x _ ? : 5 221 226
-x _ ? : 0 0 0
270 0 ▶ $file->{setuid} = -u _ ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
-u _ ? : 0 226 226
-u _ ? : 0 0 0
271 0 ▶ $file->{setgid} = -g _ ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
-g _ ? : 0 226 226
-g _ ? : 0 0 0
272 0 ▶ $file->{sticky} = -k _ ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
-k _ ? : 0 226 226
-k _ ? : 0 0 0
273  
274 0 ▶ $meta->{is_file} = $file->{is_file} = -f _ ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
-f _ ? : 225 1 226
-f _ ? : 0 0 0
275 0 ▶ $meta->{is_dir} = $file->{is_dir} = -d _ ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
-d _ ? : 1 225 226
-d _ ? : 0 0 0
276  
277 # symlink check requires another system call
278 $meta->{is_symlink} = $file->{is_symlink}
279 0 ▶ = -l $source ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
-l $source ? : 1 225 226
-l $source ? : 0 0 0
280 0 ▶ if ( $file->{is_symlink} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file->{"is_symlink"}) 1 225 226
if ($file->{"is_symlink"}) 0 0 0
281 0 $file->{lstat} = [ lstat(_) ];
282 }
283  
284 # put together some common info about the file
285 ( $file->{basename}, $file->{dir}, $file->{ext} )
286 0 ▶ = map { defined $_ ? $_ : '' }
Branch: 2 branches
BranchTrueFalseTotal
defined $_ ? : 678 0 678
defined $_ ? : 0 0 0
287 fileparse( $source, qr/\.[^.]*/ );
288 0 $file->{lc_ext} = lc( $file->{ext} );
289 0 ▶ $file->{basename} .= $file->{ext} if $file->{ext};
Branch: 2 branches
BranchTrueFalseTotal
if $file->{"ext"} 20 206 226
if $file->{"ext"} 0 0 0
290  
291 0 ▶ if ( !$file->{is_dir} && $file->{read} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $file->{"is_dir"} and $file->{"read"}) 225 1 226
if (not $file->{"is_dir"} and $file->{"read"}) 0 0 0
Condition33%2/6
not $file->{"is_dir"} and $file->{"read"}
!ll&&!rl&&r
10225
not $file->{"is_dir"} and $file->{"read"}
!ll&&!rl&&r
000
MC/DC50%1/2
not $file->{"is_dir"} and $file->{"read"}
not $file->{"is_dir"} $file->{"read"}
MC/DC0%0/2
not $file->{"is_dir"} and $file->{"read"}
not $file->{"is_dir"} $file->{"read"}
Truth table66%2/3
not(A) and B
A= not $file->{"is_dir"}B= $file->{"read"}
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not $file->{"is_dir"}B= $file->{"read"}
ABresult
0X0
100
111
292 0 eval { $file->{shebang} = $self->shebang($$raw); };
293 0 ▶ if ( my $e = $@ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $e = $@) 0 225 225
if (my $e = $@) 0 0 0
294 0 warn $e;
295 }
296 }
297 }
298 }
299 }
300 elsif ( $meta->{is_array} ) {
301 0 $meta->{size} = $#$raw + 1;
302 }
303 elsif ( $meta->{is_hash} ) {
304 ; # do nothing
305 }
306  
307 0 return $meta;
308 }
309  
310 =head3 C<shebang>
311  
312 Get the shebang line for a script file.
313  
314 my $shebang = TAP::Parser::Source->shebang( $some_script );
315  
316 May be called as a class method
317  
318 =cut
319  
320 {
321  
322 # Global shebang cache.
323 38 my %shebang_for;
324  
325 116 ▶ sub _read_shebang {
Subroutine _read_shebang: called CC=4 SCAR=21.7
326 0 ▶ my ( $class, $file ) = @_;
Subroutine _read_shebang: not called CC=4 SCAR=21.7
327 0 ▶ open my $fh, '<', $file or die "Can't read $file: $!\n";
Branch: 2 branches
BranchTrueFalseTotal
open my $fh, "<", $file or die "Can't read ${file}: $!\n" 116 0 116
open my $fh, "<", $file or die "Can't read ${file}: $!\n" 0 0 0
328  
329 # Might be a binary file - so read a fixed number of bytes.
330 0 my $got = read $fh, my ($buf), BLK_SIZE;
331 0 ▶ defined $got or die "I/O error: $!\n";
Branch: 2 branches
BranchTrueFalseTotal
defined $got or die "I/O error: $!\n" 116 0 116
defined $got or die "I/O error: $!\n" 0 0 0
332 0 ▶ return $1 if $buf =~ /(.*)/;
Branch: 2 branches
BranchTrueFalseTotal
if $buf =~ /(.*)/ 116 0 116
if $buf =~ /(.*)/ 0 0 0
333 0 return;
334 }
335  
336 225 ▶ sub shebang {
Subroutine shebang: called CC=2 SCAR=9.2
Pod documented
337 0 ▶ my ( $class, $file ) = @_;
Subroutine shebang: not called CC=2 SCAR=9.2
Pod documented
338 $shebang_for{$file} = $class->_read_shebang($file)
339 0 ▶ unless exists $shebang_for{$file};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $shebang_for{$file} 116 109 225
unless exists $shebang_for{$file} 0 0 0
340 0 return $shebang_for{$file};
341 }
342 }
343  
344 =head3 C<config_for>
345  
346 my $config = $source->config_for( $class );
347  
348 Returns L</config> for the $class given. Class names may be fully qualified
349 or abbreviated, eg:
350  
351 # these are equivalent
352 $source->config_for( 'Perl' );
353 $source->config_for( 'TAP::Parser::SourceHandler::Perl' );
354  
355 If a fully qualified $class is given, its abbreviated version is checked first.
356  
357 =cut
358  
359 38 ▶sub config_for {
Subroutine config_for: called CC=2 SCAR=9.6
Pod documented
360 0 ▶ my ( $self, $class ) = @_;
Subroutine config_for: not called CC=2 SCAR=10.1
Pod documented
361 0 my ($abbrv_class) = ( $class =~ /(?:\:\:)?(\w+)$/ );
362 0 ▶ my $config = $self->config->{$abbrv_class} || $self->config->{$class};
Condition33%2/6
$self->config->{$abbrv_class} || $self->config->{$class}
l!l&&r!l&&!r
90316
$self->config->{$abbrv_class} || $self->config->{$class}
l!l&&r!l&&!r
000
MC/DC50%1/2
$self->config->{$abbrv_class} || $self->config->{$class}
$self->config->{$abbrv_class} $self->config->{$class}
MC/DC0%0/2
$self->config->{$abbrv_class} || $self->config->{$class}
$self->config->{$abbrv_class} $self->config->{$class}
Truth table66%2/3
A || B
A= $self->config->{$abbrv_class}B= $self->config->{$class}
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $self->config->{$abbrv_class}B= $self->config->{$class}
ABresult
1X1
011
000
363 0 return $config;
364 }
365  
366 38 1;
367  
368 __END__
↑ Summary blib/lib/TAP/Harness/Env.pm »