diff options
author | David Schmitt <david.schmitt@puppet.com> | 2016-10-04 13:48:30 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-04 13:48:30 +0100 |
commit | 440f98b45a79528c36c3286de70f5e8a2fe4466a (patch) | |
tree | 3222c9b0969de0df0d28b8e423f16440243e11cc | |
parent | 7228160175a17fb3d7f5b464553ef6c7791cf140 (diff) | |
parent | cce67b42bb6d09d4e9773b64e28c07d8a93ed088 (diff) |
Merge pull request #662 from domcleal/validate-string-undef
Permit undef passed as `nil` to validate_string
-rw-r--r-- | lib/puppet/parser/functions/validate_string.rb | 2 | ||||
-rwxr-xr-x | spec/acceptance/is_string_spec.rb | 11 | ||||
-rwxr-xr-x | spec/acceptance/validate_string_spec.rb | 7 | ||||
-rw-r--r-- | spec/aliases/string_spec.rb | 3 | ||||
-rwxr-xr-x | spec/functions/validate_string_spec.rb | 1 | ||||
-rw-r--r-- | types/compat/string.pp | 2 |
6 files changed, 23 insertions, 3 deletions
diff --git a/lib/puppet/parser/functions/validate_string.rb b/lib/puppet/parser/functions/validate_string.rb index e92a2fc..0057fc1 100644 --- a/lib/puppet/parser/functions/validate_string.rb +++ b/lib/puppet/parser/functions/validate_string.rb @@ -30,7 +30,7 @@ module Puppet::Parser::Functions end args.each do |arg| - unless arg.is_a?(String) + unless arg.is_a?(String) || arg.nil? raise Puppet::ParseError, ("#{arg.inspect} is not a string. It looks to be a #{arg.class}") end end diff --git a/spec/acceptance/is_string_spec.rb b/spec/acceptance/is_string_spec.rb index 94d8e96..f526888 100755 --- a/spec/acceptance/is_string_spec.rb +++ b/spec/acceptance/is_string_spec.rb @@ -95,6 +95,17 @@ describe 'is_string function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('o expect(r.stdout).to match(/Notice: output correct/) end end + it 'is_strings undef' do + pp = <<-EOS + $a = undef + $o = is_string($a) + notice(inline_template('is_string is <%= @o.inspect %>')) + EOS + + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(/is_string is true/) + end + end end describe 'failure' do it 'handles improper argument counts' diff --git a/spec/acceptance/validate_string_spec.rb b/spec/acceptance/validate_string_spec.rb index 8956f48..ae3468f 100755 --- a/spec/acceptance/validate_string_spec.rb +++ b/spec/acceptance/validate_string_spec.rb @@ -20,6 +20,13 @@ describe 'validate_string function', :unless => UNSUPPORTED_PLATFORMS.include?(f apply_manifest(pp, :catch_failures => true) end + it 'validates undef' do + pp = <<-EOS + validate_string(undef) + EOS + + apply_manifest(pp, :catch_failures => true) + end it 'validates a non-string' do { %{validate_string({ 'a' => 'hash' })} => "Hash", diff --git a/spec/aliases/string_spec.rb b/spec/aliases/string_spec.rb index 8e01d55..853b5af 100644 --- a/spec/aliases/string_spec.rb +++ b/spec/aliases/string_spec.rb @@ -6,6 +6,7 @@ if Puppet.version.to_f >= 4.0 [ '', 'one', + nil, ].each do |value| describe value.inspect do let(:params) {{ value: value }} @@ -23,7 +24,7 @@ if Puppet.version.to_f >= 4.0 ].each do |value| describe value.inspect do let(:params) {{ value: value }} - it { is_expected.to compile.and_raise_error(/parameter 'value' expects a Stdlib::Compat::String = String/) } + it { is_expected.to compile.and_raise_error(/parameter 'value' expects a String/) } end end end diff --git a/spec/functions/validate_string_spec.rb b/spec/functions/validate_string_spec.rb index b5ce763..0907ede 100755 --- a/spec/functions/validate_string_spec.rb +++ b/spec/functions/validate_string_spec.rb @@ -18,6 +18,7 @@ describe 'validate_string' do describe 'valid inputs' do it { is_expected.to run.with_params('') } + it { is_expected.to run.with_params(nil) } it { is_expected.to run.with_params('one') } it { is_expected.to run.with_params('one', 'two') } end diff --git a/types/compat/string.pp b/types/compat/string.pp index 4c36e5f..b06255d 100644 --- a/types/compat/string.pp +++ b/types/compat/string.pp @@ -1,2 +1,2 @@ # Emulate the is_string and validate_string functions -type Stdlib::Compat::String = String +type Stdlib::Compat::String = Optional[String] |