blib/lib/Server/Module/Comparison.pm

filter: Statementstmt 68.3% 110 / 161 Branchbran 66.6% 28 / 42 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 71.4% 15 / 21 Podpod 55.5% 5 / 9 totaltotal 68.1% 165 / 242 CC 31 SCAR 18.5
CC
31
Cov
68%
FromModuleList
12.0
check_correct_guess
12.0
_module_pair
12.0
CRAP 6.3
↑ Summary
1 3 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Server::Module::Comparison;
3  
4 # ABSTRACT: check perl module versions installed on servers.
5  
6 3 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Capture::Tiny qw/capture/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use failures qw/module::comparison/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Perl::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $VERSION = '0.012';
14  
15 3 has perl_path => (is => 'ro', isa => Str, default => '');
16 3 has modules => (is => 'ro', isa => ArrayRef);
17  
18 sub FromModuleList
19 0 ▶{
Subroutine FromModuleList: not called CC=3 SCAR=24.8
Pod documented
20 0 my $filename = shift;
21 0 my $extra_params = shift;
22 0 ▶ $extra_params = {} unless defined $extra_params;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extra_params 0 0 0
23 0 my @lines;
24 0 ▶ if($filename eq '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq '-') { } 0 0 0
25 {
26 0 @lines = <STDIN>;
27 }
28 else
29 {
30 0 @lines = path($filename)->lines_utf8;
31 }
32 0 my @modules = map { chomp; $_ } grep { !/^\s*$/ } @lines;
33 0 return Server::Module::Comparison->new({ %$extra_params, modules => \@modules });
34 }
35  
36 sub _mversion_command
37 1 ▶{
Subroutine _mversion_command: called CC=2 SCAR=7.0
38 1 my $self = shift;
39 1 my $command = 'mversion';
40 1 ▶ if($self->perl_path)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->perl_path) 1 0 1
41 {
42 1 $command = path($self->perl_path)->child($command);
43 }
44 1 return [$command, '-f', @{$self->modules}, '2>&1'];
45 }
46  
47 sub check_container
48 0 ▶{
Subroutine check_container: not called CC=1 SCAR=6.9
Pod documented
49 0 my $self = shift;
50 0 my $container = shift;
51 0 my $cmd = [qw/docker run --rm -i/, $container, @{$self->_mversion_command}];
52 0 return $self->_run_mversion($cmd);
53 }
54  
55 sub check_ssh_server
56 0 ▶{
Subroutine check_ssh_server: not called CC=1 SCAR=6.9
Pod documented
57 0 my $self = shift;
58 0 my $server = shift;
59 0 my $cmd = ['ssh', $server, @{$self->_mversion_command}];
60 0 return $self->_run_mversion($cmd);
61 }
62  
63 sub check_local
64 1 ▶{
Subroutine check_local: called CC=1 SCAR=0.0
Pod documented
65 1 my $self = shift;
66 1 my $cmd = $self->_mversion_command;
67 1 return $self->_run_mversion($cmd);
68 }
69  
70 sub identify_resource
71 4 ▶{
Subroutine identify_resource: called CC=4 SCAR=13.9
Pod undocumented
72 4 my $self = shift;
73 4 my $identifier = shift;
74 4 ▶ if($identifier =~ m|docker://(.*)$|)
Branch: 3 branches
BranchTrueFalseTotal
if ($identifier =~ m[docker://(.*)$]) { } 1 3 4
elsif ($identifier =~ m[ssh://(.*)$]) { } 1 2 3
elsif ($identifier =~ m[/]) { } 1 1 2
75 {
76 1 return ('docker', $1);
77 }
78 elsif($identifier =~ m|ssh://(.*)$|)
79 {
80 1 return ('ssh', $1);
81 }
82 elsif($identifier =~ m|/|)
83 {
84 # assume it's docker
85 1 return ('docker', $identifier);
86 }
87 else
88 {
89 # assume it's ssh
90 1 return ('ssh', $identifier);
91 }
92 }
93  
94 sub check_correct_guess
95 0 ▶{
Subroutine check_correct_guess: not called CC=3 SCAR=24.8
Pod documented
96 0 my $self = shift;
97 0 my $identifier = shift;
98 0 my ($type, $server) = $self->identify_resource($identifier);
99 0 ▶ if($type eq 'ssh')
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ssh') { } 0 0 0
elsif ($type eq 'docker') { } 0 0 0
100 {
101 0 return $self->check_ssh_server($server);
102 }
103 elsif($type eq 'docker')
104 {
105 0 return $self->check_container($server);
106 }
107 else
108 {
109 0 die 'I don\'t know what to do!';
110 }
111 }
112  
113 sub _run_mversion
114 1 ▶{
Subroutine _run_mversion: called CC=2 SCAR=9.2
115 1 my $self = shift;
116 1 my $cmd = shift;
117 #print "$cmd\n";
118 1 ▶ my ($stdout, $stderr, $exit) = capture {
Subroutine __ANON__: called CC=1 SCAR=0.0
119 1 system(@$cmd);
120 1 };
121 1 ▶ if($exit)
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) 1 0 1
122 {
123 1 my $command = join(' ', @$cmd);
124 1 failure::module::comparison->throw("Failure running $command: $exit");
125 }
126 0 my @lines = map { chomp; $_ } grep { !/^\s*$/ } split(/\r\n|\r|\n/, $stdout);
127 0 my %versions = map { _module_pair($_) } @lines;
128 0 return \%versions;
129 }
130  
131 sub _module_pair
132 0 ▶{
Subroutine _module_pair: not called CC=3 SCAR=24.8
133 0 my $line = shift;
134 0 my ($module, $version) = $line =~ /(.*) ((:?\d+(:?\.\d+)*|undef))/;
135 0 ▶ if($module)
Branch: 1 branch
BranchTrueFalseTotal
if ($module) { } 0 0 0
136 {
137 0 return ($module, $version);
138 }
139 else
140 {
141 0 my ($missing_module) = $line =~ /'(.*)' does not seem/;
142 0 ▶ unless($missing_module)
Branch: 1 branch
BranchTrueFalseTotal
unless ($missing_module) 0 0 0
143 {
144 0 print "Error: $line\n";
145 0 return ("error", "error");
146 }
147 0 return ($missing_module, 'missing');
148 }
149 }
150  
151 sub _wrap_version
152 26 ▶{
Subroutine _wrap_version: called CC=4 SCAR=13.9
153 26 my $version = shift;
154 26 ▶ if($version && ($version eq 'undef' || $version eq 'missing'))
Branch: 1 branch
BranchTrueFalseTotal
if ($version and $version eq "undef" || $version eq "missing") 4 22 26
Condition83%5/6
$version and $version eq "undef" || $version eq "missing"
!ll&&!rl&&r
0224
$version eq "undef" or $version eq "missing"
l!l&&r!l&&!r
2222
MC/DC66%2/3
$version and $version eq "undef" || $version eq "missing"
$version $version eq "undef" $version eq "missing"
Truth table75%3/4
A and B or C
A= $versionB= $version eq "undef"C= $version eq "missing"
ABCresult
0XX0
1000
11X1
1011
155 {
156 # this isn't perfect, in fact it may bite us.
157 # I believe that undef does indicate that a module is
158 # there, it's just it doesn't have $VERSION set.
159 4 return Perl::Version->new('0');
160 }
161 22 return Perl::Version->new($version);
162 }
163  
164 sub difference_report
165 2 ▶{
Subroutine difference_report: called CC=7 SCAR=19.5
Pod undocumented
166 2 my $self = shift;
167 2 my $first = shift;
168 2 my $second = shift;
169  
170 2 my %newer;
171 my %older;
172 2 my %newly_installed;
173 2 my %removed;
174 2 for my $module (keys %$first)
175 {
176 16 ▶ if(exists $second->{$module})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $second->{$module}) { } 13 3 16
177 {
178 13 my $v1 = _wrap_version($first->{$module});
179 13 my $v2 = _wrap_version($second->{$module});
180 13 ▶ if($v1 > $v2)
Branch: 2 branches
BranchTrueFalseTotal
if ($v1 > $v2) { } 1 12 13
elsif ($v1 < $v2) { } 2 10 12
181 {
182 1 $older{$module} = [$first->{$module}, $second->{$module}];
183 }
184 elsif($v1 < $v2)
185 {
186 2 $newer{$module} = [$first->{$module}, $second->{$module}];
187 }
188 }
189 else
190 {
191 3 $removed{$module} = $first->{$module};
192 }
193 }
194 2 for my $module (keys %$second)
195 {
196 14 ▶ unless(exists $first->{$module})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $first->{$module}) 1 13 14
197 {
198 1 $newly_installed{$module} = $second->{$module};
199 }
200 }
201 return {
202 2 updated => \%newer,
203 downgraded => \%older,
204 removed => \%removed,
205 installed => \%newly_installed,
206 };
207 }
208  
209 sub human_readable_report
210 3 ▶{
Subroutine human_readable_report: called CC=10 SCAR=23.0
Pod undocumented
211 3 my $self = shift;
212 3 my $report = shift;
213 3 my @lines;
214 3 ▶ if(%{$report->{downgraded}})
Branch: 1 branch
BranchTrueFalseTotal
if (%{$report->{"downgraded"};}) 2 1 3
215 {
216 2 push @lines, 'DOWNGRADED Modules', '';
217 2 for my $key (sort keys %{$report->{downgraded}})
218 {
219 2 my ($v1, $v2) = @{$report->{downgraded}->{$key}};
220 2 push @lines, sprintf("%-40s\t%s -> %s", $key, $v1, $v2);
221 }
222 2 push @lines, '';
223 }
224 3 ▶ if(%{$report->{removed}})
Branch: 1 branch
BranchTrueFalseTotal
if (%{$report->{"removed"};}) 2 1 3
225 {
226 2 push @lines, 'REMOVED Modules', '';
227 2 for my $key (sort keys %{$report->{removed}})
228 {
229 6 my $v1 = $report->{removed}->{$key};
230 6 push @lines, sprintf("%-40s\t%s", $key, $v1);
231 }
232 2 push @lines, '';
233 }
234 3 ▶ if(%{$report->{installed}})
Branch: 1 branch
BranchTrueFalseTotal
if (%{$report->{"installed"};}) 2 1 3
235 {
236 2 push @lines, 'Installed Modules', '';
237 2 for my $key (sort keys %{$report->{installed}})
238 {
239 2 my $v1 = $report->{installed}->{$key};
240 2 push @lines, sprintf("%-40s\t%s", $key, $v1);
241 }
242 2 push @lines, '';
243 }
244 3 ▶ if(%{$report->{updated}})
Branch: 1 branch
BranchTrueFalseTotal
if (%{$report->{"updated"};}) 2 1 3
245 {
246 2 push @lines, 'Updated Modules', '';
247 2 for my $key (sort keys %{$report->{updated}})
248 {
249 4 my ($v1, $v2) = @{$report->{updated}->{$key}};
250 4 push @lines, sprintf("%-40s\t%s -> %s", $key, $v1, $v2);
251 }
252 2 push @lines, '';
253 }
254 3 ▶ unless(@lines)
Branch: 1 branch
BranchTrueFalseTotal
unless (@lines) 1 2 3
255 {
256 1 push @lines, 'No differences', '';
257 }
258 3 return join "\n", @lines;
259 }
260  
261 sub human_readable_list
262 0 ▶{
Subroutine human_readable_list: not called CC=2 SCAR=17.9
Pod undocumented
263 0 my $self = shift;
264 0 my $list = shift;
265  
266 0 my @lines;
267 0 for my $module (keys %$list)
268 {
269 0 push @lines, sprintf("%-40s\t%s", $module, $list->{$module});
270 }
271 0 return join "\n", @lines;
272 }
273  
274 3 1;
275  
276 __END__
↑ Summary