Skip to content

Commit e467d8f

Browse files
author
Ashley Penney
committed
Merge pull request #220 from mediatemple/fix/master/deep_merge_fix
PUP-1724 Don't modify the paramaters to deep_merge
2 parents 6a89e1c + c12e9af commit e467d8f

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

lib/puppet/parser/functions/deep_merge.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Puppet::Parser::Functions
2020
end
2121

2222
deep_merge = Proc.new do |hash1,hash2|
23-
hash1.merge!(hash2) do |key,old_value,new_value|
23+
hash1.merge(hash2) do |key,old_value,new_value|
2424
if old_value.is_a?(Hash) && new_value.is_a?(Hash)
2525
deep_merge.call(old_value, new_value)
2626
else
@@ -37,7 +37,7 @@ module Puppet::Parser::Functions
3737
raise Puppet::ParseError, "deep_merge: unexpected argument type #{arg.class}, only expects hash arguments"
3838
end
3939

40-
deep_merge.call(result, arg)
40+
result = deep_merge.call(result, arg)
4141
end
4242
return( result )
4343
end

spec/unit/puppet/parser/functions/deep_merge_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,33 @@
7373
hash['key1'].should == { 'a' => 1, 'b' => 99 }
7474
hash['key2'].should == { 'c' => 3 }
7575
end
76+
77+
it 'should not change the original hashes' do
78+
hash1 = {'one' => { 'two' => 2 } }
79+
hash2 = { 'one' => { 'three' => 3 } }
80+
hash = scope.function_deep_merge([hash1, hash2])
81+
hash1.should == {'one' => { 'two' => 2 } }
82+
hash2.should == { 'one' => { 'three' => 3 } }
83+
hash['one'].should == { 'two' => 2, 'three' => 3 }
84+
end
85+
86+
it 'should not change the original hashes 2' do
87+
hash1 = {'one' => { 'two' => [1,2] } }
88+
hash2 = { 'one' => { 'three' => 3 } }
89+
hash = scope.function_deep_merge([hash1, hash2])
90+
hash1.should == {'one' => { 'two' => [1,2] } }
91+
hash2.should == { 'one' => { 'three' => 3 } }
92+
hash['one'].should == { 'two' => [1,2], 'three' => 3 }
93+
end
94+
95+
it 'should not change the original hashes 3' do
96+
hash1 = {'one' => { 'two' => [1,2, {'two' => 2} ] } }
97+
hash2 = { 'one' => { 'three' => 3 } }
98+
hash = scope.function_deep_merge([hash1, hash2])
99+
hash1.should == {'one' => { 'two' => [1,2, {'two' => 2}] } }
100+
hash2.should == { 'one' => { 'three' => 3 } }
101+
hash['one'].should == { 'two' => [1,2, {'two' => 2} ], 'three' => 3 }
102+
hash['one']['two'].should == [1,2, {'two' => 2}]
103+
end
76104
end
77105
end

0 commit comments

Comments
 (0)