]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
(#20681) fix behaviour of delete_values
authorLeonardo Rodrigues de Mello <l@lmello.eu.org>
Mon, 16 Sep 2013 13:33:58 +0000 (10:33 -0300)
committerLeonardo Rodrigues de Mello <l@lmello.eu.org>
Tue, 17 Sep 2013 15:05:23 +0000 (12:05 -0300)
The issue #20681 describe the error of delete() function
removing the elements from the origin array/hash/string.

This issue affected other delete functions. Because
ruby delete and delete_if functions make destructive
changes to the origin array/hash.

The delete_undef_values removed elements from the
origin hash and this is not the desired behaviour.

To solve this, we should dup or clone the hash
before using the delete or delete_if ruby functions.

This fix the problem and add unit tests, so we could
enforce this behaviour and prevent regressions.

lib/puppet/parser/functions/delete_values.rb
spec/unit/puppet/parser/functions/delete_values_spec.rb

index 17b9d375e9cc9bfc80644496da9bf5e0f8412c0e..ca8eef576c47da9032013d9f565946cd977d9ec4 100644 (file)
@@ -21,6 +21,6 @@ Would return: {'a'=>'A','c'=>'C','B'=>'D'}
       raise(TypeError, "delete_values(): First argument must be a Hash. " + \
                        "Given an argument of class #{hash.class}.") 
     end
-    hash.delete_if { |key, val| item == val }
+    hash.dup.delete_if { |key, val| item == val }
   end
 end
index c62e55f395a9f650862f944f8c68cee52cf21631..180cc3021c0f91681b6bf12c1bf1c1e44e6ba0c0 100644 (file)
@@ -27,4 +27,10 @@ describe "the delete_values function" do
     result.should(eq({ 'a'=>'A', 'B'=>'C' }))
   end
 
+  it "should not change origin hash passed as argument" do 
+    origin_hash = { 'a' => 1, 'b' => 2, 'c' => 3 } 
+    result = scope.function_delete_values([origin_hash, 2])
+    origin_hash.should(eq({ 'a' => 1, 'b' => 2, 'c' => 3 }))
+  end 
+
 end