puppetlabs-stdlib/spec/unit/puppet/parser/functions/validate_re_spec.rb
Jeff McCune 98ff3abd09 (Maint) use PuppetlabsSpec::PuppetSeams.parser_scope (2.3.x)
This patch is the same approach as the one that want into 2.2.x.  It
covers the functions in 2.3.x that do not exist in 2.2.x.

Without this patch all of the spec tests for parser functions in stdlib
would instantiate their own scope instances.  This is a problem because
the standard library is tightly coupled with the internal behavior of
Puppet.  Tight coupling like this creates failures when we change the
internal behavior of Puppet.  This is exactly what happened recently
when we changed the method signature for the initializer of
Puppet::Parser::Scope instances.

This patch fixes the problem by creating scope instances using the
puppet labs spec helper.  The specific method that provides scope
instances in Puppet-version-independent way is something like this:

    let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

This patch simply implements this across the board.
2012-07-23 09:13:08 -07:00

74 lines
2.2 KiB
Ruby

require 'spec_helper'
describe Puppet::Parser::Functions.function(:validate_re) do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
# 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