lib/PDF/PDFUnit.pm

filter: Statementstmt 46.1% 84 / 182 Branchbran 19.3% 12 / 62 Conditioncond 31.5% 6 / 19 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 82.6% 19 / 23 Podpod 0.0% 0 / 7 totaltotal 40.0% 122 / 305 CC 43 SCAR 37.7
CC
43
Cov
39%
load_config
112.0
build_classpath
47.2
attach_java
30.0
CRAP 43.4
↑ Summary script/pdfunit-perl.pl »
1 #####################
2 package PDF::PDFUnit;
3 #####################
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 @PDF::PDFUnit::EXPORT = qw( DEBUG );
15 }
16  
17 # These two extend the @EXPORT list:
18 4 ▶use PDF::PDFUnit::Shortcuts;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use PDF::PDFUnit::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 ▶use PDF::PDFUnit::StudyClasses;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 require Exporter;
24 4 our @ISA = qw(Exporter);
25  
26  
27 4 $PDF::PDFUnit::VERSION = 0.17;
28  
29 4 $PDF::PDFUnit::PDFUNIT_JAVA_VERSION = "2016.05";
30  
31  
32  
33  
34 4 our $instance = {
35 is_loaded => 0,
36 is_sane => 0,
37 config_path => undef,
38 classpath_elements => [],
39 };
40  
41  
42 0 ▶sub val {
Subroutine val: not called CC=2 SCAR=17.9
Pod undocumented
43 0 my ($class, $key);
44  
45
46 0 ▶ if ($_[0] eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq __PACKAGE__) { } 0 0 0
47 0 ($class, $key) = @_;
48 }
49 else {
50 0 ($key) = @_;
51 }
52  
53 0 return $instance->{$key};
54 }
55  
56  
57 ###########
58 20 ▶sub DEBUG {
Subroutine DEBUG: called CC=5 SCAR=18.6
Pod undocumented
59 ###########
60 20 ▶ my $debug_level = $ENV{PDFUNIT_PERL_DEBUG} // 0;
Condition50%1/2
$ENV{"PDFUNIT_PERL_DEBUG"} // 0
l!l
020
MC/DC0%0/1
$ENV{"PDFUNIT_PERL_DEBUG"} // 0
$ENV{"PDFUNIT_PERL_DEBUG"}
Truth table50%1/2
A
A= $ENV{"PDFUNIT_PERL_DEBUG"}
Aresult
11
00
61  
62 20 my ($msg, $level) = @_;
63 20 ▶ $level //= 1;
Condition100%2/2
$level //= 1
l!l
164
MC/DC100%1/1
$level //= 1
$level
Truth table100%2/2
A
A= $level
Aresult
11
00
64 20 ▶ return unless $debug_level >= $level;
Branch: 1 branch
BranchTrueFalseTotal
unless $debug_level >= $level 20 0 20
65  
66 0 my $prefix = "***[${level}] ";
67  
68 0 my @msg = split(/\n/, $msg);
69
70 0 say STDERR $prefix, $_ foreach @msg;
71 }
72  
73  
74 4 ▶sub import {
Subroutine import: called CC=7 SCAR=28.7
75 4 my ($package, $init_style) = @_;
76  
77 4 ▶ if (defined $init_style) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $init_style) 4 0 4
78  
79 4 ▶ unless (grep {$init_style eq $_} qw(:skip_on_error :noinit)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$init_style eq $_;} ":skip_on_error", ":noinit") 0 4 4
80 0 carp "Unknown import tag: '$init_style'";
81 }
82 }
83  
84 4 ▶ if (defined $init_style && $init_style eq ':skip_on_error') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $init_style and $init_style eq ":skip_on_error") 4 0 4
Condition33%1/3
defined $init_style and $init_style eq ":skip_on_error"
!ll&&!rl&&r
004
MC/DC0%0/2
defined $init_style and $init_style eq ":skip_on_error"
defined $init_style $init_style eq ":skip_on_error"
Truth table33%1/3
A and B
A= defined $init_styleB= $init_style eq ":skip_on_error"
ABresult
0X0
100
111
85 4 $package->init(skip_on_error => 1);
86 }
87
88 0 ▶ unless (defined $init_style && $init_style eq ':noinit') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $init_style and $init_style eq ":noinit") 0 0 0
Condition0%0/3
defined $init_style and $init_style eq ":noinit"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $init_style and $init_style eq ":noinit"
defined $init_style $init_style eq ":noinit"
Truth table0%0/3
A and B
A= defined $init_styleB= $init_style eq ":noinit"
ABresult
0X0
100
111
89 0 $package->init();
90 }
91  
92 0 @_ = ($package);
93  
94 0 goto &Exporter::import;
95 }
96  
97  
98 ##########
99 4 ▶sub init {
Subroutine init: called CC=3 SCAR=18.4
Pod undocumented
100 ##########
101 4 my $class = shift;
102  
103 4 ▶ $class->load_config(@_) &&
Branch: 1 branch
BranchTrueFalseTotal
$class->load_config(@_) and $class->build_classpath(@_) and $class->attach_java(@_) 0 0 0
Condition0%0/3
$class->load_config(@_) and $class->build_classpath(@_)
!ll&&!rl&&r
000
MC/DC0%0/2
$class->load_config(@_) and $class->build_classpath(@_)
$class->load_config(@_) $class->build_classpath(@_)
Truth table0%0/3
A and B
A= $class->load_config(@_)B= $class->build_classpath(@_)
ABresult
0X0
100
111
104 $class->build_classpath(@_) &&
105 $class->attach_java(@_);
106 }
107
108  
109  
110  
111 #################
112 4 ▶sub load_config {
Subroutine load_config: called CC=19 SCAR=47.2
Pod undocumented
113 #################
114 4 my $class = shift;
115 4 my %args = @_;
116  
117 4 ▶ return 1 if $instance->{is_loaded};
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::PDFUnit::instance->{"is_loaded"} 0 4 4
118
119 4 DEBUG "Operating system: $OSNAME";
120  
121 4 my $config_found;
122
123 4 foreach (@{os_dep()->{cfg_locations}}) {
124  
125 16 my $location_as_text = $_;
126 16 my $location = $_;
127  
128
129 16 ▶ if ( my ($envar) = m/ENV%(.*?)%/ ) { # Environment var
Branch: 1 branch
BranchTrueFalseTotal
if (my($envar) = /ENV%(.*?)%/) 8 8 16
130  
131 8 my $env_prefix = $class->os_dep()->{env_prefix};
132 8 my $env_suffix = $class->os_dep()->{env_suffix};
133
134 8 $location_as_text =~ s/ENV%(.*?)%/$env_prefix$envar$env_suffix/;
135  
136 8 ▶ if (exists $ENV{$envar}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{$envar}) { } 4 4 8
137 4 $location =~ s/ENV%(.*?)%/$ENV{$envar}/;
138 }
139 else {
140 4 $location = undef;
141 }
142 }
143  
144 16 DEBUG "Possible config location: $location_as_text", 2;
145  
146 16 ▶ if (defined $location && -r $location) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $location and -r $location) 0 16 16
Condition66%2/3
defined $location and -r $location
!ll&&!rl&&r
4120
MC/DC0%0/2
defined $location and -r $location
defined $location -r $location
Truth table66%2/3
A and B
A= defined $locationB= -r $location
ABresult
0X0
100
111
147 0 $config_found = $location;
148 0 DEBUG "Found readable config: $location";
149 0 last;
150 }
151 }
152  
153 4 ▶ unless ($config_found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config_found) 4 0 4
154 4 my $msg = "No configuration found";
155
156 4 ▶ if ($args{skip_on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip_on_error"}) 4 0 4
157 4 _skip_all_tests($msg);
158 }
159
160 0 warn "$msg\n";
161 0 return 0;
162 }
163  
164 0 $instance->{config_path} = $config_found;
165  
166 0 ▶ open(my $cfghandle, "<", $config_found)
Branch: 1 branch
BranchTrueFalseTotal
open my $cfghandle, "<", $config_found or die "${config_found}: $!\n" 0 0 0
167 || die "$config_found: $!\n";
168 0 DEBUG "Config opened for reading", 2;
169  
170  
171 # Valid configuration options:
172 0 my %config_schema = (
173 pdfunit_java_home => 'mandatory',
174 pdfunit_root => 'deprecated;pdfunit_java_home',
175 outfox_display => 'optional',
176 );
177  
178 0 my %config = (); # This will be filled
179  
180 0 ▶ while (<$cfghandle>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $cfghandle)) 0 0 0
181 0 chomp;
182 0 ▶ next if /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ 0 0 0
183 0 ▶ next if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 0 0
184  
185 0 my ($key, $value) = split(/\s*=\s*/, $_, 2);
186
187 0 ▶ if (!exists $config_schema{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $config_schema{$key}) { } 0 0 0
188 0 warn("$config_found: Invalid key: $key\n");
189 }
190 else {
191 0 DEBUG "Found key/value: $key = $value";
192
193 0 ▶ if ($config_schema{$key} =~ /^deprecated;/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config_schema{$key} =~ /^deprecated;/) { } 0 0 0
194 0 my ($new_name) = (split(/;/, $config_schema{$key}))[1];
195 0 warn "Deprecated configuration key: $key "
196 . "(use $new_name instead)\n";
197 0 $config{$new_name} = $value;
198 }
199 else {
200 0 $config{$key} = $value;
201 }
202 }
203  
204 }
205 0 close $cfghandle;
206
207 # Do we have values for all mandatory config keys?
208 0 foreach (grep { $config_schema{$_} eq 'mandatory'} keys %config_schema) {
209 0 ▶ unless (exists $config{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $config{$_}) 0 0 0
210 0 my $msg = "Missing mandatory key in configuration: $_";
211
212 0 ▶ if ($args{skip_on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip_on_error"}) 0 0 0
213 0 _skip_all_tests($msg);
214 }
215
216 0 die "$msg\n";
217 }
218 }
219  
220 # Now put all found keys into the instance:
221 0 foreach (keys %config) {
222 0 $instance->{$_} = $config{$_};
223 }
224  
225
226  
227 0 $instance->{is_loaded} = 1;
228  
229 0 return 1;
230 }
231  
232  
233  
234  
235 #####################
236 0 ▶sub build_classpath {
Subroutine build_classpath: not called CC=7 SCAR=38.5
Pod undocumented
237 #####################
238 0 my $class = shift;
239 0 my %args = @_;
240
241 0 ▶ return 1 if $instance->{is_sane};
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::PDFUnit::instance->{"is_sane"} 0 0 0
242  
243 0 DEBUG "Building CLASSPATH.";
244  
245 0 ▶ my $pdfunit_java_home = glob($class->val('pdfunit_java_home'));
Subroutine BEGIN: called CC=1 SCAR=0.2
246  
247 0 DEBUG "Searching for jar files under $pdfunit_java_home";
248  
249 0 ▶ unless (-d $pdfunit_java_home) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $pdfunit_java_home) 0 0 0
250 0 my $msg = "Configured pdfunit_java_home is not a directory";
251
252 0 ▶ if ($args{skip_on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip_on_error"}) 0 0 0
253 0 _skip_all_tests($msg);
254 }
255  
256 0 die "$msg\n";
257 }
258
259 0 find(\&_wanted, $pdfunit_java_home);
260  
261 # The root directory of PDFUnit has to be in the classpath
262 # (otherwise the license file cannot be read):
263 0 unshift @{$instance->{classpath_elements}}, $pdfunit_java_home;
264  
265 0 my $count = @{$instance->{classpath_elements}};
266 0 DEBUG "Detected $count elements for classpath.";
267  
268 $ENV{CLASSPATH} = join(os_dep()->{env_pathlist_sep_char},
269 0 @{$instance->{classpath_elements}});
270  
271
272 # (Weak) consisteny check of classpath elements:
273
274 my @contains_main_jar =
275 0 grep { /^pdfunit-java-.*\.jar$/ }
276 0 map { basename $_ } @{$instance->{classpath_elements}};
277  
278
279 0 ▶ if ( @contains_main_jar && $count >= 20) {
Branch: 1 branch
BranchTrueFalseTotal
if (@contains_main_jar and $count >= 20) { } 0 0 0
Condition0%0/3
@contains_main_jar and $count >= 20
!ll&&!rl&&r
000
MC/DC0%0/2
@contains_main_jar and $count >= 20
@contains_main_jar $count >= 20
Truth table0%0/3
A and B
A= @contains_main_jarB= $count >= 20
ABresult
0X0
100
111
280 0 DEBUG "Setup seems to be sane!";
281
282 0 $instance->{is_sane} = 1;
283  
284 0 return 1;
285 }
286 else {
287 0 my $msg = "Cannot build a sane CLASSPATH - no suitable jars found in "
288 . $pdfunit_java_home;
289
290 0 ▶ if ($args{skip_on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip_on_error"}) 0 0 0
291 0 _skip_all_tests($msg);
292 }
293  
294 0 die "$msg\n";
295 }
296 }
297  
298  
299 0 ▶sub _wanted {
Subroutine _wanted: not called CC=2 SCAR=17.9
300 0 ▶ return unless /\.jar$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless /\.jar$/i 0 0 0
301  
302 0 DEBUG "Found jar: $_", 2;
303 0 push @{$instance->{classpath_elements}}, $File::Find::name;
304 }
305  
306  
307 #################
308 0 ▶sub attach_java {
Subroutine attach_java: not called CC=5 SCAR=34.0
Pod undocumented
309 #################
310 0 my $class = shift;
311 0 my %args = @_;
312  
313 0 ▶ return 0 unless $instance->{is_sane};
Branch: 1 branch
BranchTrueFalseTotal
unless $PDF::PDFUnit::instance->{"is_sane"} 0 0 0
314  
315
316 0 ▶ $ENV{DISPLAY} = $class->val('outfox_display')
Branch: 1 branch
BranchTrueFalseTotal
if defined $class->val("outfox_display") 0 0 0
317 if defined $class->val('outfox_display');
318  
319  
320 0 DEBUG("Attaching Java with Inline::Java");
321
322 0 eval {
323 0 require Inline;
324 0 import Inline (
325 Java => 'STUDY',
326 STUDY => $PDF::PDFUnit::StudyClasses::java_classes,
327 PACKAGE => 'main',
328 AUTOSTUDY => 1,
329 );
330 };
331  
332 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
333 0 ▶ if ($args{skip_on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip_on_error"}) 0 0 0
334 0 _skip_all_tests($@);
335 }
336  
337 0 warn $@;
338 0 return 0;
339 }
340  
341 0 return 1;
342 }
343  
344  
345 4 ▶sub _skip_all_tests {
Subroutine _skip_all_tests: called CC=1 SCAR=0.2
346 4 my ($msg) = @_;
347
348 4 require Test::More;
349 4 import Test::More (
350 skip_all => ($msg)
351 );
352  
353 0 exit 1;
354 }
355  
356  
357  
358 ############
359 20 ▶sub os_dep {
Subroutine os_dep: called CC=1 SCAR=0.0
Pod undocumented
360 ############
361 20 my $class = shift;
362  
363 20 my $cfgname = 'pdfunit-perl.cfg';
364  
365 20 my $os_dep = {
366 linux => {
367 env_pathlist_sep_char => ':',
368  
369 env_prefix => '$',
370 env_suffix => '',
371
372 cfg_locations => [
373 "ENV%PDFUNIT_PERL_CONFIG%",
374 "./$cfgname",
375 "ENV%HOME%/.$cfgname",
376 "/etc/$cfgname"
377 ],
378 },
379
380 MSWin32 => {
381 env_pathlist_sep_char => ';',
382
383 env_prefix => '%',
384 env_suffix => '%',
385  
386 cfg_locations => [
387 "ENV%PDFUNIT_PERL_CONFIG%",
388 ".\\$cfgname",
389 "ENV%HOMEPATH%\\.$cfgname",
390 "ENV%USERPROFILE%\\.$cfgname",
391 "ENV%LOCALAPPDATA%\\pdfunit-perl\\$cfgname",
392 ],
393 },
394 };
395  
396 20 return $os_dep->{$OSNAME};
397 }
398  
399  
400  
401  
402  
403  
404  
405 4 1;
406  
407  
408 __END__
↑ Summary script/pdfunit-perl.pl »