Skip to content

Commit 55ece78

Browse files
committed
(MAINT) validate_re: Clarify docs and error message
1 parent 399ce03 commit 55ece78

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

README.markdown

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,13 @@ test, and the second argument should be a stringified regular expression (withou
10541054
validate_re($::puppetversion, '^2.7', 'The $puppetversion fact value does not match 2.7')
10551055
~~~
10561056

1057+
Note: Compilation will also abort, if the first argument is not a String. Always use
1058+
quotes to force stringification:
1059+
1060+
~~~
1061+
validate_re("${::operatingsystemmajrelease}", '^[57]$')
1062+
~~~
1063+
10571064
*Type*: statement.
10581065

10591066
#### `validate_slength`

lib/puppet/parser/functions/validate_re.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,23 @@ module Puppet::Parser::Functions
2323
2424
validate_re($::puppetversion, '^2.7', 'The $puppetversion fact value does not match 2.7')
2525
26+
Note: Compilation will also abort, if the first argument is not a String. Always use
27+
quotes to force stringification:
28+
29+
validate_re("${::operatingsystemmajrelease}", '^[57]$')
30+
2631
ENDHEREDOC
2732
if (args.length < 2) or (args.length > 3) then
28-
raise Puppet::ParseError, ("validate_re(): wrong number of arguments (#{args.length}; must be 2 or 3)")
33+
raise Puppet::ParseError, "validate_re(): wrong number of arguments (#{args.length}; must be 2 or 3)"
2934
end
3035

36+
raise Puppet::ParseError, "validate_re(): input needs to be a String, not a #{args[0].class}" unless args[0].is_a? String
37+
3138
msg = args[2] || "validate_re(): #{args[0].inspect} does not match #{args[1].inspect}"
3239

3340
# We're using a flattened array here because we can't call String#any? in
3441
# Ruby 1.9 like we can in Ruby 1.8
35-
raise Puppet::ParseError, (msg) unless [args[1]].flatten.any? do |re_str|
42+
raise Puppet::ParseError, msg unless [args[1]].flatten.any? do |re_str|
3643
args[0] =~ Regexp.compile(re_str)
3744
end
3845

spec/functions/validate_re_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@
4141
it { is_expected.to run.with_params('notone', '^one').and_raise_error(Puppet::ParseError, /does not match/) }
4242
it { is_expected.to run.with_params('notone', [ '^one', '^two' ]).and_raise_error(Puppet::ParseError, /does not match/) }
4343
it { is_expected.to run.with_params('notone', [ '^one', '^two' ], 'custom error').and_raise_error(Puppet::ParseError, /custom error/) }
44+
45+
describe 'non-string inputs' do
46+
[
47+
1, # Fixnum
48+
3.14, # Float
49+
nil, # NilClass
50+
true, # TrueClass
51+
false, # FalseClass
52+
["10"], # Array
53+
:key, # Symbol
54+
{:key=>"val"}, # Hash
55+
].each do |input|
56+
it { is_expected.to run.with_params(input, '.*').and_raise_error(Puppet::ParseError, /needs to be a String/) }
57+
end
58+
end
4459
end
4560
end
4661
end

0 commit comments

Comments
 (0)