[前][次][番号順一覧][スレッド一覧]

ruby-changes:70244

From: David <ko1@a...>
Date: Wed, 15 Dec 2021 19:47:00 +0900 (JST)
Subject: [ruby-changes:70244] 9f87c0cc6d (master): [rubygems/rubygems] Pass `:bimode` explicitly to `File.open`

https://git.ruby-lang.org/ruby.git/commit/?id=9f87c0cc6d

From 9f87c0cc6dff70e8a1c4d204d1d45920d26f2e3a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?David=20Rodr=C3=ADguez?= <deivid.rodriguez@r...>
Date: Mon, 13 Dec 2021 16:53:50 +0100
Subject: [rubygems/rubygems] Pass `:bimode` explicitly to `File.open`

The `File::BINARY` flag is apparently ignored due to a ruby bug, and
thus writing can cause encoding issues.

https://github.com/rubygems/rubygems/commit/db4efbebf2
---
 lib/rubygems.rb                          |  2 +-
 test/rubygems/test_gem_remote_fetcher.rb | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/lib/rubygems.rb b/lib/rubygems.rb
index b7dda38d522..40f3a893d84 100644
--- a/lib/rubygems.rb
+++ b/lib/rubygems.rb
@@ -800,7 +800,7 @@ An Array (#{env.inspect}) was passed in from #{caller[3]} https://github.com/ruby/ruby/blob/trunk/lib/rubygems.rb#L800
   ##
   # Safely write a file in binary mode on all platforms.
   def self.write_binary(path, data)
-    File.open(path, File::RDWR | File::CREAT | File::BINARY | File::LOCK_EX) do |io|
+    File.open(path, File::RDWR | File::CREAT | File::LOCK_EX, binmode: true) do |io|
       io.write data
     end
   rescue *WRITE_BINARY_ERRORS
diff --git a/test/rubygems/test_gem_remote_fetcher.rb b/test/rubygems/test_gem_remote_fetcher.rb
index 5ce420b91a8..3ce9be7c90a 100644
--- a/test/rubygems/test_gem_remote_fetcher.rb
+++ b/test/rubygems/test_gem_remote_fetcher.rb
@@ -173,6 +173,21 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg== https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_remote_fetcher.rb#L173
     assert_equal 'hello', File.read(path)
   end
 
+  def test_cache_update_path_with_utf8_internal_encoding
+    with_internal_encoding('UTF-8') do
+      uri = URI 'http://example/file'
+      path = File.join @tempdir, 'file'
+      data = String.new("\xC8").force_encoding(Encoding::BINARY)
+
+      fetcher = util_fuck_with_fetcher data
+
+      written_data = fetcher.cache_update_path uri, path
+
+      assert_equal data, written_data
+      assert_equal data, File.binread(path)
+    end
+  end
+
   def test_cache_update_path_no_update
     uri = URI 'http://example/file'
     path = File.join @tempdir, 'file'
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]