Skip to content

Commit 40c67e2

Browse files
committed
(#22129) force explicit file content to be treated as a bytestring
This replicates the implicit behavior in Ruby-1.8.7, and avoids chasing down every location where a value is passed to `should=` and setting the encoding there.
1 parent d2acfbd commit 40c67e2

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/puppet/type/file/content.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ def retrieve
133133

134134
# Make sure we're also managing the checksum property.
135135
def should=(value)
136+
# treat the value as a bytestring, in Ruby versions that support it, regardless of the encoding
137+
# in which it has been supplied
138+
value = value.clone.force_encoding(Encoding::ASCII_8BIT) if value.respond_to?(:force_encoding)
136139
@resource.newattr(:checksum) unless @resource.parameter(:checksum)
137140
super
138141
end

spec/unit/type/file/content_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@
8484

8585
@content.should.must == string
8686
end
87+
88+
if "".respond_to? :encode then
89+
it "should convert the value to ASCII-8BIT" do
90+
@content = content.new(:resource => @resource)
91+
@content.should= "Let's make a \u{2603}"
92+
93+
@content.actual_content.should == "Let's make a \xE2\x98\x83"
94+
end
95+
end
8796
end
8897

8998
describe "when retrieving the current content" do

0 commit comments

Comments
 (0)