Skip to content

[Backport 7.x] Accept UnaryMinusExpression as class parameter type #9272

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 @@ -75,6 +75,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
10 changes: 9 additions & 1 deletion spec/unit/info_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,15 @@ class oops_2(Optional[[String]] $double_brackets) { }
})
end

it "errors with a descriptive message if non-literal class parameter is given" do
it "warns with a descriptive message if non-literal class parameter is given" do
files = ['non_literal.pp', 'non_literal_2.pp'].map {|f| File.join(code_dir, f) }
Puppet::InfoService.classes_per_environment({'production' => files })
expect(@logs).to include(an_object_having_attributes(message: "The parameter '$bad_int' must be a literal type, not a Puppet::Pops::Model::AccessExpression"))
expect(@logs).to include(an_object_having_attributes(message: "The parameter '$double_brackets' must be a literal type, not a Puppet::Pops::Model::AccessExpression"))
end

it "errors in strict mode if non-literal class parameter is given" do
Puppet[:strict] = "error"
files = ['non_literal.pp', 'non_literal_2.pp'].map {|f| File.join(code_dir, f) }
result = Puppet::InfoService.classes_per_environment({'production' => files })
expect(result).to eq({
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 @@ -212,6 +212,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 @@ -263,6 +270,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 @@ -293,6 +307,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