From c12e9afc97f4356bc20554d32861889680c5fdc1 Mon Sep 17 00:00:00 2001
From: Justin Burnham <jburnham@mediatemple.net>
Date: Mon, 17 Feb 2014 11:46:55 -0800
Subject: PUP-1724 Don't modify the paramaters to deep_merge

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.
---
 .../puppet/parser/functions/deep_merge_spec.rb     | 28 ++++++++++++++++++++++
 1 file changed, 28 insertions(+)

(limited to 'spec/unit/puppet')

diff --git a/spec/unit/puppet/parser/functions/deep_merge_spec.rb b/spec/unit/puppet/parser/functions/deep_merge_spec.rb
index fffb7f7..9623fd6 100644
--- a/spec/unit/puppet/parser/functions/deep_merge_spec.rb
+++ b/spec/unit/puppet/parser/functions/deep_merge_spec.rb
@@ -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
-- 
cgit v1.2.3