]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
PUP-1724 Don't modify the paramaters to deep_merge
authorJustin Burnham <jburnham@mediatemple.net>
Mon, 17 Feb 2014 19:46:55 +0000 (11:46 -0800)
committerJustin Burnham <jburnham@mediatemple.net>
Mon, 17 Feb 2014 20:19:40 +0000 (12:19 -0800)
Instead of modifying the first paramater of deep_merge due to the
use of the merge! function, instead use merge to return a copy of
the merged object. This allows one to continue to use the original
first parameter after the call to deep_merge.

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

index 94677b8b38cfa0266557fe4bec5ad545e6ef16a9..6df32e9c56729d981cd42e4322e40ba28bfd59b8 100644 (file)
@@ -20,7 +20,7 @@ module Puppet::Parser::Functions
     end
 
     deep_merge = Proc.new do |hash1,hash2|
-      hash1.merge!(hash2) do |key,old_value,new_value|
+      hash1.merge(hash2) do |key,old_value,new_value|
         if old_value.is_a?(Hash) && new_value.is_a?(Hash)
           deep_merge.call(old_value, new_value)
         else
@@ -37,7 +37,7 @@ module Puppet::Parser::Functions
         raise Puppet::ParseError, "deep_merge: unexpected argument type #{arg.class}, only expects hash arguments"
       end
 
-      deep_merge.call(result, arg)
+      result = deep_merge.call(result, arg)
     end
     return( result )
   end
index fffb7f798a74714759eebd195aac7298ced0d7d6..9623fd66f385b2c2c85e9e5a8246ddeece866504 100644 (file)
@@ -73,5 +73,33 @@ describe Puppet::Parser::Functions.function(:deep_merge) do
       hash['key1'].should == { 'a' => 1, 'b' => 99 }
       hash['key2'].should == { 'c' => 3 }
     end
+
+    it 'should not change the original hashes' do
+      hash1 = {'one' => { 'two' => 2 } }
+      hash2 = { 'one' => { 'three' => 3 } }
+      hash = scope.function_deep_merge([hash1, hash2])
+      hash1.should == {'one' => { 'two' => 2 } }
+      hash2.should == { 'one' => { 'three' => 3 } }
+      hash['one'].should == { 'two' => 2, 'three' => 3 }
+    end
+
+    it 'should not change the original hashes 2' do
+      hash1 = {'one' => { 'two' => [1,2] } }
+      hash2 = { 'one' => { 'three' => 3 } }
+      hash = scope.function_deep_merge([hash1, hash2])
+      hash1.should == {'one' => { 'two' => [1,2] } }
+      hash2.should == { 'one' => { 'three' => 3 } }
+      hash['one'].should == { 'two' => [1,2], 'three' => 3 }
+    end
+
+    it 'should not change the original hashes 3' do
+      hash1 = {'one' => { 'two' => [1,2, {'two' => 2} ] } }
+      hash2 = { 'one' => { 'three' => 3 } }
+      hash = scope.function_deep_merge([hash1, hash2])
+      hash1.should == {'one' => { 'two' => [1,2, {'two' => 2}] } }
+      hash2.should == { 'one' => { 'three' => 3 } }
+      hash['one'].should == { 'two' => [1,2, {'two' => 2} ], 'three' => 3 }
+      hash['one']['two'].should == [1,2, {'two' => 2}]
+    end
   end
 end