Skip to content

Commit a557077

Browse files
authored
Merge pull request #8639 from joshcooper/ignore_bom_in_templates_8243
(PUP-8243) Strip leading BOM from ERB templates
2 parents 78a9f15 + 9deef9c commit a557077

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

lib/puppet/file_system/file_impl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def read(path, opts = {})
8484
end
8585

8686
def read_preserve_line_endings(path)
87-
read(path)
87+
read(path, encoding: "bom|#{Encoding.default_external.name}")
8888
end
8989

9090
def binread(path)

lib/puppet/file_system/windows.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ def chmod(mode, path)
109109
end
110110

111111
def read_preserve_line_endings(path)
112-
contents = path.read( :mode => 'rb', :encoding => Encoding::UTF_8)
113-
contents = path.read( :mode => 'rb', :encoding => Encoding::default_external) unless contents.valid_encoding?
112+
contents = path.read( :mode => 'rb', :encoding => 'bom|utf-8')
113+
contents = path.read( :mode => 'rb', :encoding => "bom|#{Encoding::default_external.name}") unless contents.valid_encoding?
114114
contents = path.read unless contents.valid_encoding?
115115

116116
contents

spec/unit/file_system_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ def increment_counter_in_multiple_processes(file, num_procs, options)
290290
expect(Puppet::FileSystem.read_preserve_line_endings(file)).to eq("file content \r\nsecond line \n")
291291
end
292292
end
293+
294+
it "should ignore leading BOM" do
295+
with_file_content("\uFEFFfile content \n") do |file|
296+
expect(Puppet::FileSystem.read_preserve_line_endings(file)).to eq("file content \n")
297+
end
298+
end
293299
end
294300

295301
context "read without an encoding specified" do

spec/unit/parser/templatewrapper_spec.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
require 'puppet/parser/templatewrapper'
33

44
describe Puppet::Parser::TemplateWrapper do
5+
include PuppetSpec::Files
6+
57
let(:known_resource_types) { Puppet::Resource::TypeCollection.new("env") }
68
let(:scope) do
79
compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("mynode"))
@@ -41,6 +43,13 @@
4143
expect(tw.result).to eq(full_file_name)
4244
end
4345

46+
it "ignores a leading BOM" do
47+
full_file_name = given_a_template_file("bom_template", "\uFEFF<%= file %>")
48+
49+
tw.file = "bom_template"
50+
expect(tw.result).to eq(full_file_name)
51+
end
52+
4453
it "evaluates a given string as a template" do
4554
expect(tw.result("template contents")).to eql("template contents")
4655
end
@@ -90,11 +99,12 @@
9099
end
91100

92101
def given_a_template_file(name, contents)
93-
full_name = "/full/path/to/#{name}"
102+
full_name = tmpfile("template_#{name}")
103+
File.binwrite(full_name, contents)
104+
94105
allow(Puppet::Parser::Files).to receive(:find_template).
95106
with(name, anything()).
96107
and_return(full_name)
97-
allow(Puppet::FileSystem).to receive(:read_preserve_line_endings).with(full_name).and_return(contents)
98108

99109
full_name
100110
end

0 commit comments

Comments
 (0)