summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Schmitt <david.schmitt@puppetlabs.com>2015-09-28 17:11:10 +0100
committerDavid Schmitt <david.schmitt@puppetlabs.com>2015-09-28 17:11:10 +0100
commit76db98120acab12d5aab3adaff6d2aa1d970c1e8 (patch)
tree00140d6f633d63232a3408d85d7e5a42b497b457
parent48b658fc1c948103eaf04cd10671dd1a6fd61b8c (diff)
parentc7c4d41a8286ab05af65afc1207c8cea4c11ffff (diff)
downloadpuppet-stdlib-76db98120acab12d5aab3adaff6d2aa1d970c1e8.tar.gz
puppet-stdlib-76db98120acab12d5aab3adaff6d2aa1d970c1e8.tar.bz2
Merge pull request #533 from HelenCampbell/MODULES-2614-Improved
Modules 2614 improved numeric value handling on empty function
-rw-r--r--README.markdown2
-rw-r--r--lib/puppet/parser/functions/empty.rb12
-rwxr-xr-xspec/acceptance/empty_spec.rb14
-rwxr-xr-xspec/functions/empty_spec.rb2
4 files changed, 24 insertions, 6 deletions
diff --git a/README.markdown b/README.markdown
index f919323..d023f23 100644
--- a/README.markdown
+++ b/README.markdown
@@ -224,7 +224,7 @@ Converts the case of a string or of all strings in an array to lowercase. *Type*
#### `empty`
-Returns true if the argument is an array or hash that contains no elements, or an empty string. *Type*: rvalue.
+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.
#### `ensure_packages`
diff --git a/lib/puppet/parser/functions/empty.rb b/lib/puppet/parser/functions/empty.rb
index cca620f..b5a3cde 100644
--- a/lib/puppet/parser/functions/empty.rb
+++ b/lib/puppet/parser/functions/empty.rb
@@ -13,14 +13,18 @@ Returns true if the variable is empty.
value = arguments[0]
- unless value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(String)
+ unless value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(String) || value.is_a?(Numeric)
raise(Puppet::ParseError, 'empty(): Requires either ' +
- 'array, hash or string to work with')
+ 'array, hash, string or integer to work with')
end
- result = value.empty?
+ if value.is_a?(Numeric)
+ return false
+ else
+ result = value.empty?
- return result
+ return result
+ end
end
end
diff --git a/spec/acceptance/empty_spec.rb b/spec/acceptance/empty_spec.rb
index 8b46aac..2d4df90 100755
--- a/spec/acceptance/empty_spec.rb
+++ b/spec/acceptance/empty_spec.rb
@@ -31,6 +31,20 @@ describe 'empty function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('opera
expect(r.stdout).to match(/Notice: output correct/)
end
end
+ it 'handles numerical values' do
+ pp = <<-EOS
+ $a = 7
+ $b = false
+ $o = empty($a)
+ if $o == $b {
+ notify { 'output correct': }
+ }
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/Notice: output correct/)
+ end
+ end
end
describe 'failure' do
it 'handles improper argument counts'
diff --git a/spec/functions/empty_spec.rb b/spec/functions/empty_spec.rb
index 94b1c68..a3a25d6 100755
--- a/spec/functions/empty_spec.rb
+++ b/spec/functions/empty_spec.rb
@@ -3,11 +3,11 @@ require 'spec_helper'
describe 'empty' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
- it { is_expected.to run.with_params(0).and_raise_error(Puppet::ParseError) }
it {
pending("Current implementation ignores parameters after the first.")
is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError)
}
+ it { is_expected.to run.with_params(0).and_return(false) }
it { is_expected.to run.with_params('').and_return(true) }
it { is_expected.to run.with_params('one').and_return(false) }