script/pdfunit-perl.pl untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 136 SCAR 17.9
CC
2
Cov
0%
check_result
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 6.0
1 #!/usr/local/bin/perl
2 use warnings;
3 use strict;
4 use feature ':5.10';
5 use Getopt::Long;
6 use Pod::Usage;
7 use File::Basename;
8 use File::Temp qw(tempfile);
9 use Data::Dumper;
10 use MIME::Base64;
11  
12 use PDF::PDFUnit qw(:noinit);
13 use PDF::PDFUnit::Shortcuts;
14  
15  
16 my $progname = basename($0);
17 my %opts;
18  
19 GetOptions(\%opts,
20 "help",
21 "debug+",
22 "check",
23 ) || pod2usage(-verbose => 1);
24  
25 $ENV{PDFUNIT_PERL_DEBUG} = $opts{debug} if exists $opts{debug};
26 $ENV{PDFUNIT_PERL_DEBUG} //= 0;
27  
28  
29 DEBUG "$progname started.";
30 DEBUG "Debug level: $ENV{PDFUNIT_PERL_DEBUG}";
31  
32  
33 if ($opts{help} || (keys %opts) == 0) {
34  
35 pod2usage(exitval => 0);
36 }
37  
38  
39 if ($opts{check}) {
40 DEBUG "Check/verify of installation requested.";
41  
42 say "PDF::PDFUnit version = $PDF::PDFUnit::VERSION";
43  
44
45 PDF::PDFUnit->load_config();
46  
47  
48 if ($PDF::PDFUnit::instance->{is_loaded}) {
49 say "Config file = ", $PDF::PDFUnit::instance->{config_path};
50 }
51 else {
52 check_result(0);
53 }
54  
55  
56 PDF::PDFUnit->build_classpath() || check_result(0);
57  
58 PDF::PDFUnit->attach_java() || check_result(0);
59  
60  
61 DEBUG Dumper($PDF::PDFUnit::instance), 3;
62  
63  
64  
65 my $pdfunit_java_version;
66 my $compatible_versions;
67  
68 eval {
69 DEBUG("Trying to get version info from the Java lib");
70 $pdfunit_java_version = VersionInfo->getCurrentVersion();
71  
72 # This is an Inline::Java::Array object:
73 $compatible_versions = VersionInfo->getCompatibleVersions();
74 };
75 if ($@) {
76 die "Cannot get version information out of "
77 . "your installed PDFUnit Java library.\n";
78 }
79  
80
81 print "PDFUnit Java version = $pdfunit_java_version";
82  
83 if (@$compatible_versions >= 2) {
84 say " (compatible: "
85 . join(', ', @$compatible_versions)
86 . ")";
87 }
88 else {
89 print "\n";
90 }
91  
92 if (! grep { $PDF::PDFUnit::PDFUNIT_JAVA_VERSION eq $_ }
93 @$compatible_versions) {
94 die("\nThis version of PDF::PDFUnit only works with"
95 . " $PDF::PDFUnit::PDFUNIT_JAVA_VERSION"
96 . " (and compatible releases)\n");
97 }
98
99 my $tmp_pdf = File::Temp->new(UNLINK => 1, SUFFIX => '.pdf');
100  
101 {
102 local $/ = '__END__';
103 my $base64_encoded = <DATA>;
104 my $decoded = decode_base64($base64_encoded);
105 DEBUG "Now try to validate this PDF:", 4;
106 DEBUG $decoded, 4;
107 print $tmp_pdf $decoded;
108 }
109  
110 DEBUG "Created temporary PDF for validation test: " . $tmp_pdf->filename;
111 close $tmp_pdf || die;
112  
113 eval {
114 DEBUG "Now trying to load it via AssertThat->document().";
115 AssertThat->document( $tmp_pdf->filename );
116 };
117  
118  
119 check_result( $@? 0 : 1 );
120 }
121  
122  
123 sub check_result {
124 my ($result) = @_;
125  
126 say "";
127  
128 if ($result == 0) {
129 say "Something is wrong with your setup.";
130 say "Run again with multiple -d switches to get diagnostic messages...";
131 exit 1;
132 }
133 else {
134 say "Your setup looks fine. Have fun with PDF::PDFUnit!";
135 exit 0;
136 }
137 }
138  
139  
140  
141  
142 __DATA__