summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown4
-rw-r--r--lib/facter/service_provider.rb3
-rw-r--r--lib/puppet/parser/functions/empty.rb12
-rwxr-xr-xspec/acceptance/empty_spec.rb14
-rwxr-xr-xspec/functions/empty_spec.rb2
5 files changed, 28 insertions, 7 deletions
diff --git a/README.markdown b/README.markdown
index 27dd0a7..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`
@@ -405,7 +405,7 @@ Returns an array an intersection of two. For example, `intersection(["a","b","c"
#### `is_a`
-Boolean check to determine whether a variable is of a given data type. This is equivalent to the `=~` type checks.
+Boolean check to determine whether a variable is of a given data type. This is equivalent to the `=~` type checks. This function is only available in Puppet 4, or when using the "future" parser.
~~~
foo = 3
diff --git a/lib/facter/service_provider.rb b/lib/facter/service_provider.rb
index 54db937..a117921 100644
--- a/lib/facter/service_provider.rb
+++ b/lib/facter/service_provider.rb
@@ -7,6 +7,9 @@
#
# Caveats:
#
+require 'puppet/type'
+require 'puppet/type/service'
+
Facter.add(:service_provider) do
setcode do
Puppet::Type.type(:service).newservice(:name => 'dummy')[:provider].to_s
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) }