diff options
author | Jeff McCune <jeff@puppetlabs.com> | 2012-03-07 15:01:11 -0800 |
---|---|---|
committer | Jeff McCune <jeff@puppetlabs.com> | 2012-03-07 15:33:27 -0800 |
commit | 41b07232e464f25403a8f9c786ec0061bf6dc40e (patch) | |
tree | 44dccf99e8f7ebbcf3d8a061ee3a467689385d94 /spec/unit/puppet | |
parent | 898ff80fa762dbbe52f50b872dd1bf3b04c254c9 (diff) | |
download | puppet-stdlib-41b07232e464f25403a8f9c786ec0061bf6dc40e.tar.gz puppet-stdlib-41b07232e464f25403a8f9c786ec0061bf6dc40e.tar.bz2 |
(#12357) Add ability to display an error message from validate_re
I've seen a number of times the following error displayed to the end
user:
validate_re(): "" does not match "^true$|^false$" at /p/t/f.pp:40
This is an absolutely horrific error message. I'm to blame for it.
Users stumble over this quite often and they shouldn't have to go read
the code to sort out what's happening.
This patch makes an effort to fix the problem by adding a third,
optional, argument to validate_re(). This third argument will be the
message thrown back in the exception which will be displayed to the end
user.
This sets the stage for nicer error messages coming from modules we
write.
This patch is backwards compatible but is a new feature.
Diffstat (limited to 'spec/unit/puppet')
-rw-r--r-- | spec/unit/puppet/parser/functions/validate_re_spec.rb | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/spec/unit/puppet/parser/functions/validate_re_spec.rb b/spec/unit/puppet/parser/functions/validate_re_spec.rb new file mode 100644 index 0000000..c35ae14 --- /dev/null +++ b/spec/unit/puppet/parser/functions/validate_re_spec.rb @@ -0,0 +1,80 @@ +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:validate_re) do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + let(:scope) do + scope = Puppet::Parser::Scope.new + end + + # The subject of these examplres is the method itself. + subject do + scope.method :function_validate_re + end + + context 'Using Puppet::Parser::Scope.new' do + + describe 'Garbage inputs' do + inputs = [ + [ nil ], + [ [ nil ] ], + [ { 'foo' => 'bar' } ], + [ { } ], + [ '' ], + [ "one", "one", "MSG to User", "4th arg" ], + ] + + inputs.each do |input| + it "validate_re(#{input.inspect}) should fail" do + expect { subject.call [input] }.to raise_error Puppet::ParseError + end + end + end + + describe 'Valid inputs' do + inputs = [ + [ '/full/path/to/something', '^/full' ], + [ '/full/path/to/something', 'full' ], + [ '/full/path/to/something', ['full', 'absent'] ], + [ '/full/path/to/something', ['full', 'absent'], 'Message to the user' ], + ] + + inputs.each do |input| + it "validate_re(#{input.inspect}) should not fail" do + expect { subject.call input }.not_to raise_error + end + end + end + describe "Valid inputs which should raise an exception without a message" do + # The intent here is to make sure valid inputs raise exceptions when they + # don't specify an error message to display. This is the behvior in + # 2.2.x and prior. + inputs = [ + [ "hello", [ "bye", "later", "adios" ] ], + [ "greetings", "salutations" ], + ] + + inputs.each do |input| + it "validate_re(#{input.inspect}) should fail" do + expect { subject.call input }.to raise_error /validate_re.*?does not match/ + end + end + end + describe "Nicer Error Messages" do + # The intent here is to make sure the function returns the 3rd argument + # in the exception thrown + inputs = [ + [ "hello", [ "bye", "later", "adios" ], "MSG to User" ], + [ "greetings", "salutations", "Error, greetings does not match salutations" ], + ] + + inputs.each do |input| + it "validate_re(#{input.inspect}) should fail" do + expect { subject.call input }.to raise_error /#{input[2]}/ + end + end + end + end +end |