Skip to content

Accept UnaryMinusExpression as class parameter type #9269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since strict defaults to warning in 7.x, this would effectively cause incorrect types to no longer fail compilation for the cases we fixed in adbb02c, like Integer[1-3].

We could instead do the following for both main and 7.x branches

Puppet[:strict] == :off ? :ignore : :error

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the least surprising thing would be to let it default to warning. In the case there is just a warning would the type just not be enforced? That doesn't seem terribly dangerous?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit subtle. In the case of Integer[1-3], the type is actually valid, because it evaluates to Integer[-2], which in the context of a class parameter Integer[-2] $i means it will accept integers >= -2. However, the manifest author intended to specify the range Integer[1, 3]. So if you pass a value bigger than 3, then it passes validation (surprise)!

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