From 612518f9701c4fed65cd7a2d1579f99165edc8bc Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Fri, 28 Jun 2024 20:25:27 +0300 Subject: [PATCH] Fix StringIO#initialize and preserve initial string's encoding when mode is `w` so the initial string is truncated * close https://github.com/oracle/truffleruby/issues/3599 --- library/stringio/initialize_spec.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/library/stringio/initialize_spec.rb b/library/stringio/initialize_spec.rb index ad067a0be..49489acf0 100644 --- a/library/stringio/initialize_spec.rb +++ b/library/stringio/initialize_spec.rb @@ -130,6 +130,26 @@ -> { @io.send(:initialize, str, "w") }.should raise_error(Errno::EACCES) -> { @io.send(:initialize, str, "a") }.should raise_error(Errno::EACCES) end + + it "truncates all the content if passed w mode" do + io = StringIO.allocate + source = +"example".encode(Encoding::ISO_8859_1); + + io.send(:initialize, source, "w") + + io.string.should.empty? + io.string.encoding.should == Encoding::ISO_8859_1 + end + + it "truncates all the content if passed IO::TRUNC mode" do + io = StringIO.allocate + source = +"example".encode(Encoding::ISO_8859_1); + + io.send(:initialize, source, IO::TRUNC) + + io.string.should.empty? + io.string.encoding.should == Encoding::ISO_8859_1 + end end describe "StringIO#initialize when passed [Object]" do