Skip to content

Commit c14cbf3

Browse files
committed
bug # 20681 delete() function should not remove elements from original list
The setup: list with 3 elements, delete one: $test_list = [‘a’, ‘b’, ‘c’] $test_deleted = delete($test_list, ‘a’) Print out the elements in ‘test_deleted’: notify { ‘group_output2’: withpath => true, name => “$cfeng::test_deleted”, } Notice: /Stage[main]/Syslog/Notify[group_output2]/message: bc Good! Run-on output shows that ‘a’ was deleted Print out the elements in ‘test_list’: notify { ‘group_output1’: withpath => true, name => “$cfeng::test_list”, } Notice: /Stage[main]/Syslog/Notify[group_output1]/message: bc WHAT!? 'a' was deleted from ‘test_list’ as well! Expected abc as output! This behaviour is confirmed for string, hash and array. This is fixed on this commit, I had added two spec tests to cover that cases. bug #20681 spec test for delete() function. I had forgot in the last commit the spec test for hash in the delete function. bug # 20681 delete() function change aproach. Instead of rejecting elements from the original list, we use collection = arguments[0].dup . then latter we could continue to use delete and gsub! on collection without impact on original argument. this is a better solution than the previous one, and works on ruby 1.8.7, 1.9.3 and 2.0.0. The previous solution does not work on ruby 1.8.7. delete function remove typo whitespace. fix typo whitespaces.
1 parent 8064302 commit c14cbf3

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/puppet/parser/functions/delete.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module Puppet::Parser::Functions
2727
"given #{arguments.size} for 2.")
2828
end
2929

30-
collection = arguments[0]
30+
collection = arguments[0].dup
3131
item = arguments[1]
3232

3333
case collection

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,22 @@
3535
result.should(eq({ 'a' => 1, 'c' => 3 }))
3636
end
3737

38+
it "should not change origin array passed as argument" do
39+
origin_array = ['a','b','c','d']
40+
result = scope.function_delete([origin_array, 'b'])
41+
origin_array.should(eq(['a','b','c','d']))
42+
end
43+
44+
it "should not change the origin string passed as argument" do
45+
origin_string = 'foobarbabarz'
46+
result = scope.function_delete([origin_string,'bar'])
47+
origin_string.should(eq('foobarbabarz'))
48+
end
49+
50+
it "should not change origin hash passed as argument" do
51+
origin_hash = { 'a' => 1, 'b' => 2, 'c' => 3 }
52+
result = scope.function_delete([origin_hash, 'b'])
53+
origin_hash.should(eq({ 'a' => 1, 'b' => 2, 'c' => 3 }))
54+
end
55+
3856
end

0 commit comments

Comments
 (0)