Skip to content

Commit 939d5d4

Browse files
authored
Merge pull request #9273 from joshcooper/backport_unary_851
Backport #9269 to 8.5.1
2 parents dd1bb20 + c797f18 commit 939d5d4

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

lib/puppet/pops/evaluator/literal_evaluator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def literal_AccessExpression(o)
7676
o.keys.map { |v| literal(v) }
7777
end
7878

79+
def literal_UnaryMinusExpression(o)
80+
-literal(o.expr)
81+
end
82+
7983
def literal_ConcatenatedString(o)
8084
# use double quoted string value if there is no interpolation
8185
throw :not_literal unless o.segments.size == 1 && o.segments[0].is_a?(Model::LiteralString)

lib/puppet/pops/validation/validator_factory_4_0.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def severity_producer
3636
p[Issues::NAME_WITH_HYPHEN] = :error
3737
p[Issues::EMPTY_RESOURCE_SPECIALIZATION] = :ignore
3838
p[Issues::CLASS_NOT_VIRTUALIZABLE] = :error
39+
40+
p[Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE] = Puppet[:strict] == :off ? :ignore : Puppet[:strict]
3941
p
4042
end
4143
end

spec/unit/pops/evaluator/literal_evaluator_spec.rb

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,26 @@
1616
'"a"' => 'a',
1717
'a' => 'a',
1818
'a::b' => 'a::b',
19+
'Boolean[true]' => [true],
1920
'Integer[1]' => [1],
21+
'Integer[-1]' => [-1],
22+
'Integer[-5, -1]' => [-5, -1],
23+
'Integer[-5, 5]' => [-5, 5],
24+
# we can't actually represent MIN_INTEGER because it's glexed as
25+
# UnaryMinusExpression containing a positive LiteralInteger and the integer
26+
# must be <= MAX_INTEGER
27+
"Integer[#{Puppet::Pops::MIN_INTEGER + 1}]" => [-0x7FFFFFFFFFFFFFFF],
28+
"Integer[0, #{Puppet::Pops::MAX_INTEGER}]" => [0, 0x7FFFFFFFFFFFFFFF],
29+
'Integer[0, default]' => [0, :default],
30+
'Integer[Infinity]' => ['infinity'],
31+
'Float[Infinity]' => ['infinity'],
32+
'Array[Integer, 1]' => ['integer', 1],
33+
'Hash[Integer, String, 1, 3]' => ['integer', 'string', 1, 3],
34+
'Regexp[/-1/]' => [/-1/],
35+
'Sensitive[-1]' => [-1],
36+
'Timespan[-5, 5]' => [-5, 5],
37+
'Timestamp["2012-10-10", 1]' => ['2012-10-10', 1],
38+
'Undef' => 'undef',
2039
'File' => "file",
2140

2241
# special values
@@ -37,7 +56,16 @@
3756
expect(leval.literal(parser.parse_string('undef'))).to be_nil
3857
end
3958

40-
['1+1', '[1,2, 1+2]', '{a=>1+1}', '"x$y"', '"x${y}z"', 'Integer[1-3]', 'Optional[[String]]'].each do |source|
59+
[ '',
60+
'1+1',
61+
'[1,2, 1+2]',
62+
'{a=>1+1}',
63+
'"x$y"',
64+
'"x${y}z"',
65+
'Integer[1-3]',
66+
'Integer[-1-3]',
67+
'Optional[[String]]'
68+
].each do |source|
4169
it "throws :not_literal for non literal expression '#{source}'" do
4270
expect{leval.literal(parser.parse_string(source))}.to throw_symbol(:not_literal)
4371
end

spec/unit/pops/validator/validator_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ def with_environment(environment, env_params = {})
211211
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_TOP_CONSTRUCT_LOCATION)
212212
end
213213
end
214+
215+
it 'produces a warning for non-literal class parameters' do
216+
acceptor = validate(parse('class test(Integer[2-1] $port) {}'))
217+
expect(acceptor.warning_count).to eql(1)
218+
expect(acceptor.error_count).to eql(0)
219+
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE)
220+
end
214221
end
215222

216223
context 'with --strict set to error' do
@@ -262,6 +269,13 @@ def with_environment(environment, env_params = {})
262269
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_TOP_CONSTRUCT_LOCATION)
263270
end
264271
end
272+
273+
it 'produces an error for non-literal class parameters' do
274+
acceptor = validate(parse('class test(Integer[2-1] $port) {}'))
275+
expect(acceptor.warning_count).to eql(0)
276+
expect(acceptor.error_count).to eql(1)
277+
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE)
278+
end
265279
end
266280

267281
context 'with --strict set to off' do
@@ -292,6 +306,13 @@ def with_environment(environment, env_params = {})
292306
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_TOP_CONSTRUCT_LOCATION)
293307
end
294308
end
309+
310+
it 'does not produce an error or warning for non-literal class parameters' do
311+
acceptor = validate(parse('class test(Integer[2-1] $port) {}'))
312+
expect(acceptor.warning_count).to eql(0)
313+
expect(acceptor.error_count).to eql(0)
314+
expect(acceptor).to_not have_issue(Puppet::Pops::Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE)
315+
end
295316
end
296317

297318
context 'irrespective of --strict' do

0 commit comments

Comments
 (0)