Skip to content

Commit 40d8dbf

Browse files
authored
Merge pull request #9307 from joshcooper/backport_12026
[Backport] (PUP-12026) Change non-literal type check to deprecation warning
2 parents b53fd33 + 99d5d8c commit 40d8dbf

File tree

6 files changed

+28
-33
lines changed

6 files changed

+28
-33
lines changed

lib/puppet/info_service/class_information_service.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def extract_param(p)
7474

7575
def extract_type(structure, p)
7676
return structure if p.type_expr.nil?
77-
structure[:type] = typeexpr_to_string(p.type_expr)
77+
structure[:type] = typeexpr_to_string(p.name, p.type_expr)
7878
structure
7979
end
8080

@@ -87,12 +87,11 @@ def extract_default(structure, p)
8787
structure
8888
end
8989

90-
def typeexpr_to_string(type_expr)
90+
def typeexpr_to_string(name, type_expr)
9191
begin
9292
type_parser.interpret_any(type_expr, nil).to_s
93-
rescue Puppet::ParseError
94-
# type is to complex - contains expressions that are not literal
95-
nil
93+
rescue Puppet::ParseError => e
94+
raise Puppet::Error, "The parameter '$#{name}' is invalid: #{e.message}", e.backtrace
9695
end
9796
end
9897

lib/puppet/pops/validation/validator_factory_4_0.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def severity_producer
3737
p[Issues::EMPTY_RESOURCE_SPECIALIZATION] = :ignore
3838
p[Issues::CLASS_NOT_VIRTUALIZABLE] = :error
3939

40-
p[Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE] = Puppet[:strict] == :off ? :ignore : Puppet[:strict]
40+
p[Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE] = :deprecation
4141
p
4242
end
4343
end

spec/lib/puppet_spec/matchers.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
end
1515
end
1616

17+
RSpec::Matchers.define :have_matching_log_with_source do |expected, file, line, pos|
18+
match do |actual|
19+
actual.any? { |item| item.message =~ expected && item.file == file && item.line == line && item.pos == pos }
20+
end
21+
end
22+
1723
RSpec::Matchers.define :exit_with do |expected|
1824
actual = nil
1925
match do |block|

spec/unit/info_service_spec.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,20 +518,23 @@ class oops_2(Optional[[String]] $double_brackets) { }
518518
it "warns with a descriptive message if non-literal class parameter is given" do
519519
files = ['non_literal.pp', 'non_literal_2.pp'].map {|f| File.join(code_dir, f) }
520520
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"))
521+
expect(@logs).to have_matching_log_with_source(/The parameter '\$bad_int' must be a literal type, not a Puppet::Pops::Model::AccessExpression/, "#{code_dir}/non_literal.pp", 1, 37)
522+
expect(@logs).to have_matching_log_with_source(/The parameter '\$double_brackets' must be a literal type, not a Puppet::Pops::Model::AccessExpression/, "#{code_dir}/non_literal_2.pp", 1, 44)
523523
end
524524

525525
it "errors in strict mode if non-literal class parameter is given" do
526526
Puppet[:strict] = "error"
527527
files = ['non_literal.pp', 'non_literal_2.pp'].map {|f| File.join(code_dir, f) }
528528
result = Puppet::InfoService.classes_per_environment({'production' => files })
529+
expect(@logs).to have_matching_log_with_source(/The parameter '\$bad_int' must be a literal type, not a Puppet::Pops::Model::AccessExpression/, "#{code_dir}/non_literal.pp", 1, 37)
530+
expect(@logs).to have_matching_log_with_source(/The parameter '\$double_brackets' must be a literal type, not a Puppet::Pops::Model::AccessExpression/, "#{code_dir}/non_literal_2.pp", 1, 44)
531+
529532
expect(result).to eq({
530533
"production"=>{
531534
"#{code_dir}/non_literal.pp" =>
532-
{:error=> "The parameter '$bad_int' must be a literal type, not a Puppet::Pops::Model::AccessExpression (file: #{code_dir}/non_literal.pp, line: 1, column: 37)"},
535+
{:error=> "The parameter '\$bad_int' is invalid: The expression <1-3> is not a valid type specification."},
533536
"#{code_dir}/non_literal_2.pp" =>
534-
{:error=> "The parameter '$double_brackets' must be a literal type, not a Puppet::Pops::Model::AccessExpression (file: #{code_dir}/non_literal_2.pp, line: 1, column: 44)"}
537+
{:error=> "The parameter '\$double_brackets' is invalid: The expression <Optional[[String]]> is not a valid type specification."}
535538
} # end production env
536539
})
537540
end

spec/unit/pops/evaluator/literal_evaluator_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
'Integer[-1]' => [-1],
2222
'Integer[-5, -1]' => [-5, -1],
2323
'Integer[-5, 5]' => [-5, 5],
24-
# we can't actually represent MIN_INTEGER because it's glexed as
24+
# we can't actually represent MIN_INTEGER below, because it's lexed as
2525
# UnaryMinusExpression containing a positive LiteralInteger and the integer
26-
# must be <= MAX_INTEGER
26+
# must be <= MAX_INTEGER. Therefore, the effective minimum is one greater.
2727
"Integer[#{Puppet::Pops::MIN_INTEGER + 1}]" => [-0x7FFFFFFFFFFFFFFF],
2828
"Integer[0, #{Puppet::Pops::MAX_INTEGER}]" => [0, 0x7FFFFFFFFFFFFFFF],
2929
'Integer[0, default]' => [0, :default],

spec/unit/pops/validator/validator_spec.rb

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,6 @@ 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
222215
end
223216

224217
context 'with --strict set to error' do
@@ -270,13 +263,6 @@ def with_environment(environment, env_params = {})
270263
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_TOP_CONSTRUCT_LOCATION)
271264
end
272265
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
280266
end
281267

282268
context 'with --strict set to off' do
@@ -307,13 +293,6 @@ def with_environment(environment, env_params = {})
307293
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_TOP_CONSTRUCT_LOCATION)
308294
end
309295
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
317296
end
318297

319298
context 'irrespective of --strict' do
@@ -344,6 +323,14 @@ def with_environment(environment, env_params = {})
344323
expect(acceptor.error_count).to eql(1)
345324
expect(acceptor).to have_issue(Puppet::Pops::Issues::CLASS_NOT_VIRTUALIZABLE)
346325
end
326+
327+
it 'produces a deprecation warning for non-literal class parameters' do
328+
acceptor = validate(parse('class test(Integer[2-1] $port) {}'))
329+
expect(deprecation_count(acceptor)).to eql(1)
330+
expect(acceptor.warning_count).to eql(1)
331+
expect(acceptor.error_count).to eql(0)
332+
expect(acceptor).to have_issue(Puppet::Pops::Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE)
333+
end
347334
end
348335

349336
context 'with --tasks set' do

0 commit comments

Comments
 (0)