blib/lib/RackMan/Utils.pm

filter: Statementstmt 32.0% 24 / 75 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 77.7% 7 / 9 Podpod 100% 2 / 2 totaltotal 22.1% 33 / 149 CC 26 SCAR 59.3
CC
26
Cov
19%
diff_lines
552.0
parse_regexp
20.0
BEGIN
1.0
CRAP 376.0
« bin/cfengine-tags ↑ Summary blib/lib/RackMan/Device/Server/HP_ProLiant.pm »
1 package RackMan::Utils;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Algorithm::Diff ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Term::ANSIColor qw< CLEAR GREEN RED >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 DIFF_CONTEXT => 3,
14 CHUNK_SEPARATOR => '@@',
15 2 };
16  
17 2 our @ISA = qw< Exporter >;
18 2 our @EXPORT = qw< diff_lines parse_regexp >;
19  
20  
21 #
22 # diff_lines()
23 # ----------
24 0 ▶sub diff_lines {
Subroutine diff_lines: not called CC=23 SCAR=63.1
Pod documented
25 0 my ($input_A, $input_B, $opts) = @_;
26  
27 0 ▶ croak "error: input A is expected as an arrayref"
Branch: 1 branch
BranchTrueFalseTotal
if ref $input_A ne "ARRAY" 0 0 0
28 if ref $input_A ne "ARRAY";
29 0 ▶ croak "error: input B is expected as an arrayref"
Branch: 1 branch
BranchTrueFalseTotal
if ref $input_B ne "ARRAY" 0 0 0
30 if ref $input_B ne "ARRAY";
31 0 ▶ croak "error: options are expected as a hashref"
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts and ref $opts ne "HASH" 0 0 0
Condition0%0/3
ref $opts and ref $opts ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $opts and ref $opts ne "HASH"
ref $opts ref $opts ne "HASH"
Truth table0%0/3
A and B
A= ref $optsB= ref $opts ne "HASH"
ABresult
0X0
100
111
32 if ref $opts and ref $opts ne "HASH";
33  
34 # default values
35 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
36 0 ▶ $opts->{input_A_header} ||= "Current configuration";
Condition0%0/2
$opts->{"input_A_header"} ||= "Current configuration"
l!l
00
MC/DC0%0/1
$opts->{"input_A_header"} ||= "Current configuration"
$opts->{"input_A_header"}
Truth table0%0/2
A
A= $opts->{"input_A_header"}
Aresult
11
00
37 0 ▶ $opts->{input_B_header} ||= "Expected configuration";
Condition0%0/2
$opts->{"input_B_header"} ||= "Expected configuration"
l!l
00
MC/DC0%0/1
$opts->{"input_B_header"} ||= "Expected configuration"
$opts->{"input_B_header"}
Truth table0%0/2
A
A= $opts->{"input_B_header"}
Aresult
11
00
38  
39 # computes the differences between the two input
40 0 my @lines = Algorithm::Diff::sdiff($input_A, $input_B);
41  
42 # Note: We use Algorithm::Diff::sdiff() instead of Text::Diff::diff()
43 # because we want to ignore blank lines, an option not provided by
44 # Text::Diff (as of v1.41).
45  
46 # do we want colours?
47 0 my %x = ( "+" => GREEN, "-" => RED, R => CLEAR );
48  
49 0 my @ctx;
50 0 my $last_diff = DIFF_CONTEXT;
51 0 my $chunk_no = 0;
52 0 my @diff;
53  
54 # construct the output
55 0 for my $line (@lines) {
56 # a line is an arrayref with the following elements:
57 # - modifier, "+" (addition), "-" (deletion), "c" (change),
58 # "u" (unmodified)
59 # - old value
60 # - new value
61 #
62 # see Algorithm::Diff documentation for more details
63  
64 0 ▶ if ($line->[0] eq "+" and length $line->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->[0] eq "+" and length $line->[2]) 0 0 0
Condition0%0/3
$line->[0] eq "+" and length $line->[2]
!ll&&!rl&&r
000
MC/DC0%0/2
$line->[0] eq "+" and length $line->[2]
$line->[0] eq "+" length $line->[2]
Truth table0%0/3
A and B
A= $line->[0] eq "+"B= length $line->[2]
ABresult
0X0
100
111
65 # push the "before-change" context, and the new line
66 0 ▶ push @diff, CHUNK_SEPARATOR."\n" if $chunk_no > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_no > 1 0 0 0
67 0 push @diff, @ctx, "$x{$line->[0]}$line->[0]$line->[2]$x{R}\n";
68 0 @ctx = ();
69 0 $last_diff = 0;
70 0 $chunk_no++;
71 }
72  
73 0 ▶ if ($line->[0] eq "-" and length $line->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->[0] eq "-" and length $line->[1]) 0 0 0
Condition0%0/3
$line->[0] eq "-" and length $line->[1]
!ll&&!rl&&r
000
MC/DC0%0/2
$line->[0] eq "-" and length $line->[1]
$line->[0] eq "-" length $line->[1]
Truth table0%0/3
A and B
A= $line->[0] eq "-"B= length $line->[1]
ABresult
0X0
100
111
74 # push the "before-change" context, and the old line
75 0 ▶ push @diff, CHUNK_SEPARATOR."\n" if $chunk_no > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_no > 1 0 0 0
76 0 push @diff, @ctx, "$x{$line->[0]}$line->[0]$line->[1]$x{R}\n";
77 0 @ctx = ();
78 0 $last_diff = 0;
79 0 $chunk_no++;
80 }
81  
82 0 ▶ if ($line->[0] eq "c" and length $line->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->[0] eq "c" and length $line->[1]) 0 0 0
Condition0%0/3
$line->[0] eq "c" and length $line->[1]
!ll&&!rl&&r
000
MC/DC0%0/2
$line->[0] eq "c" and length $line->[1]
$line->[0] eq "c" length $line->[1]
Truth table0%0/3
A and B
A= $line->[0] eq "c"B= length $line->[1]
ABresult
0X0
100
111
83 # push the "before-change" context, the old line and the new line
84 0 ▶ push @diff, CHUNK_SEPARATOR."\n" if $chunk_no > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_no > 1 0 0 0
85 0 push @diff, @ctx, "$x{'-'}-$line->[1]\n$x{'+'}+$line->[2]$x{R}\n";
86 0 @ctx = ();
87 0 $last_diff = 0;
88 0 $chunk_no++;
89 }
90  
91 0 ▶ if ($line->[0] eq "u") {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->[0] eq "u") 0 0 0
92 # accumulate diff context, no more than DIFF_CONTEXT lines
93 0 push @ctx, " $line->[1]\n";
94 0 ▶ shift @ctx while @ctx > DIFF_CONTEXT;
Branch: 1 branch
BranchTrueFalseTotal
@ctx > 3 and do { shift @ctx } 0 0 0
95 0 $last_diff++;
96  
97 0 ▶ if ($last_diff == DIFF_CONTEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_diff == 3) 0 0 0
98 # push "after-change" context
99 0 push @diff, @ctx;
100 0 @ctx = ();
101 }
102 }
103 }
104  
105 # add the diff headers
106 0 ▶ @diff and unshift @diff,
Branch: 1 branch
BranchTrueFalseTotal
@diff and unshift @diff, qq[$x{"-"}--- $opts->{"input_A_header"}$x{"R"}\n], qq[$x{"+"}+++ $opts->{"input_B_header"}$x{"R"}\n] 0 0 0
107 "$x{'-'}--- $opts->{input_A_header}$x{R}\n",
108 "$x{'+'}+++ $opts->{input_B_header}$x{R}\n";
109  
110 0 ▶ return wantarray ? @diff : \@diff
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
111 }
112  
113  
114 #
115 # parse_regexp()
116 # ------------
117 0 ▶sub parse_regexp {
Subroutine parse_regexp: not called CC=4 SCAR=30.0
Pod documented
118 0 my ($text) = @_;
119 0 my $flags = "";
120  
121 0 ▶ if ($text =~ m:^/:) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ m[^/]) 0 0 0
122 0 $text =~ s:^/::;
123 0 $text =~ s:/(\w+)?$::;
124 0 ▶ $flags = "(?:$1)" if defined $1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 0 0 0
125 }
126  
127 0 my $re = eval { qr/$flags$text/ };
128 0 (my $err = $@) =~ s/ at .* line \d+\.//;
129  
130 0 ▶ return wantarray ? ($re, $err) : $re
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
131 }
132  
133  
134 2 __PACKAGE__
135  
136 __END__
« bin/cfengine-tags ↑ Summary blib/lib/RackMan/Device/Server/HP_ProLiant.pm »