Reporter/Dumpable/File.pm

filter: Statementstmt 81.5% 31 / 38 Branchbran 44.4% 8 / 18 Conditioncond 50.0% 5 / 10 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 88.8% 8 / 9 Podpod 20.0% 1 / 5 totaltotal 63.5% 54 / 85 SCAR 30.6
CC
14
Cov
67%
new
20.6
_mimetype_from_filename
6.0
BEGIN
1.0
CRAP 21.3
« Reporter/Sender/Email.pm ↑ Summary Reporter.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Exception::Reporter::Dumpable::File;
4 # ABSTRACT: a dumpable object for a file on disk
5 1 $Exception::Reporter::Dumpable::File::VERSION = '0.014';
6 #pod =head1 SYNOPSIS
7 #pod
8 #pod $reporter->report_exception(
9 #pod [
10 #pod ...,
11 #pod [ import_file => Exception::Reporter::Dumpable::File->new(
12 #pod $path_to_file,
13 #pod { mimetype => 'text/csv', charset => 'us-ascii' },
14 #pod ) ],
15 #pod ]
16 #pod );
17 #pod
18 #pod This class exists to provide a simple way to tell Exception::Reporter to
19 #pod include a file from disk. To make this useful, you should also include
20 #pod L<Exception::Reporter::Summarizer::File> in your summarizers.
21 #pod
22 #pod Right now, file content is read as soon as the file is constructed. This may
23 #pod change in the future.
24 #pod
25 #pod =cut
26  
27 sub _err_msg {
28 1 ▶ my ($class, $path, $msg) = @_;
Subroutine _err_msg: called CC=1 SCAR=0.0
29 1 return "(file at <$path> was requested for dumping, but $msg)";
30 }
31  
32 #pod =method new
33 #pod
34 #pod my $file_dumpable = Exception::Reporter::Dumpable::File->new(
35 #pod $path,
36 #pod \%arg,
37 #pod );
38 #pod
39 #pod Useful arguments are:
40 #pod
41 #pod mimetype - defaults to a guess by extension or application/octet-stream
42 #pod charset - defaults to utf-8 for text, undef otherwise
43 #pod max_size - the maximum size to include; if the file is larger, a placeholder
44 #pod will be included instead
45 #pod
46 #pod If the file object can't be constructed, B<the method does not die>. This to
47 #pod avoid requiring exception handling in your exception handling. Instead, C<new>
48 #pod I<will return a string> which will then be summarized as any other string.
49 #pod
50 #pod Maybe this will change in the future, and the file summarizer will know how to
51 #pod expect File::Error objects, or something like that.
52 #pod
53 #pod =cut
54  
55 sub new {
56 2 ▶ my ($class, $path, $arg) = @_;
Subroutine new: called CC=13 SCAR=30.3
Pod documented
57 2 ▶ $arg ||= {};
Condition100%2/2
$arg ||= {}
l!l
11
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
58  
59 2 ▶ return $class->_err_msg($path, 'does not exist') unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 1 1 2
60  
61 1 ▶ my $realpath = -l $path ? readlink $path : $path;
Branch: 1 branch
BranchTrueFalseTotal
-l $path ? : 0 1 1
62  
63 1 ▶ return $class->_err_msg($path, 'is not a normal file') unless -f $realpath;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $realpath 0 1 1
64  
65 1 ▶ return $class->_err_msg($path, "can't be read") unless -r $realpath;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $realpath 0 1 1
66  
67 1 ▶ if ($arg->{max_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"max_size"}) 0 1 1
68 0 my $size = -s $realpath;
69 0 ▶ if ($size > $arg->{max_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $arg->{"max_size"}) 0 0 0
70 0 return $class->_err_msg(
71 $path,
72 "its size $size " . "exceeds maximum allowed size $arg->{max_size}"
73 );
74 }
75 }
76  
77 1 my $guts = { path => $path };
78  
79 $guts->{mimetype} = $arg->{mimetype}
80 1 ▶ || $class->_mimetype_from_filename($path)
Condition40%2/5
$arg->{'mimetype'} || $class->_mimetype_from_filename($path) || 'application/octet-stream'
l!l
10
$arg->{"mimetype"} or $class->_mimetype_from_filename($path)
l!l&&r!l&&!r
100
MC/DC0%0/2
$arg->{'mimetype'} || $class->_mimetype_from_filename($path) || 'application/octet-stream'
$arg->{"mimetype"} $class->_mimetype_from_filename($path)
Truth table33%1/3
A or B
A= $arg->{"mimetype"}B= $class->_mimetype_from_filename($path)
ABresult
1X1
011
000
81 || 'application/octet-stream';
82  
83 $guts->{charset} = $arg->{charset}
84 1 ▶ || $guts->{mimetype} =~ m{\Atext/} ? 'utf-8' : undef;
Branch: 1 branch
BranchTrueFalseTotal
$arg->{'charset'} || $guts->{'mimetype'} =~ m[\Atext/] ? : 1 0 1
Condition33%1/3
$arg->{"charset"} or $guts->{"mimetype"} =~ m[\Atext/]
l!l&&r!l&&!r
100
MC/DC0%0/2
$arg->{"charset"} or $guts->{"mimetype"} =~ m[\Atext/]
$arg->{"charset"} $guts->{"mimetype"} =~ m[\Atext/]
Truth table33%1/3
A or B
A= $arg->{"charset"}B= $guts->{"mimetype"} =~ m[\Atext/]
ABresult
1X1
011
000
85  
86 1 ▶ open my $fh, '<', $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or return $class->_err_msg("there was an error reading it: $!") 0 1 1
87 or return $class->_err_msg("there was an error reading it: $!");
88  
89 1 my $contents = do { local $/; <$fh> };
90  
91 1 $guts->{contents_ref} = \$contents;
92  
93 1 bless $guts => $class;
94 }
95  
96 2 ▶sub path { $_[0]->{path} }
Subroutine path: called CC=1 SCAR=0.0
Pod undocumented
97 2 ▶sub mimetype { $_[0]->{mimetype} }
Subroutine mimetype: called CC=1 SCAR=0.0
Pod undocumented
98 4 ▶sub charset { $_[0]->{charset} }
Subroutine charset: called CC=1 SCAR=0.0
Pod undocumented
99 2 ▶sub contents_ref { $_[0]->{contents_ref} }
Subroutine contents_ref: called CC=1 SCAR=0.0
Pod undocumented
100  
101 # replace with MIME::Type or something -- rjbs, 2012-07-03
102 1 my %LOOKUP = (
103 txt => 'text/plain',
104 html => 'text/html',
105 );
106  
107 sub _mimetype_from_filename {
108 0 ▶ my ($class, $filename) = @_;
Subroutine _mimetype_from_filename: not called CC=2 SCAR=17.9
109  
110 0 my ($extension) = $filename =~ m{\.(.+?)\z};
111 0 ▶ return unless $extension;
Branch: 1 branch
BranchTrueFalseTotal
unless $extension 0 0 0
112  
113 0 return $LOOKUP{ $extension };
114 }
115  
116 1 1;
117  
118 __END__
« Reporter/Sender/Email.pm ↑ Summary Reporter.pm »