Skip to content

Commit 6fd9af4

Browse files
authored
Merge pull request #9190 from AriaXLi/PUP-11981
(PUP-11981) Syntactically incorrect types cause nil in Puppet::InfoService::ClassInformationService
2 parents 9dcff4d + adbb02c commit 6fd9af4

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

lib/puppet/pops/evaluator/literal_evaluator.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ module Evaluator
1212
# QualifiedName
1313
# Default (produced :default)
1414
# Regular Expression (produces ruby regular expression)
15-
#
16-
# Not considered literal
17-
# QualifiedReference # i.e. File, FooBar
15+
# QualifiedReference
16+
# AccessExpresion
1817
#
1918
class LiteralEvaluator
2019
COMMA_SEPARATOR = ', '
@@ -67,6 +66,16 @@ def literal_LiteralRegularExpression(o)
6766
o.value
6867
end
6968

69+
def literal_QualifiedReference(o)
70+
o.value
71+
end
72+
73+
def literal_AccessExpression(o)
74+
# to prevent parameters with [[]] like Optional[[String]]
75+
throw :not_literal if o.keys.size == 1 && o.keys[0].is_a?(Model::LiteralList)
76+
o.keys.map {|v| literal(v) }
77+
end
78+
7079
def literal_ConcatenatedString(o)
7180
# use double quoted string value if there is no interpolation
7281
throw :not_literal unless o.segments.size == 1 && o.segments[0].is_a?(Model::LiteralString)

lib/puppet/pops/issues.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ def self.hard_issue(issue_code, *args, &block)
211211
_("The numeric parameter name '$%{name}' cannot be used (clashes with numeric match result variables)") % { name: name }
212212
end
213213

214+
ILLEGAL_NONLITERAL_PARAMETER_TYPE = issue :ILLEGAL_NONLITERAL_PARAMETER_TYPE, :name, :type_class do
215+
_("The parameter '$%{name}' must be a literal type, not %{type_class}") % { name: name, type_class: label.a_an(type_class) }
216+
end
217+
214218
# In certain versions of Puppet it may be allowed to assign to a not already assigned key
215219
# in an array or a hash. This is an optional validation that may be turned on to prevent accidental
216220
# mutation.

lib/puppet/pops/validation/checker4_0.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ def check_HostClassDefinition(o)
472472
internal_check_parameter_name_uniqueness(o)
473473
internal_check_reserved_params(o)
474474
internal_check_no_idem_last(o)
475+
internal_check_parameter_type_literal(o)
475476
end
476477

477478
def check_ResourceTypeDefinition(o)
@@ -482,6 +483,18 @@ def check_ResourceTypeDefinition(o)
482483
internal_check_no_idem_last(o)
483484
end
484485

486+
def internal_check_parameter_type_literal(o)
487+
o.parameters.each do |p|
488+
next unless p.type_expr
489+
490+
type = nil
491+
catch :not_literal do
492+
type = literal(p.type_expr)
493+
end
494+
acceptor.accept(Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE, p, {name: p.name, type_class: p.type_expr.class}) if type.nil?
495+
end
496+
end
497+
485498
def internal_check_return_type(o)
486499
r = o.return_type
487500
internal_check_type_ref(o, r) unless r.nil?

spec/unit/pops/evaluator/literal_evaluator_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
'"a"' => 'a',
1717
'a' => 'a',
1818
'a::b' => 'a::b',
19+
'Integer[1]' => [1],
20+
'File' => "file",
1921

2022
# special values
2123
'default' => :default,
@@ -35,9 +37,9 @@
3537
expect(leval.literal(parser.parse_string('undef'))).to be_nil
3638
end
3739

38-
['1+1', 'File', '[1,2, 1+2]', '{a=>1+1}', 'Integer[1]', '"x$y"', '"x${y}z"'].each do |source|
40+
['1+1', '[1,2, 1+2]', '{a=>1+1}', '"x$y"', '"x${y}z"', 'Integer[1-3]', 'Optional[[String]]'].each do |source|
3941
it "throws :not_literal for non literal expression '#{source}'" do
40-
expect{leval.literal(parser.parse_string('1+1'))}.to throw_symbol(:not_literal)
42+
expect{leval.literal(parser.parse_string(source))}.to throw_symbol(:not_literal)
4143
end
4244
end
4345
end

0 commit comments

Comments
 (0)