From 9938cd806703904cb0069abcc5753a359f869412 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Wed, 15 Jun 2022 11:51:00 +0200 Subject: [PATCH] (PUP-11552) Pass ERB arguments as keywords Ruby 2.6 introduced trim_mode as a keyword argument and deprecated the use of safe_level as well as calling it as positional arguments. On Ruby 3.1 calling it as a non-keyword argument raises a deprecation warning. warning: Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments. warning: Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: ...) instead. --- lib/puppet/face/help.rb | 2 +- lib/puppet/generate/type.rb | 2 +- lib/puppet/parser/templatewrapper.rb | 2 +- lib/puppet/util.rb | 11 +++++++++++ lib/puppet/util/resource_template.rb | 2 +- spec/unit/util/resource_template_spec.rb | 2 +- 6 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/puppet/face/help.rb b/lib/puppet/face/help.rb index 362094094f3..0240162fa78 100644 --- a/lib/puppet/face/help.rb +++ b/lib/puppet/face/help.rb @@ -140,7 +140,7 @@ def template_for(face, action) def erb(name) template = (Pathname(__FILE__).dirname + "help" + name) - erb = ERB.new(template.read, nil, '-') + erb = Puppet::Util.create_erb(template.read) erb.filename = template.to_s return erb end diff --git a/lib/puppet/generate/type.rb b/lib/puppet/generate/type.rb index dd4015838b8..866d85ba284 100644 --- a/lib/puppet/generate/type.rb +++ b/lib/puppet/generate/type.rb @@ -167,7 +167,7 @@ def self.generate(inputs, outputdir = nil, force = false) templates = {} templates.default_proc = lambda { |hash, key| raise _("template was not found at '%{key}'.") % { key: key } unless Puppet::FileSystem.file?(key) - template = ERB.new(File.read(key), nil, '-') + template = Puppet::Util.create_erb(File.read(key)) template.filename = key template } diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb index 75bd153712d..d3b897dafd3 100644 --- a/lib/puppet/parser/templatewrapper.rb +++ b/lib/puppet/parser/templatewrapper.rb @@ -90,7 +90,7 @@ def result(string = nil) result = nil benchmark(:debug, _("Interpolated template %{template_source} in %%{seconds} seconds") % { template_source: escaped_template_source }) do - template = ERB.new(string, 0, "-") + template = Puppet::Util.create_erb(string) template.filename = @__file__ result = template.result(binding) end diff --git a/lib/puppet/util.rb b/lib/puppet/util.rb index 06b42fdf7a7..4c0ecea8a41 100644 --- a/lib/puppet/util.rb +++ b/lib/puppet/util.rb @@ -34,6 +34,17 @@ def default_env end module_function :default_env + if RUBY_VERSION >= "2.6" + def create_erb(content) + ERB.new(content, trim_mode: '-') + end + else + def create_erb(content) + ERB.new(content, 0, '-') + end + end + module_function :create_erb + # @param name [String] The name of the environment variable to retrieve # @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect # @return [String] Value of the specified environment variable. nil if it does not exist diff --git a/lib/puppet/util/resource_template.rb b/lib/puppet/util/resource_template.rb index 30bbd5a8558..0af602ffd50 100644 --- a/lib/puppet/util/resource_template.rb +++ b/lib/puppet/util/resource_template.rb @@ -40,7 +40,7 @@ class Puppet::Util::ResourceTemplate def evaluate set_resource_variables - ERB.new(Puppet::FileSystem.read(@file, :encoding => 'utf-8'), 0, "-").result(binding) + Puppet::Util.create_erb(Puppet::FileSystem.read(@file, :encoding => 'utf-8')).result(binding) end def initialize(file, resource) diff --git a/spec/unit/util/resource_template_spec.rb b/spec/unit/util/resource_template_spec.rb index 2fb1ed56807..08880e0a369 100644 --- a/spec/unit/util/resource_template_spec.rb +++ b/spec/unit/util/resource_template_spec.rb @@ -39,7 +39,7 @@ it "should create a template instance with the contents of the file" do expect(Puppet::FileSystem).to receive(:read).with("/my/template", :encoding => 'utf-8').and_return("yay") - expect(ERB).to receive(:new).with("yay", 0, "-").and_return(@template) + expect(Puppet::Util).to receive(:create_erb).with("yay").and_return(@template) allow(@wrapper).to receive(:set_resource_variables)