From 899276380a719ae7b4e7b4db499cde29f0c3a39b Mon Sep 17 00:00:00 2001 From: Aria Li Date: Wed, 17 Jan 2024 15:32:06 -0800 Subject: [PATCH] (PUP-11981) Add test for non-literal class params in ClassInfoService spec --- .../pops/evaluator/literal_evaluator.rb | 2 +- lib/puppet/pops/validation/checker4_0.rb | 2 +- spec/unit/info_service_spec.rb | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/puppet/pops/evaluator/literal_evaluator.rb b/lib/puppet/pops/evaluator/literal_evaluator.rb index 19c1be08a06..4bf30c6d953 100644 --- a/lib/puppet/pops/evaluator/literal_evaluator.rb +++ b/lib/puppet/pops/evaluator/literal_evaluator.rb @@ -73,7 +73,7 @@ def literal_QualifiedReference(o) def literal_AccessExpression(o) # to prevent parameters with [[]] like Optional[[String]] throw :not_literal if o.keys.size == 1 && o.keys[0].is_a?(Model::LiteralList) - o.keys.map {|v| literal(v) } + o.keys.map { |v| literal(v) } end def literal_ConcatenatedString(o) diff --git a/lib/puppet/pops/validation/checker4_0.rb b/lib/puppet/pops/validation/checker4_0.rb index 62f953fffa5..b707483b834 100644 --- a/lib/puppet/pops/validation/checker4_0.rb +++ b/lib/puppet/pops/validation/checker4_0.rb @@ -491,7 +491,7 @@ def internal_check_parameter_type_literal(o) catch :not_literal do type = literal(p.type_expr) end - acceptor.accept(Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE, p, {name: p.name, type_class: p.type_expr.class}) if type.nil? + acceptor.accept(Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE, p, { name: p.name, type_class: p.type_expr.class }) if type.nil? end end diff --git a/spec/unit/info_service_spec.rb b/spec/unit/info_service_spec.rb index 7c9ee12c2c1..1f171806d0f 100644 --- a/spec/unit/info_service_spec.rb +++ b/spec/unit/info_service_spec.rb @@ -313,6 +313,12 @@ class Borked($Herp+$Derp) {} CODE 'json_unsafe.pp' => <<-CODE, class json_unsafe($arg1 = /.*/, $arg2 = default, $arg3 = {1 => 1}) {} + CODE + 'non_literal.pp' => <<-CODE, + class oops(Integer[1-3] $bad_int) { } + CODE + 'non_literal_2.pp' => <<-CODE, + class oops_2(Optional[[String]] $double_brackets) { } CODE }) end @@ -509,6 +515,19 @@ class json_unsafe($arg1 = /.*/, $arg2 = default, $arg3 = {1 => 1}) {} }) end + it "errors 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) } + result = Puppet::InfoService.classes_per_environment({'production' => files }) + expect(result).to eq({ + "production"=>{ + "#{code_dir}/non_literal.pp" => + {: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)"}, + "#{code_dir}/non_literal_2.pp" => + {: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)"} + } # end production env + }) + end + it "produces no type entry if type is not given" do files = ['fum.pp'].map {|f| File.join(code_dir, f) } result = Puppet::InfoService.classes_per_environment({'production' => files })