Skip to content

Commit dce8d7b

Browse files
authored
Merge pull request #649 from DavidS/modules-3737
(MODULES-3737) refactor validate_legacy and tests
2 parents d350ee4 + f1edd27 commit dce8d7b

7 files changed

Lines changed: 97 additions & 107 deletions

File tree

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ group :development, :unit_tests do
2121
gem 'puppet_facts'
2222
gem 'puppet-blacksmith', '>= 3.4.0'
2323
gem 'puppetlabs_spec_helper', '>= 1.2.1'
24-
gem 'rspec-puppet', '>= 2.3.2'
24+
gem 'rspec-puppet', '>= 2.3.2', :git => 'https://github.com/rodjek/rspec-puppet.git', :branch => 'fb27c533e2664057fba4b73d0bd902a946abfce0'
2525
gem 'rspec-puppet-facts'
2626
gem 'simplecov'
2727
gem 'parallel_tests'

lib/puppet/functions/validate_legacy.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,49 @@
22
# The function checks a value against both the target_type (new) and the previous_validation function (old).
33

44
dispatch :validate_legacy do
5+
scope_param
56
param 'Type', :target_type
6-
param 'String', :previous_validation
7-
param 'NotUndef', :value
8-
optional_param 'Any', :args
7+
param 'String', :function_name
8+
param 'Any', :value
9+
optional_repeated_param 'Any', :args
910
end
11+
1012
dispatch :validate_legacy_s do
1113
scope_param
1214
param 'String', :type_string
13-
param 'String', :previous_validation
14-
param 'NotUndef', :value
15+
param 'String', :function_name
16+
param 'Any', :value
1517
optional_repeated_param 'Any', :args
1618
end
1719

1820
def validate_legacy_s(scope, type_string, *args)
1921
t = Puppet::Pops::Types::TypeParser.new.parse(type_string, scope)
20-
validate_legacy(t, *args)
22+
validate_legacy(scope, t, *args)
2123
end
2224

23-
def validate_legacy(target_type, previous_validation, value, *prev_args)
25+
def validate_legacy(scope, target_type, function_name, value, *prev_args)
2426
if assert_type(target_type, value)
25-
if previous_validation(previous_validation, value, *prev_args)
27+
if previous_validation(scope, function_name, value, *prev_args)
2628
# Silently passes
2729
else
28-
Puppet.warn("Accepting previously invalid value for target_type '#{target_type}'")
30+
Puppet.notice("Accepting previously invalid value for target type '#{target_type}'")
2931
end
3032
else
3133
caller_infos = caller.first.split(":")
3234
inferred_type = Puppet::Pops::Types::TypeCalculator.infer_set(value)
33-
message = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch(previous_validation, target_type, inferred_type)
34-
error_msg = "#{message} : #{caller_infos[0]} : #{caller_infos[1]}"
35-
if previous_validation(previous_validation, value, *prev_args)
36-
Puppet.warn(error_msg)
35+
error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch("validate_legacy(#{function_name}) [#{caller_infos[0]}:#{caller_infos[1]}]", target_type, inferred_type)
36+
if previous_validation(scope, function_name, value, *prev_args)
37+
call_function('deprecation', 'validate_legacy', error_msg)
3738
else
3839
call_function('fail', error_msg)
3940
end
4041
end
4142
end
4243

43-
def previous_validation(previous_validation, value, *prev_args)
44+
def previous_validation(scope, function_name, value, *prev_args)
4445
# Call the previous validation function and catch any errors. Return true if no errors are thrown.
4546
begin
46-
call_function(previous_validation, value, *prev_args)
47+
scope.send("function_#{function_name}".to_s, [value, *prev_args])
4748
true
4849
rescue Puppet::ParseError
4950
false

spec/classes/validate_legacy_spec.rb

Lines changed: 0 additions & 39 deletions
This file was deleted.

spec/fixtures/test/manifests/validate_legacy.pp

Lines changed: 0 additions & 18 deletions
This file was deleted.

spec/functions/assert_private_spec.rb

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,26 @@
55
it "should not fail" do
66
scope.expects(:lookupvar).with('module_name').returns('foo')
77
scope.expects(:lookupvar).with('caller_module_name').returns('foo')
8-
expect {
9-
subject.call []
10-
}.not_to raise_error
8+
9+
is_expected.to run.with_params()
1110
end
1211
end
1312

14-
context "with an explicit failure message" do
15-
it "prints the failure message on error" do
13+
context "when called from private class" do
14+
before :each do
1615
scope.expects(:lookupvar).with('module_name').returns('foo')
1716
scope.expects(:lookupvar).with('caller_module_name').returns('bar')
18-
expect {
19-
subject.call ['failure message!']
20-
}.to raise_error Puppet::ParseError, /failure message!/
2117
end
22-
end
2318

