Cfn/Diff.pm

filter: Statementstmt 94.8% 110 / 116 Branchbran 81.9% 59 / 72 Conditioncond 60.0% 18 / 30 MC/DCmcdc 40.0% 8 / 20 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 82.3% 205 / 249 CC 51 SCAR 30.2
CC
51
Cov
86%
_compare_resource
28.0
_properties_equal
24.6
_do_diff
7.0
CRAP 20.4
« Cfn/YAML/Schema.pm ↑ Summary Cfn/Dependencies.pm »
1 2 package Cfn::Diff::Changes {
2 2 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 has path => (is => 'ro', isa => 'Str');
4 2 has change => (is => 'ro', isa => 'Str');
5 # to and from are left as rw because diff wants to
6 # reassign these properties to the resolved version
7 # when a DynamicValue is found
8 2 has from => (is => 'rw');
9 2 has to => (is => 'rw');
10 }
11  
12 2 package Cfn::Diff::IncompatibleChange {
13 2 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 extends 'Cfn::Diff::Changes';
15 }
16  
17 2 package Cfn::Diff::ResourcePropertyChange {
18 2 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 extends 'Cfn::Diff::Changes';
20 2 has resource => (is => 'ro', isa => 'Cfn::Resource', required => 1);
21 2 has property => (is => 'ro', isa => 'Str', required => 1);
22  
23 has mutability => (is => 'ro', isa => 'Str|Undef', lazy => 1, default => sub {
24 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
25 1 my $prop_meta = $self->resource->Properties->meta->find_attribute_by_name($self->property);
26 1 ▶ return undef if (not $prop_meta->does('CfnMutability'));
Branch: 1 branch
BranchTrueFalseTotal
unless $prop_meta->does("CfnMutability") 0 1 1
27 1 return $prop_meta->mutability;
28 2 });
29 }
30  
31 2 package Cfn::Diff {
32 2 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 2 has resolve_dynamicvalues => (
35 is => 'ro',
36 isa => 'Bool',
37 default => 0
38 );
39  
40 sub changes {
41 35 ▶ my $self = shift;
Subroutine changes: called CC=2 SCAR=6.9
Pod undocumented
42 35 ▶ return $self->_changes if (defined $self->_changes);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_changes 17 18 35
43 18 $self->_changes([]);
44 18 $self->_do_diff;
45 18 return $self->_changes;
46 }
47  
48 2 has _changes => (
49 is => 'rw',
50 isa => 'ArrayRef[Cfn::Diff::Changes]',
51 traits => [ 'Array' ],
52 handles => {
53 new_addition => 'push',
54 new_deletion => 'push',
55 new_change => 'push',
56 },
57 );
58  
59 2 has left => (is => 'ro', isa => 'Cfn', required => 1);
60 2 has right => (is => 'ro', isa => 'Cfn', required => 1);
61  
62 sub _do_diff {
63 18 ▶ my ($self) = @_;
Subroutine _do_diff: called CC=7 SCAR=19.5
64 18 ▶ my $old = ($self->resolve_dynamicvalues) ? $self->left->resolve_dynamicvalues : $self->left;
Branch: 1 branch
BranchTrueFalseTotal
$self->resolve_dynamicvalues ? : 1 17 18
65 18 ▶ my $new = ($self->resolve_dynamicvalues) ? $self->right->resolve_dynamicvalues : $self->right;
Branch: 1 branch
BranchTrueFalseTotal
$self->resolve_dynamicvalues ? : 1 17 18
66  
67 18 my %new_resources = map { ( $_ => 1 ) } $new->ResourceList;
68 18 my %old_resources = map { ( $_ => 1 ) } $old->ResourceList;
69 18 my %changed = ();
70 18 foreach my $res (keys %new_resources) {
71 18 ▶ if (exists $old_resources{ $res }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $old_resources{$res}) { } 16 2 18
72  
73 16 ▶ if (my @changes = $self->_compare_resource($new->Resource($res), $old->Resource($res), $res)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@changes) = $self->_compare_resource($new->Resource($res), $old->Resource($res), $res)) 11 5 16
74 11 $self->new_change(@changes);
75 }
76  
77 16 delete $new_resources{ $res };
78 16 delete $old_resources{ $res };
79 } else {
80 2 $self->new_addition(Cfn::Diff::Changes->new(path => "Resources.$res", change => 'Resource Added', from => undef, to => $new->Resource($res)));
81 2 delete $new_resources{ $res };
82 }
83 }
84 18 foreach my $res (keys %old_resources) {
85 4 $self->new_deletion(Cfn::Diff::Changes->new(path => "Resources.$res", change => 'Resource Deleted', from => $old->Resource($res), to => undef));
86 }
87 }
88  
89 sub _compare_resource {
90 16 ▶ my ($self, $new_res, $old_res, $logical_id) = @_;
Subroutine _compare_resource: called CC=24 SCAR=33.3
91  
92 16 my $new_res_type = $new_res->Type;
93 16 ▶ $new_res_type = 'AWS::CloudFormation::CustomResource' if ($new_res->isa('Cfn::Resource::AWS::CloudFormation::CustomResource'));
Branch: 1 branch
BranchTrueFalseTotal
if $new_res->isa("Cfn::Resource::AWS::CloudFormation::CustomResource") 3 13 16
94 16 my $old_res_type = $old_res->Type;
95 16 ▶ $old_res_type = 'AWS::CloudFormation::CustomResource' if ($old_res->isa('Cfn::Resource::AWS::CloudFormation::CustomResource'));
Branch: 1 branch
BranchTrueFalseTotal
if $old_res->isa("Cfn::Resource::AWS::CloudFormation::CustomResource") 3 13 16
96  
97 16 ▶ if ($new_res_type ne $old_res_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_res_type ne $old_res_type) 1 15 16
98 1 return Cfn::Diff::IncompatibleChange->new(
99 path => "Resources.$logical_id",
100 change => 'Resource Type Changed',
101 from => $old_res->Type,
102 to => $new_res->Type,
103 );
104 }
105  
106 # This section diffs the resources properties
107 15 my $new = $new_res->Properties;
108 15 my $old = $old_res->Properties;
109  
110 15 ▶ if (not defined $new and not defined $old) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $new || defined $old) { } 1 14 15
elsif (not defined $new && defined $old) { } 2 12 14
Condition100%3/3
defined $new || defined $old
l!l&&r!l&&!r
1311
MC/DC100%2/2
defined $new || defined $old
defined $new defined $old
Truth table100%3/3
A || B
A= defined $newB= defined $old
ABresult
1X1
011
000
111 1 return ; # No changes, and don't go on trying to
112 # diff the properties of unexisting objects
113 ▶ } elsif (not defined $new or not defined $old) {
Condition100%3/3
defined $new && defined $old
!ll&&!rl&&r
1112
MC/DC100%2/2
defined $new && defined $old
defined $new defined $old
Truth table100%3/3
A && B
A= defined $newB= defined $old
ABresult
0X0
100
111
114 2 my $message;
115 2 ▶ $message = "Properties key deleted" if (not defined $new and defined $old);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $new and defined $old 1 1 2
Condition66%2/3
not defined $new and defined $old
!ll&&!rl&&r
101
MC/DC50%1/2
not defined $new and defined $old
not defined $new defined $old
Truth table66%2/3
not(A) and B
A= not defined $newB= defined $old
ABresult
0X0
100
111
116 2 ▶ $message = "Properties key added" if (defined $new and not defined $old);
Branch: 1 branch
BranchTrueFalseTotal
if defined $new and not defined $old 1 1 2
Condition66%2/3
defined $new and not defined $old
!ll&&!rl&&r
101
MC/DC50%1/2
defined $new and not defined $old
defined $new not defined $old
Truth table66%2/3
A and not(B)
A= defined $newB= not defined $old
ABresult
0X0
100
111
117  
118 2 return Cfn::Diff::Changes->new(
119 path => "Resources.$logical_id",
120 change => $message,
121 from => $old,
122 to => $new,
123 );
124 }
125  
126 # If we get here, the two objects have properties
127 12 my @changes = ();
128 12 foreach my $p ($new->meta->get_all_attributes) {
129 259 my $meth = $p->name;
130 259 my $new_val = $new->$meth;
131 259 my $old_val = $old->$meth;
132  
133 259 ▶ next if (not defined $new_val and not defined $old_val);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_val or defined $old_val 237 22 259
Condition66%2/3
defined $new_val or defined $old_val
l!l&&r!l&&!r
220237
MC/DC50%1/2
defined $new_val or defined $old_val
defined $new_val defined $old_val
Truth table66%2/3
A or B
A= defined $new_valB= defined $old_val
ABresult
1X1
011
000
134  
135 22 my $change_description;
136 22 ▶ if ( defined $old_val and not defined $new_val) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $old_val and not defined $new_val) { } 0 22 22
elsif (not defined $old_val and defined $new_val) { } 1 21 22
elsif (defined $old_val and defined $new_val) { } 21 0 21
elsif (not defined $old_val || defined $new_val) { } 0 0 0
Condition66%2/3
defined $old_val and not defined $new_val
!ll&&!rl&&r
1210
MC/DC0%0/2
defined $old_val and not defined $new_val
defined $old_val not defined $new_val
Truth table66%2/3
A and not(B)
A= defined $old_valB= not defined $new_val
ABresult
0X0
100
111
137 0 $change_description = 'Property Deleted';
138 ▶ } elsif (not defined $old_val and defined $new_val) {
Condition66%2/3
not defined $old_val and defined $new_val
!ll&&!rl&&r
2101
MC/DC50%1/2
not defined $old_val and defined $new_val
not defined $old_val defined $new_val
Truth table66%2/3
not(A) and B
A= not defined $old_valB= defined $new_val
ABresult
0X0
100
111
139 1 $change_description = 'Property Added';
140 ▶ } elsif ( defined $old_val and defined $new_val) {
Condition33%1/3
defined $old_val and defined $new_val
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $old_val and defined $new_val
defined $old_val defined $new_val
Truth table33%1/3
A and B
A= defined $old_valB= defined $new_val
ABresult
0X0
100
111
141 21 ▶ if (not $self->_properties_equal($new_val, $old_val, "$logical_id.$meth")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_properties_equal($new_val, $old_val, "$logical_id.$meth")) { } 8 13 21
142 8 $change_description = 'Property Changed';
143 } else {
144 next
145 13 }
146 ▶ } elsif (not defined $old_val and not defined $new_val) {
Condition0%0/3
defined $old_val || defined $new_val
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $old_val || defined $new_val
defined $old_val defined $new_val
Truth table0%0/3
A || B
A= defined $old_valB= defined $new_val
ABresult
1X1
011
000
147 0 next;
148 }
149  
150 9 push @changes, Cfn::Diff::ResourcePropertyChange->new(
151 path => "Resources.$logical_id.Properties.$meth",
152 change => $change_description,
153 from => $old_val,
154 to => $new_val,
155 resource => $new_res,
156 property => $meth,
157 );
158 }
159 12 return @changes;
160 }
161  
162 2 ▶ use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 sub _properties_equal {
164 46 ▶ my ($self, $new, $old) = @_;
Subroutine _properties_equal: called CC=20 SCAR=32.0
165  
166 46 ▶ if (blessed($new)){
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($new)) { } 23 23 46
167 23 ▶ if (blessed($old)){
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($old)) { } 23 0 23
168 # See if old and new are of the same class
169 23 ▶ return 0 if ($new->meta->name ne $old->meta->name);
Branch: 1 branch
BranchTrueFalseTotal
if $new->meta->name ne $old->meta->name 1 22 23
170  
171 # Old and new are guaranteed to be the same type now, so just go on with new
172 22 ▶ if ($new->isa('Cfn::DynamicValue')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($new->isa('Cfn::DynamicValue')) { } 1 21 22
elsif ($new->isa('Cfn::Value::Primitive')) { } 14 7 21
elsif ($new->isa('Cfn::Value::Function')) { } 2 5 7
elsif ($new->isa('Cfn::Value')) { } 5 0 5
173 1 return 0;
174 } elsif ($new->isa('Cfn::Value::Primitive')) {
175 14 return ($new->Value eq $old->Value);
176 } elsif ($new->isa('Cfn::Value::Function')) {
177 2 ▶ return (($new->Function eq $old->Function) and $self->_properties_equal($new->Value, $old->Value));
Condition33%1/3
$new->Function eq $old->Function && $self->_properties_equal($new->Value, $old->Value)
!ll&&!rl&&r
002
MC/DC0%0/2
$new->Function eq $old->Function && $self->_properties_equal($new->Value, $old->Value)
$new->Function eq $old->Function $self->_properties_equal($new->Value, $old->Value)
Truth table33%1/3
A && B
A= $new->Function eq $old->FunctionB= $self->_properties_equal($new->Value, $old->Value)
ABresult
0X0
100
111
178 } elsif ($new->isa('Cfn::Value')) {
179 5 return $self->_properties_equal($new->as_hashref, $old->as_hashref);
180 } else {
181 0 die "Don't know how to compare $new";
182 }
183 } else {
184 0 return 0;
185 }
186 } else {
187 23 ▶ if (blessed($old)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($old)) { } 0 23 23
188 0 return 0;
189 } else {
190 23 ▶ return 0 if (ref($old) ne ref($new));
Branch: 1 branch
BranchTrueFalseTotal
if ref $old ne ref $new 0 23 23
191 23 ▶ if (not ref($new)){
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $new) { } 10 13 23
elsif (ref $new eq 'ARRAY') { } 6 7 13
elsif (ref $new eq 'HASH') { } 7 0 7
192 10 return ($new eq $old);
193 } elsif (ref($new) eq 'ARRAY') {
194 6 ▶ return 0 if (@$new != @$old);
Branch: 1 branch
BranchTrueFalseTotal
if @$new != @$old 0 6 6
195 6 ▶ for (my $i = 0; $i < @$new; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$new) 11 6 17
196 11 ▶ return 0 if (not $self->_properties_equal($new->[$i], $old->[$i]));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_properties_equal($new->[$i], $old->[$i]) 0 11 11
197 }
198 6 return 1;
199 } elsif (ref($new) eq 'HASH') {
200 7 ▶ return 0 if ((keys %$new) != (keys %$old));
Branch: 1 branch
BranchTrueFalseTotal
if keys %$new != keys %$old 0 7 7
201 7 foreach my $key (keys %$new) {
202 7 ▶ return 0 if (not $self->_properties_equal($new->{ $key }, $old->{ $key }));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_properties_equal($new->{$key}, $old->{$key}) 2 5 7
203 }
204 5 return 1;
205 } else {
206 0 die "Don't know how to non-blessed compare " . ref($new);
207 }
208 }
209 }
210 }
211 }
212  
213 2 1;
« Cfn/YAML/Schema.pm ↑ Summary Cfn/Dependencies.pm »