Skip to content

Commit ed44890

Browse files
authored
Merge pull request #8918 from ekohl/PUP-11552-erb-ruby-3.1
(PUP-11552) Pass ERB arguments as keywords
2 parents 13188f1 + 9938cd8 commit ed44890

File tree

6 files changed

+16
-5
lines changed

6 files changed

+16
-5
lines changed

lib/puppet/face/help.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def template_for(face, action)
140140

141141
def erb(name)
142142
template = (Pathname(__FILE__).dirname + "help" + name)
143-
erb = ERB.new(template.read, nil, '-')
143+
erb = Puppet::Util.create_erb(template.read)
144144
erb.filename = template.to_s
145145
return erb
146146
end

lib/puppet/generate/type.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def self.generate(inputs, outputdir = nil, force = false)
167167
templates = {}
168168
templates.default_proc = lambda { |hash, key|
169169
raise _("template was not found at '%{key}'.") % { key: key } unless Puppet::FileSystem.file?(key)
170-
template = ERB.new(File.read(key), nil, '-')
170+
template = Puppet::Util.create_erb(File.read(key))
171171
template.filename = key
172172
template
173173
}

lib/puppet/parser/templatewrapper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def result(string = nil)
9090

9191
result = nil
9292
benchmark(:debug, _("Interpolated template %{template_source} in %%{seconds} seconds") % { template_source: escaped_template_source }) do
93-
template = ERB.new(string, 0, "-")
93+
template = Puppet::Util.create_erb(string)
9494
template.filename = @__file__
9595
result = template.result(binding)
9696
end

lib/puppet/util.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ def default_env
3434
end
3535
module_function :default_env
3636

37+
if RUBY_VERSION >= "2.6"
38+
def create_erb(content)
39+
ERB.new(content, trim_mode: '-')
40+
end
41+
else
42+
def create_erb(content)
43+
ERB.new(content, 0, '-')
44+
end
45+
end
46+
module_function :create_erb
47+
3748
# @param name [String] The name of the environment variable to retrieve
3849
# @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
3950
# @return [String] Value of the specified environment variable. nil if it does not exist

lib/puppet/util/resource_template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Puppet::Util::ResourceTemplate
4040

4141
def evaluate
4242
set_resource_variables
43-
ERB.new(Puppet::FileSystem.read(@file, :encoding => 'utf-8'), 0, "-").result(binding)
43+
Puppet::Util.create_erb(Puppet::FileSystem.read(@file, :encoding => 'utf-8')).result(binding)
4444
end
4545

4646
def initialize(file, resource)

spec/unit/util/resource_template_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
it "should create a template instance with the contents of the file" do
4141
expect(Puppet::FileSystem).to receive(:read).with("/my/template", :encoding => 'utf-8').and_return("yay")
42-
expect(ERB).to receive(:new).with("yay", 0, "-").and_return(@template)
42+
expect(Puppet::Util).to receive(:create_erb).with("yay").and_return(@template)
4343

4444
allow(@wrapper).to receive(:set_resource_variables)
4545

0 commit comments

Comments
 (0)