Skip to content

Commit cf517c6

Browse files
committed
Permit undef passed as nil to validate_string
When validate_string is called via the Puppet 4 deprecation wrappers from deprecation_gen (introduced in 970852d), `undef` is passed as `nil` where it was previously passed as `''` from the Puppet 3-style function API. This change explicitly permits a `nil` value in validate_string, and adds a test case to `is_string` which also accepts the same. Fixes test failures in apt, concat etc: Error while evaluating a Function Call, nil is not a string. It looks to be a NilClass at apt/manifests/source.pp:23:3 [..] # ./spec/fixtures/modules/stdlib/lib/puppet/parser/functions/validate_string.rb:34:in `block (2 levels) in <module:Functions>' # ./spec/fixtures/modules/stdlib/lib/puppet/parser/functions/validate_string.rb:32:in `each' # ./spec/fixtures/modules/stdlib/lib/puppet/parser/functions/validate_string.rb:32:in `block in <module:Functions>' # puppet-4.7.0/lib/puppet/parser/functions.rb:174:in `block (2 levels) in newfunction' # puppet-4.7.0/lib/puppet/util/profiler/around_profiler.rb:58:in `profile' # puppet-4.7.0/lib/puppet/util/profiler.rb:51:in `profile' # puppet-4.7.0/lib/puppet/parser/functions.rb:167:in `block in newfunction' # ./spec/fixtures/modules/stdlib/lib/puppet_x/puppetlabs/stdlib/deprecation_gen.rb:13:in `block (2 levels) in deprecation_gen'
1 parent 7228160 commit cf517c6

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

lib/puppet/parser/functions/validate_string.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Puppet::Parser::Functions
3030
end
3131

3232
args.each do |arg|
33-
unless arg.is_a?(String)
33+
unless arg.is_a?(String) || arg.nil?
3434
raise Puppet::ParseError, ("#{arg.inspect} is not a string. It looks to be a #{arg.class}")
3535
end
3636
end

spec/acceptance/is_string_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@
9595
expect(r.stdout).to match(/Notice: output correct/)
9696
end
9797
end
98+
it 'is_strings undef' do
99+
pp = <<-EOS
100+
$a = undef
101+
$o = is_string($a)
102+
notice(inline_template('is_string is <%= @o.inspect %>'))
103+
EOS
104+
105+
apply_manifest(pp, :catch_failures => true) do |r|
106+
expect(r.stdout).to match(/is_string is true/)
107+
end
108+
end
98109
end
99110
describe 'failure' do
100111
it 'handles improper argument counts'

spec/acceptance/validate_string_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020

2121
apply_manifest(pp, :catch_failures => true)
2222
end
23+
it 'validates undef' do
24+
pp = <<-EOS
25+
validate_string(undef)
26+
EOS
27+
28+
apply_manifest(pp, :catch_failures => true)
29+
end
2330
it 'validates a non-string' do
2431
{
2532
%{validate_string({ 'a' => 'hash' })} => "Hash",

spec/functions/validate_string_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
describe 'valid inputs' do
2020
it { is_expected.to run.with_params('') }
21+
it { is_expected.to run.with_params(nil) }
2122
it { is_expected.to run.with_params('one') }
2223
it { is_expected.to run.with_params('one', 'two') }
2324
end

types/compat/string.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# Emulate the is_string and validate_string functions
2-
type Stdlib::Compat::String = String
2+
type Stdlib::Compat::String = Optional[String]

0 commit comments

Comments
 (0)