Skip to content

Commit

Permalink
Merge pull request #9269 from joshcooper/unary_minus_literal
Browse files Browse the repository at this point in the history
Accept UnaryMinusExpression as class parameter type
  • Loading branch information
cthorn42 authored Feb 29, 2024
2 parents 2bc53e6 + 17ba937 commit e8d1797
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/puppet/pops/evaluator/literal_evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def literal_AccessExpression(o)
o.keys.map { |v| literal(v) }
end

def literal_UnaryMinusExpression(o)
-literal(o.expr)
end

def literal_ConcatenatedString(o)
# use double quoted string value if there is no interpolation
throw :not_literal unless o.segments.size == 1 && o.segments[0].is_a?(Model::LiteralString)
Expand Down
2 changes: 2 additions & 0 deletions lib/puppet/pops/validation/validator_factory_4_0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def severity_producer
p[Issues::NAME_WITH_HYPHEN] = :error
p[Issues::EMPTY_RESOURCE_SPECIALIZATION] = :ignore
p[Issues::CLASS_NOT_VIRTUALIZABLE] = :error

p[Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE] = Puppet[:strict] == :off ? :ignore : Puppet[:strict]
p
end
end
Expand Down
30 changes: 29 additions & 1 deletion spec/unit/pops/evaluator/literal_evaluator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,26 @@
'"a"' => 'a',
'a' => 'a',
'a::b' => 'a::b',
'Boolean[true]' => [true],
'Integer[1]' => [1],
'Integer[-1]' => [-1],
'Integer[-5, -1]' => [-5, -1],
'Integer[-5, 5]' => [-5, 5],
# we can't actually represent MIN_INTEGER because it's glexed as
# UnaryMinusExpression containing a positive LiteralInteger and the integer
# must be <= MAX_INTEGER
"Integer[#{Puppet::Pops::MIN_INTEGER + 1}]" => [-0x7FFFFFFFFFFFFFFF],
"Integer[0, #{Puppet::Pops::MAX_INTEGER}]" => [0, 0x7FFFFFFFFFFFFFFF],
'Integer[0, default]' => [0, :default],
'Integer[Infinity]' => ['infinity'],
'Float[Infinity]' => ['infinity'],
'Array[Integer, 1]' => ['integer', 1],
'Hash[Integer, String, 1, 3]' => ['integer', 'string', 1, 3],
'Regexp[/-1/]' => [/-1/],
'Sensitive[-1]' => [-1],
'Timespan[-5, 5]' => [-5, 5],
'Timestamp["2012-10-10", 1]' => ['2012-10-10', 1],
'Undef' => 'undef',
'File' => "file",

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

['1+1', '[1,2, 1+2]', '{a=>1+1}', '"x$y"', '"x${y}z"', 'Integer[1-3]', 'Optional[[String]]'].each do |source|
[ '',
'1+1',
'[1,2, 1+2]',
'{a=>1+1}',
'"x$y"',
'"x${y}z"',
'Integer[1-3]',
'Integer[-1-3]',
'Optional[[String]]'
].each do |source|
it "throws :not_literal for non literal expression '#{source}'" do
expect{leval.literal(parser.parse_string(source))}.to throw_symbol(:not_literal)
end
Expand Down
21 changes: 21 additions & 0 deletions spec/unit/pops/validator/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ def with_environment(environment, env_params = {})
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_TOP_CONSTRUCT_LOCATION)
end
end

it 'produces a warning for non-literal class parameters' do
acceptor = validate(parse('class test(Integer[2-1] $port) {}'))
expect(acceptor.warning_count).to eql(1)
expect(acceptor.error_count).to eql(0)
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE)
end
end

context 'with --strict set to error' do
Expand Down Expand Up @@ -262,6 +269,13 @@ def with_environment(environment, env_params = {})
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_TOP_CONSTRUCT_LOCATION)
end
end

it 'produces an error for non-literal class parameters' do
acceptor = validate(parse('class test(Integer[2-1] $port) {}'))
expect(acceptor.warning_count).to eql(0)
expect(acceptor.error_count).to eql(1)
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE)
end
end

context 'with --strict set to off' do
Expand Down Expand Up @@ -292,6 +306,13 @@ def with_environment(environment, env_params = {})
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_TOP_CONSTRUCT_LOCATION)
end
end

it 'does not produce an error or warning for non-literal class parameters' do
acceptor = validate(parse('class test(Integer[2-1] $port) {}'))
expect(acceptor.warning_count).to eql(0)
expect(acceptor.error_count).to eql(0)
expect(acceptor).to_not have_issue(Puppet::Pops::Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE)
end
end

context 'irrespective of --strict' do
Expand Down

0 comments on commit e8d1797

Please sign in to comment.