Skip to content

delete_undef_values function fix bug #20681 #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/puppet/parser/functions/delete_undef_values.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ module Puppet::Parser::Functions
raise(Puppet::ParseError,
"delete_undef_values(): Wrong number of arguments given " +
"(#{args.size})") if args.size < 1

result = args[0]

unless args[0].is_a? Array or args[0].is_a? Hash
raise(Puppet::ParseError,
"delete_undef_values(): expected an array or hash, got #{args[0]} type #{args[0].class} ")
end
result = args[0].dup
if result.is_a?(Hash)
result.delete_if {|key, val| val.equal? :undef}
elsif result.is_a?(Array)
result.delete :undef
else
raise(Puppet::ParseError,
"delete_undef_values(): Wrong argument type #{args[0].class} " +
"for first argument")
end
result
end
Expand Down
12 changes: 12 additions & 0 deletions spec/unit/puppet/parser/functions/delete_undef_values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@
result = scope.function_delete_undef_values([{'a'=>'A','b'=>:undef,'c'=>'C','d'=>'undef'}])
result.should(eq({'a'=>'A','c'=>'C','d'=>'undef'}))
end

it "should not change origin array passed as argument" do
origin_array = ['a',:undef,'c','undef']
result = scope.function_delete_undef_values([origin_array])
origin_array.should(eq(['a',:undef,'c','undef']))
end

it "should not change origin hash passed as argument" do
origin_hash = { 'a' => 1, 'b' => :undef, 'c' => 'undef' }
result = scope.function_delete_undef_values([origin_hash])
origin_hash.should(eq({ 'a' => 1, 'b' => :undef, 'c' => 'undef' }))
end
end