Skip to content

Commit 51d9608

Browse files
committed
(#20681) fix behaviour of delete_values
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.
1 parent 8064302 commit 51d9608

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/puppet/parser/functions/delete_values.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ module Puppet::Parser::Functions
2121
raise(TypeError, "delete_values(): First argument must be a Hash. " + \
2222
"Given an argument of class #{hash.class}.")
2323
end
24-
hash.delete_if { |key, val| item == val }
24+
hash.dup.delete_if { |key, val| item == val }
2525
end
2626
end

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@
2727
result.should(eq({ 'a'=>'A', 'B'=>'C' }))
2828
end
2929

30+
it "should not change origin hash passed as argument" do
31+
origin_hash = { 'a' => 1, 'b' => 2, 'c' => 3 }
32+
result = scope.function_delete_values([origin_hash, 2])
33+
origin_hash.should(eq({ 'a' => 1, 'b' => 2, 'c' => 3 }))
34+
end
35+
3036
end

0 commit comments

Comments
 (0)