Introspector/Util.pm

filter: Statementstmt 95.5% 86 / 90 Branchbran 68.7% 22 / 32 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 9 totaltotal 80.8% 127 / 157 CC 19 SCAR 13.5
CC
19
Cov
86%
handle_from_command
7.3
output_from_command
7.1
files_from_dir
4.0
CRAP 3.9
« Introspector/State.pm ↑ Summary Introspector/Probe/MountPoints.pm »
1 16 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package System::Introspector::Util;
4 16 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use IPC::Run qw( run );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use IPC::Open2;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Capture::Tiny qw( capture_stderr );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 our @EXPORT_OK = qw(
12 handle_from_command
13 handle_from_file
14 output_from_command
15 output_from_file
16 lines_from_command
17 files_from_dir
18 transform_exceptions
19 fail
20 );
21  
22 16 do {
23 package System::Introspector::_Exception;
24 16 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 16 has message => (is => 'ro');
26 };
27  
28 1 ▶sub fail { die System::Introspector::_Exception->new(message => shift) }
Subroutine fail: called CC=1 SCAR=0.0
Pod undocumented
29 1 ▶sub is_report_exception { ref(shift) eq 'System::Introspector::_Exception' }
Subroutine is_report_exception: called CC=1 SCAR=0.0
Pod undocumented
30  
31 4 ▶sub files_from_dir {
Subroutine files_from_dir: called CC=4 SCAR=13.9
Pod undocumented
32 4 my ($dir) = @_;
33 4 my $dh;
34 4 ▶ opendir $dh, $dir
Branch: 1 branch
BranchTrueFalseTotal
opendir $dh, $dir or fail("Unable to read directory ${dir}: $!") 4 0 4
35 or fail "Unable to read directory $dir: $!";
36 4 my @files;
37 4 ▶ while (defined( my $item = readdir $dh )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $item = readdir $dh)) 14 4 18
38 14 ▶ next if -d "$dir/$item";
Branch: 1 branch
BranchTrueFalseTotal
if -d "$dir/$item" 8 6 14
39 6 push @files, $item;
40 }
41 4 return @files;
42 }
43  
44 48 ▶sub transform_exceptions (&) {
Subroutine transform_exceptions: called CC=3 SCAR=11.2
Pod undocumented
45 48 my ($code) = @_;
46 48 my $result = eval { $code->() };
47 48 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 1 47 48
48 1 ▶ return { __error__ => $error->message }
Branch: 1 branch
BranchTrueFalseTotal
if is_report_exception($error) 1 0 1
49 if is_report_exception $error;
50 0 die $@;
51 }
52 47 return $result;
53 }
54  
55 16 ▶sub output_from_command {
Subroutine output_from_command: called CC=7 SCAR=19.6
Pod undocumented
56 16 my ($command, $in) = @_;
57 16 ▶ $in = ''
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in 16 0 16
58 unless defined $in;
59 16 my ($out, $err) = ('', '');
60 16 ▶ my $ok = eval { run($command, \$in, \$out, \$err) or die $err};
Condition50%1/2
run($command, \$in, \$out, \$err) || die($err)
l!l
150
MC/DC0%0/1
run($command, \$in, \$out, \$err) || die($err)
run($command, \$in, \$out, \$err)
Truth table50%1/2
A
A= run($command, \$in, \$out, \$err)
Aresult
11
00
61 16 ▶ $err = $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 15 16
62 16 ▶ return $out, $err, $ok
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 2 14 16
63 if wantarray;
64 14 ▶ $command = join ' ', @$command
Branch: 1 branch
BranchTrueFalseTotal
if ref $command 14 0 14
65 if ref $command;
66 14 ▶ fail "Error running command ($command): $err"
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 13 14
67 unless $ok;
68 13 return $out;
69 }
70  
71 4 ▶sub lines_from_command {
Subroutine lines_from_command: called CC=1 SCAR=0.0
Pod undocumented
72 4 my ($command) = @_;
73 4 my $output = output_from_command $command;
74 3 chomp $output;
75 3 return split m{\n}, $output;
76 }
77  
78 8 ▶sub handle_from_command {
Subroutine handle_from_command: called CC=6 SCAR=19.9
Pod undocumented
79 8 my ($command) = @_;
80 8 my $pipe;
81 8 local $@;
82 8 my $ok = eval {
83 8 my $out;
84 my $child_pid;
85 8 my @lines;
86 8 ▶ my ($err) = capture_stderr {
Subroutine __ANON__: called CC=1 SCAR=0.0
87 8 $child_pid = open2($out, File::Spec->devnull, $command);
88 8 @lines = <$out>;
89 8 close $out;
90 8 waitpid $child_pid, 0;
91 8 };
92 8 my $content = join '', @lines;
93 8 my $status = $? >> 8;
94 8 ▶ $err = "Unknown error"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 0 8 8
95 unless defined $err;
96 8 ▶ fail "Command error ($command): $err\n"
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 8 8
97 if $status;
98 8 open $pipe, '<', \$content;
99 8 1;
100 };
101 8 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 8 8
102 0 my $err = $@;
103 0 ▶ die $err
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($err) and $err->isa("System::Introspector::_Exception") 0 0 0
Condition0%0/3
builtin::blessed($err) and $err->isa("System::Introspector::_Exception")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($err) and $err->isa("System::Introspector::_Exception")
builtin::blessed($err) $err->isa("System::Introspector::_Exception")
Truth table0%0/3
A and B
A= builtin::blessed($err)B= $err->isa("System::Introspector::_Exception")
ABresult
0X0
100
111
104 if blessed($err) and $err->isa('System::Introspector::_Exception');
105 0 fail "Error from command '$command': $err";
106 }
107 8 return $pipe;
108 }
109  
110 19 ▶sub handle_from_file {
Subroutine handle_from_file: called CC=2 SCAR=7.1
Pod undocumented
111 19 my ($file) = @_;
112 19 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or fail("Unable to read ${file}: $!") 19 0 19
113 or fail "Unable to read $file: $!";
114 19 return $fh;
115 }
116  
117 15 ▶sub output_from_file {
Subroutine output_from_file: called CC=2 SCAR=6.9
Pod undocumented
118 15 my ($file) = @_;
119 15 my $fh = handle_from_file $file;
120 return <$fh>
121 15 ▶ if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 5 10 15
122 10 return do { local $/; <$fh> };
123 }
124  
125 16 1;
126  
127 __END__
« Introspector/State.pm ↑ Summary Introspector/Probe/MountPoints.pm »