Skip to content

Commit 090122a

Browse files
authored
Merge pull request #9274 from joshcooper/backport_unary_7291
Backport #9269 to 7.29.1
2 parents 1fff77d + dba653f commit 090122a

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

lib/puppet/pops/evaluator/literal_evaluator.rb

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

78+
def literal_UnaryMinusExpression(o)
79+
-literal(o.expr)
80+
end
81+
7882
def literal_ConcatenatedString(o)
7983
# use double quoted string value if there is no interpolation
8084
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/info_service_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,15 @@ class oops_2(Optional[[String]] $double_brackets) { }
515515
})
516516
end
517517

518-
it "errors with a descriptive message if non-literal class parameter is given" do
518+
it "warns with a descriptive message if non-literal class parameter is given" do
519+
files = ['non_literal.pp', 'non_literal_2.pp'].map {|f| File.join(code_dir, f) }
520+
Puppet::InfoService.classes_per_environment({'production' => files })
521+
expect(@logs).to include(an_object_having_attributes(message: "The parameter '$bad_int' must be a literal type, not a Puppet::Pops::Model::AccessExpression"))
522+
expect(@logs).to include(an_object_having_attributes(message: "The parameter '$double_brackets' must be a literal type, not a Puppet::Pops::Model::AccessExpression"))
523+
end
524+
525+
it "errors in strict mode if non-literal class parameter is given" do
526+
Puppet[:strict] = "error"
519527
files = ['non_literal.pp', 'non_literal_2.pp'].map {|f| File.join(code_dir, f) }
520528
result = Puppet::InfoService.classes_per_environment({'production' => files })
521529
expect(result).to eq({

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
@@ -212,6 +212,13 @@ def with_environment(environment, env_params = {})
212212
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_TOP_CONSTRUCT_LOCATION)
213213
end
214214
end
215+
216+
it 'produces a warning for non-literal class parameters' do
217+
acceptor = validate(parse('class test(Integer[2-1] $port) {}'))
218+
expect(acceptor.warning_count).to eql(1)
219+
expect(acceptor.error_count).to eql(0)
220+
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE)
221+
end
215222
end
216223

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

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

298319
context 'irrespective of --strict' do

0 commit comments

Comments
 (0)