Skip to content

Commit e0eadf2

Browse files
committed
Merge pull request #47 from jeffmccune/feature/2.3.x/validate_re_better_error_messages
(#12357) Add ability to display an error message from validate_re
2 parents 898ff80 + 41b0723 commit e0eadf2

2 files changed

Lines changed: 90 additions & 5 deletions

File tree

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module Puppet::Parser::Functions
2-
32
newfunction(:validate_re, :doc => <<-'ENDHEREDOC') do |args|
43
Perform simple validation of a string against one or more regular
54
expressions. The first argument of this function should be a string to
@@ -8,6 +7,9 @@ module Puppet::Parser::Functions
87
of the regular expressions match the string passed in, compilation will
98
abort with a parse error.
109
10+
If a third argument is specified, this will be the error message raised and
11+
seen by the user.
12+
1113
The following strings will validate against the regular expressions:
1214
1315
validate_re('one', '^one$')
@@ -17,17 +19,20 @@ module Puppet::Parser::Functions
1719
1820
validate_re('one', [ '^two', '^three' ])
1921
22+
A helpful error message can be returned like this:
23+
24+
validate_re($::puppetversion, '^2.7', 'The $puppetversion fact value does not match 2.7')
25+
2026
ENDHEREDOC
21-
if args.length != 2 then
22-
raise Puppet::ParseError, ("validate_re(): wrong number of arguments (#{args.length}; must be 2)")
27+
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)")
2329
end
2430

25-
msg = "validate_re(): #{args[0].inspect} does not match #{args[1].inspect}"
31+
msg = args[2] || "validate_re(): #{args[0].inspect} does not match #{args[1].inspect}"
2632

2733
raise Puppet::ParseError, (msg) unless args[1].any? do |re_str|
2834
args[0] =~ Regexp.compile(re_str)
2935
end
3036

3137
end
32-
3338
end
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
require 'spec_helper'
2+
3+
describe Puppet::Parser::Functions.function(:validate_re) do
4+
before :all do
5+
Puppet::Parser::Functions.autoloader.loadall
6+
end
7+
8+
let(:scope) do
9+
scope = Puppet::Parser::Scope.new
10+
end
11+
12+
# The subject of these examplres is the method itself.
13+
subject do
14+
scope.method :function_validate_re
15+
end
16+
17+
context 'Using Puppet::Parser::Scope.new' do
18+
19+
describe 'Garbage inputs' do
20+
inputs = [
21+
[ nil ],
22+
[ [ nil ] ],
23+
[ { 'foo' => 'bar' } ],
24+
[ { } ],
25+
[ '' ],
26+
[ "one", "one", "MSG to User", "4th arg" ],
27+
]
28+
29+
inputs.each do |input|
30+
it "validate_re(#{input.inspect}) should fail" do
31+
expect { subject.call [input] }.to raise_error Puppet::ParseError
32+
end
33+
end
34+
end
35+
36+
describe 'Valid inputs' do
37+
inputs = [
38+
[ '/full/path/to/something', '^/full' ],
39+
[ '/full/path/to/something', 'full' ],
40+
[ '/full/path/to/something', ['full', 'absent'] ],
41+
[ '/full/path/to/something', ['full', 'absent'], 'Message to the user' ],
42+
]
43+
44+
inputs.each do |input|
45+
it "validate_re(#{input.inspect}) should not fail" do
46+
expect { subject.call input }.not_to raise_error
47+
end
48+
end
49+
end
50+
describe "Valid inputs which should raise an exception without a message" do
51+
# The intent here is to make sure valid inputs raise exceptions when they
52+
# don't specify an error message to display. This is the behvior in
53+
# 2.2.x and prior.
54+
inputs = [
55+
[ "hello", [ "bye", "later", "adios" ] ],
56+
[ "greetings", "salutations" ],
57+
]
58+
59+
inputs.each do |input|
60+
it "validate_re(#{input.inspect}) should fail" do
61+
expect { subject.call input }.to raise_error /validate_re.*?does not match/
62+
end
63+
end
64+
end
65+
describe "Nicer Error Messages" do
66+
# The intent here is to make sure the function returns the 3rd argument
67+
# in the exception thrown
68+
inputs = [
69+
[ "hello", [ "bye", "later", "adios" ], "MSG to User" ],
70+
[ "greetings", "salutations", "Error, greetings does not match salutations" ],
71+
]
72+
73+
inputs.each do |input|
74+
it "validate_re(#{input.inspect}) should fail" do
75+
expect { subject.call input }.to raise_error /#{input[2]}/
76+
end
77+
end
78+
end
79+
end
80+
end

0 commit comments

Comments
 (0)