24-
context "when called from private class" do
2519
it "should fail with a class error message" do
26-
scope.expects(:lookupvar).with('module_name').returns('foo')
27-
scope.expects(:lookupvar).with('caller_module_name').returns('bar')
2820
scope.source.expects(:name).returns('foo::baz')
2921
scope.source.expects(:type).returns('hostclass')
30-
expect {
31-
subject.call []
32-
}.to raise_error Puppet::ParseError, /Class foo::baz is private/
22+
23+
is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /Class foo::baz is private/)
24+
end
25+
26+
context "with an explicit failure message" do
27+
it { is_expected.to run.with_params('failure message!').and_raise_error(Puppet::ParseError, /failure message!/) }
3328
end
3429
end
3530

@@ -39,9 +34,8 @@
3934
scope.expects(:lookupvar).with('caller_module_name').returns('bar')
4035
scope.source.expects(:name).returns('foo::baz')
4136
scope.source.expects(:type).returns('definition')
42-
expect {
43-
subject.call []
44-
}.to raise_error Puppet::ParseError, /Definition foo::baz is private/
37+
38+
is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /Definition foo::baz is private/)
4539
end
4640
end
4741
end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require 'spec_helper'
2+
3+
if Puppet.version.to_f >= 4.0
4+
describe 'validate_legacy' do
5+
it { is_expected.not_to eq(nil) }
6+
it { is_expected.to run.with_params.and_raise_error(ArgumentError) }
7+
8+
describe 'when passing the type assertion and passing the previous validation' do
9+
before do
10+
scope.expects(:function_validate_foo).with([5]).once
11+
Puppet.expects(:notice).never
12+
end
13+
it 'passes without notice' do
14+
is_expected.to run.with_params('Integer', 'validate_foo', 5)
15+
end
16+
end
17+
18+
describe 'when passing the type assertion and failing the previous validation' do
19+
before do
20+
scope.expects(:function_validate_foo).with([5]).raises(Puppet::ParseError, 'foo').once
21+
Puppet.expects(:notice).with(includes('Accepting previously invalid value for target type'))
22+
end
23+
it 'passes with a notice about newly accepted value' do
24+
is_expected.to run.with_params('Integer', 'validate_foo', 5)
25+
end
26+
end
27+
28+
describe 'when failing the type assertion and passing the previous validation' do
29+
before do
30+
scope.expects(:function_validate_foo).with(['5']).once
31+
subject.func.expects(:call_function).with('deprecation', 'validate_legacy', includes('expected an Integer value')).once
32+
end
33+
it 'passes with a deprecation message' do
34+
is_expected.to run.with_params('Integer', 'validate_foo', '5')
35+
end
36+
end
37+
38+
describe 'when failing the type assertion and failing the previous validation' do
39+
before do
40+
scope.expects(:function_validate_foo).with(['5']).raises(Puppet::ParseError, 'foo').once
41+
subject.func.expects(:call_function).with('fail', includes('expected an Integer value')).once
42+
end
43+
it 'fails with a helpful message' do
44+
is_expected.to run.with_params('Integer', 'validate_foo', '5')
45+
end
46+
end
47+
48+
describe 'when passing in undef' do
49+
before do
50+
scope.expects(:function_validate_foo).with([:undef]).once
51+
Puppet.expects(:notice).never
52+
end
53+
it 'works' do
54+
is_expected.to run.with_params('Optional[Integer]', 'validate_foo', :undef)
55+
end
56+
end
57+
58+
describe 'when passing in multiple arguments' do
59+
before do
60+
scope.expects(:function_validate_foo).with([:undef, 1, 'foo']).once
61+
Puppet.expects(:notice).never
62+
end
63+
it 'passes with a deprecation message' do
64+
is_expected.to run.with_params('Optional[Integer]', 'validate_foo', :undef, 1, 'foo')
65+
end
66+
end
67+
end
68+
end

spec/functions/validate_re_spec.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,6 @@
2828
end
2929

3030
describe 'invalid inputs' do
31-
it {
32-
pending('should implement stricter type checking')
33-
is_expected.to run.with_params([], '').and_raise_error(Puppet::ParseError, /is not a String/)
34-
}
35-
it {
36-
pending('should implement stricter type checking')
37-
is_expected.to run.with_params('', {}).and_raise_error(Puppet::ParseError, /is not an Array/)
38-
}
39-
it {
40-
pending('should implement stricter type checking')
41-
is_expected.to run.with_params('', '', []).and_raise_error(Puppet::ParseError, /is not a String/)
42-
}
43-
it {
44-
pending('should implement stricter type checking')
45-
is_expected.to run.with_params(nil, nil).and_raise_error(Puppet::ParseError, /is not a String/)
46-
}
4731
it { is_expected.to run.with_params('', []).and_raise_error(Puppet::ParseError, /does not match/) }
4832
it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /does not match/) }
4933
it { is_expected.to run.with_params('', 'two').and_raise_error(Puppet::ParseError, /does not match/) }

0 commit comments

Comments
 (0)