Skip to content

Commit 46e3a23

Browse files
GeoffWilliamspmcmaw
authored andcommitted
(#FM-6068) allow file encoding to be specified (#726)
* (#FM-6068) allow file encoding to be specified Add a new parameter `encoding` to allow non UTF-8 files to specify a file encoding. This prevents receiving the error message "invalid byte sequence in UTF-8" when special characters that are not UTF-8 encoded appear in the input stream, such as the copyright symbol. * (#FM-6068) allow file encoding to be specified Added docs and tests as requested
1 parent 70d071b commit 46e3a23

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

README.markdown

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ In this code example, `match` looks for a line beginning with export
118118
followed by HTTP_PROXY and delete it. If multiple lines match, an
119119
error will be raised unless the `multiple => true` parameter is set.
120120

121+
Encoding example:
122+
123+
file_line { "XScreenSaver":
124+
ensure => present,
125+
path => '/root/XScreenSaver'
126+
line => "*lock: 10:00:00",
127+
match => '^*lock:',
128+
encoding => "iso-8859-1",
129+
}
130+
131+
Files with special characters that are not valid UTF-8 will give the
132+
error message "invalid byte sequence in UTF-8". In this case, determine
133+
the correct file encoding and specify the correct encoding using the
134+
encoding attribute, the value of which needs to be a valid Ruby character
135+
encoding.
136+
121137
**Autorequires:** If Puppet is managing the file that contains the line being managed, the `file_line` resource autorequires that file.
122138

123139
##### Parameters

lib/puppet/provider/file_line/ruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def lines
4040
# file; for now assuming that this type is only used on
4141
# small-ish config files that can fit into memory without
4242
# too much trouble.
43-
@lines ||= File.readlines(resource[:path])
43+
@lines ||= File.readlines(resource[:path], :encoding => resource[:encoding])
4444
end
4545

4646
def match_regex

lib/puppet/type/file_line.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@
4848
followed by HTTP_PROXY and delete it. If multiple lines match, an
4949
error will be raised unless the `multiple => true` parameter is set.
5050
51+
Encoding example:
52+
53+
file_line { "XScreenSaver":
54+
ensure => present,
55+
path => '/root/XScreenSaver'
56+
line => "*lock: 10:00:00",
57+
match => '^*lock:',
58+
encoding => "iso-8859-1",
59+
}
60+
61+
Files with special characters that are not valid UTF-8 will give the
62+
error message "invalid byte sequence in UTF-8". In this case, determine
63+
the correct file encoding and specify the correct encoding using the
64+
encoding attribute, the value of which needs to be a valid Ruby character
65+
encoding.
66+
5167
**Autorequires:** If Puppet is managing the file that will contain the line
5268
being managed, the file_line resource will autorequire that file.
5369
EOT
@@ -107,6 +123,11 @@
107123
defaultto true
108124
end
109125

126+
newparam(:encoding) do
127+
desc 'For files that are not UTF-8 encoded, specify encoding such as iso-8859-1'
128+
defaultto 'UTF-8'
129+
end
130+
110131
# Autorequire the file resource if it's being managed
111132
autorequire(:file) do
112133
self[:path]

spec/unit/puppet/type/file_line_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@
8787
it 'should default to replace => true' do
8888
expect(file_line[:replace]).to eq :true
8989
end
90-
90+
it 'should default to encoding => UTF-8' do
91+
expect(file_line[:encoding]).to eq 'UTF-8'
92+
end
93+
it 'should accept encoding => iso-8859-1' do
94+
expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :ensure => :present, :encoding => 'iso-8859-1', :line => 'bar') }.not_to raise_error
95+
end
9196
it "should autorequire the file it manages" do
9297
catalog = Puppet::Resource::Catalog.new
9398
file = Puppet::Type.type(:file).new(:name => tmp_path)

0 commit comments

Comments
 (0)