diff --git a/lib/bundler.rb b/lib/bundler.rb index bd6fe4d0bfb..52bbc57fb5f 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -410,7 +410,7 @@ def sudo(str) def read_file(file) SharedHelpers.filesystem_access(file, :read) do - File.open(file, "rb", &:read) + File.open(file, "r:UTF-8", &:read) end end @@ -431,7 +431,7 @@ def load_gemspec(file, validate = false) 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 58fe20dbe70..78880adfc2f 100644 --- a/lib/bundler/env.rb +++ b/lib/bundler/env.rb @@ -61,7 +61,7 @@ def self.report(options = {}) end def self.read_file(filename) - File.read(filename.to_s).strip + Bundler.read_file(filename.to_s).strip rescue Errno::ENOENT "" rescue => e diff --git a/spec/install/gemfile_spec.rb b/spec/install/gemfile_spec.rb index 945d9f485d6..0a17fb3f820 100644 --- a/spec/install/gemfile_spec.rb +++ b/spec/install/gemfile_spec.rb @@ -110,4 +110,33 @@ 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