Skip to content

Commit 5f38f9f

Browse files
author
Helen Campbell
committed
(WIP) Addition of validate legacy function
1 parent 3f98b3c commit 5f38f9f

3 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Puppet::Functions.create_function(:validate_legacy, Puppet::Functions::InternalFunction) do
2+
# The function checks a value against both the target_type (new) and the previous_validation function (old).
3+
4+
dispatch :validate_legacy do
5+
param 'Type', :target_type
6+
param 'String', :previous_validation #Needs to be the name of the function previously used
7+
param 'NotUndef', :value #Really not sure what all this could be
8+
optional_param 'Any', :args # /need special stuff for any number of args herei
9+
end
10+
dispatch :validate_legacy_s do
11+
scope_param
12+
param 'String', :type_string
13+
param 'String', :previous_validation
14+
param 'NotUndef', :value
15+
optional_param 'Any', :args
16+
end
17+
18+
def validate_legacy_s(scope, type_string, *args)
19+
t = Puppet::Pops::Types::TypeParser.new.parse(type_string, scope)
20+
validate_legacy(t, *args)
21+
end
22+
23+
def validate_legacy(target_type, previous_validation, value, *prev_args)
24+
if assert_type(target_type, value)
25+
if previous_validation(previous_validation, value, *prev_args)
26+
puts 'Pass'
27+
# silently passes
28+
else
29+
# succeed with warning "Accepting previously invalid value for target_type"
30+
Puppet.warn("Accepting previously invalid value for target_type '#{target_type}'")
31+
end
32+
else
33+
inferred_type = Puppet::Pops::Types::TypeCalculator.infer_set(value)
34+
error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch(previous_validation, target_type, inferred_type)
35+
if previous_validation(previous_validation, value, *prev_args)
36+
# succeed with deprecation warning (use new error message)
37+
Puppet.warn(error_msg)
38+
else
39+
# fail with new error message
40+
call_function('fail', error_msg)
41+
end
42+
end
43+
end
44+
45+
def previous_validation(previous_validation, value, *prev_args)
46+
# Call the previous validation function and catch any errors
47+
begin
48+
call_function(previous_validation, value, *prev_args)
49+
true
50+
rescue Puppet::ParseError
51+
false
52+
end
53+
end
54+
55+
def assert_type(type, value)
56+
Puppet::Pops::Types::TypeCalculator.instance?(type, value)
57+
end
58+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'spec_helper'
2+
3+
if Puppet.version.to_f >= 4.0
4+
describe 'test::validate_legacy', type: :class do
5+
6+
describe 'validate_legacy passes assertion of type but not previous validation' do
7+
let(:params) {{ type: "Optional[Integer]", prev_validation: "validate_re", value: 5, previous_args1: ["^\\d+$", ""] }}
8+
it { Puppet.expects(:warn).with(includes('Accepting previously invalid value for target_type'))
9+
is_expected.to compile }
10+
end
11+
12+
describe 'validate_legacy passes assertion of type and previous validation' do
13+
let(:params) {{ type: "Optional[String]", prev_validation: "validate_re", value: "5", previous_args1: ["."] }}
14+
it { is_expected.to compile }
15+
end
16+
17+
describe 'validate_legacy fails assertion of type and passes previous validation' do
18+
let(:params) {{ type: "Optional[Integer]", prev_validation: "validate_re", value: "5", previous_args1: ["."] }}
19+
it { Puppet.expects(:warn).with(includes('expected'))
20+
is_expected.to compile }
21+
end
22+
23+
describe 'validate_legacy fails assertion and fails previous validation' do
24+
let(:params) {{ type: "Optional[Integer]", prev_validation: "validate_re", value: "5", previous_args1: ["thisisnotright"] }}
25+
it { is_expected.to compile.and_raise_error(/Error while evaluating a Function Call, \w* expected an \w* value, got \w*/) }
26+
end
27+
28+
end
29+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Class to test stdlib validate_legacy function
2+
3+
class test::validate_legacy( $type, $prev_validation, $value, $previous_args1
4+
) {
5+
6+
validate_legacy( $type, $prev_validation, $value, $previous_args1 )
7+
notice("Success")
8+
9+
}

0 commit comments

Comments
 (0)