Skip to content

Commit 76db981

Browse files
committed
Merge pull request #533 from HelenCampbell/MODULES-2614-Improved
Modules 2614 improved numeric value handling on empty function
2 parents 48b658f + c7c4d41 commit 76db981

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ Converts the case of a string or of all strings in an array to lowercase. *Type*
224224

225225
#### `empty`
226226

227-
Returns true if the argument is an array or hash that contains no elements, or an empty string. *Type*: rvalue.
227+
Returns true if the argument is an array or hash that contains no elements, or an empty string. Returns false when the argument is a numerical value. *Type*: rvalue.
228228

229229
#### `ensure_packages`
230230

lib/puppet/parser/functions/empty.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ module Puppet::Parser::Functions
1313

1414
value = arguments[0]
1515

16-
unless value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(String)
16+
unless value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(String) || value.is_a?(Numeric)
1717
raise(Puppet::ParseError, 'empty(): Requires either ' +
18-
'array, hash or string to work with')
18+
'array, hash, string or integer to work with')
1919
end
2020

21-
result = value.empty?
21+
if value.is_a?(Numeric)
22+
return false
23+
else
24+
result = value.empty?
2225

23-
return result
26+
return result
27+
end
2428
end
2529
end
2630

spec/acceptance/empty_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@
2727
}
2828
EOS
2929

30+
apply_manifest(pp, :catch_failures => true) do |r|
31+
expect(r.stdout).to match(/Notice: output correct/)
32+
end
33+
end
34+
it 'handles numerical values' do
35+
pp = <<-EOS
36+
$a = 7
37+
$b = false
38+
$o = empty($a)
39+
if $o == $b {
40+
notify { 'output correct': }
41+
}
42+
EOS
43+
3044
apply_manifest(pp, :catch_failures => true) do |r|
3145
expect(r.stdout).to match(/Notice: output correct/)
3246
end

spec/functions/empty_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
describe 'empty' do
44
it { is_expected.not_to eq(nil) }
55
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
6-
it { is_expected.to run.with_params(0).and_raise_error(Puppet::ParseError) }
76
it {
87
pending("Current implementation ignores parameters after the first.")
98
is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError)
109
}
10+
it { is_expected.to run.with_params(0).and_return(false) }
1111
it { is_expected.to run.with_params('').and_return(true) }
1212
it { is_expected.to run.with_params('one').and_return(false) }
1313

0 commit comments

Comments
 (0)