From 6f1d164da6fca26d41d5962c575900dfc792f004 Mon Sep 17 00:00:00 2001 From: Roman Mueller Date: Tue, 22 Sep 2015 18:05:37 +0200 Subject: [PATCH 1/2] Check for numeric values as empty fails on those --- lib/puppet/parser/functions/empty.rb | 12 ++++++++---- spec/functions/empty_spec.rb | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/puppet/parser/functions/empty.rb b/lib/puppet/parser/functions/empty.rb index cca620fae..b5a3cdea4 100644 --- a/lib/puppet/parser/functions/empty.rb +++ b/lib/puppet/parser/functions/empty.rb @@ -13,14 +13,18 @@ module Puppet::Parser::Functions 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/functions/empty_spec.rb b/spec/functions/empty_spec.rb index 94b1c6856..a3a25d6a0 100755 --- a/spec/functions/empty_spec.rb +++ b/spec/functions/empty_spec.rb @@ -3,11 +3,11 @@ 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) } From c7c4d41a8286ab05af65afc1207c8cea4c11ffff Mon Sep 17 00:00:00 2001 From: Helen Campbell Date: Mon, 28 Sep 2015 16:18:56 +0100 Subject: [PATCH 2/2] Added acceptance test and updated readme --- README.markdown | 2 +- spec/acceptance/empty_spec.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index f91932365..d023f2312 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/spec/acceptance/empty_spec.rb b/spec/acceptance/empty_spec.rb index 8b46aacda..2d4df901b 100755 --- a/spec/acceptance/empty_spec.rb +++ b/spec/acceptance/empty_spec.rb @@ -27,6 +27,20 @@ } EOS + apply_manifest(pp, :catch_failures => true) do |r| + 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