summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThe Bundler Bot <bot@bundler.io>2018-08-14 20:05:30 +0000
committerThe Bundler Bot <bot@bundler.io>2018-08-14 20:05:30 +0000
commite71418eeb1c16aa6bad8712b0a74696a6d8f1e36 (patch)
tree3ee3f4e49641f8b4ea1678cec8c19e3182085564
parent6476c457ae6ee01b7f859b9aebd8d8992daed6e5 (diff)
parenta98a849e8d72fb95f7c5ad995110785e64ead247 (diff)
downloadbundler-e71418eeb1c16aa6bad8712b0a74696a6d8f1e36.tar.gz
Auto merge of #6661 - eregon:consistent-encoding-for-reading-files, r=deivid-rodriguez
Use UTF-8 for reading files including Gemfile ### What was the end-user problem that led to this PR? See #6660 and https://github.com/oracle/truffleruby/issues/1410. ### What was your diagnosis of the problem? The above issue details the problem. ### What is your fix for the problem, implemented in this PR? To read the Gemfile and other files in Bundler with the default source encoding of Ruby, UTF-8, instead of the binary encoding which cannot interpret non-US-ASCII characters. ### Why did you choose this fix out of the possible options? Because it's what Ruby does for other source files. Fixes #6660.
-rw-r--r--lib/bundler.rb4
-rw-r--r--lib/bundler/env.rb2
-rw-r--r--spec/install/gemfile_spec.rb29
3 files changed, 32 insertions, 3 deletions
diff --git a/lib/bundler.rb b/lib/bundler.rb
index 6b62ac9b1c..5da316ec4d 100644
--- a/lib/bundler.rb
+++ b/lib/bundler.rb
@@ -424,7 +424,7 @@ EOF
def read_file(file)
SharedHelpers.filesystem_access(file, :read) do
- File.open(file, "rb", &:read)
+ File.open(file, "r:UTF-8", &:read)
end
end
@@ -445,7 +445,7 @@ EOF
def load_gemspec_uncached(file, validate = false)
path = Pathname.new(file)
- contents = File.open(file, "r:UTF-8", &:read)
+ contents = read_file(file)
spec = if contents.start_with?("---") # YAML header
eval_yaml_gemspec(path, contents)
else
diff --git a/lib/bundler/env.rb b/lib/bundler/env.rb
index b91a345cee..923a14de0c 100644
--- a/lib/bundler/env.rb
+++ b/lib/bundler/env.rb
@@ -61,7 +61,7 @@ module Bundler
end
def self.read_file(filename)
- File.read(filename.to_s).strip
+ Bundler.read_file(filename.to_s).strip
rescue Errno::ENOENT
"<No #{filename} found>"
rescue RuntimeError => e
diff --git a/spec/install/gemfile_spec.rb b/spec/install/gemfile_spec.rb
index 45bc232901..ad254a87e9 100644
--- a/spec/install/gemfile_spec.rb
+++ b/spec/install/gemfile_spec.rb
@@ -112,4 +112,33 @@ RSpec.describe "bundle install" do
end
end
end
+
+ context "with a Gemfile containing non-US-ASCII characters" do
+ it "reads the Gemfile with the UTF-8 encoding by default" do
+ skip "Ruby 1.8 has no encodings" if RUBY_VERSION < "1.9"
+
+ install_gemfile <<-G
+ str = "Il était une fois ..."
+ puts "The source encoding is: " + str.encoding.name
+ G
+
+ expect(out).to include("The source encoding is: UTF-8")
+ expect(out).not_to include("The source encoding is: ASCII-8BIT")
+ expect(out).to include("Bundle complete!")
+ end
+
+ it "respects the magic encoding comment" do
+ skip "Ruby 1.8 has no encodings" if RUBY_VERSION < "1.9"
+
+ # NOTE: This works thanks to #eval interpreting the magic encoding comment
+ install_gemfile <<-G
+ # encoding: iso-8859-1
+ str = "Il #{"\xE9".b}tait une fois ..."
+ puts "The source encoding is: " + str.encoding.name
+ G
+
+ expect(out).to include("The source encoding is: ISO-8859-1")
+ expect(out).to include("Bundle complete!")
+ end
+ end
end