Skip to content

Commit 8c9817d

Browse files
authored
Merge pull request #782 from timdiggins/respect-implicit-encoding-of-thorfiles
Respect implicit encoding of thorfiles
2 parents b7173a4 + c4f445e commit 8c9817d

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

lib/thor/util.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def find_class_and_command_by_namespace(namespace, fallback = true)
150150
# inside the sandbox to avoid namespacing conflicts.
151151
#
152152
def load_thorfile(path, content = nil, debug = false)
153-
content ||= File.binread(path)
153+
content ||= File.read(path)
154154

155155
begin
156156
Thor::Sandbox.class_eval(content, path)

spec/encoding_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require "helper"
2+
require "thor/base"
3+
4+
5+
describe "file's encoding" do
6+
def load_thorfile(filename)
7+
Thor::Util.load_thorfile(File.expand_path("./fixtures/#{filename}", __dir__))
8+
end
9+
10+
it "respects explicit UTF-8" do
11+
load_thorfile("encoding_with_utf8.thor")
12+
expect(capture(:stdout) { Thor::Sandbox::EncodingWithUtf8.new.invoke(:encoding) }).to match(/ok/)
13+
end
14+
it "respects explicit non-UTF-8" do
15+
load_thorfile("encoding_other.thor")
16+
expect(capture(:stdout) { Thor::Sandbox::EncodingOther.new.invoke(:encoding) }).to match(/ok/)
17+
end
18+
it "has implicit UTF-8" do
19+
load_thorfile("encoding_implicit.thor")
20+
expect(capture(:stdout) { Thor::Sandbox::EncodingImplicit.new.invoke(:encoding) }).to match(/ok/)
21+
end
22+
end

spec/fixtures/encoding_implicit.thor

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
class EncodingImplicit < Thor
4+
SOME_STRING = "Some λέξεις 一些词 🎉"
5+
6+
desc "encoding", "tests that encoding is correct"
7+
8+
def encoding
9+
puts "#{SOME_STRING.inspect}: #{SOME_STRING.encoding}:"
10+
if SOME_STRING.encoding.name == "UTF-8"
11+
puts "ok"
12+
else
13+
puts "expected #{SOME_STRING.encoding.name} to equal UTF-8"
14+
end
15+
end
16+
end

spec/fixtures/encoding_other.thor

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# encoding: ISO-8859-7
2+
# frozen_string_literal: true
3+
4+
class EncodingOther < Thor
5+
SOME_STRING = "Some ëÝîåéò"
6+
7+
desc "encoding", "tests that encoding is correct"
8+
9+
def encoding
10+
puts "#{SOME_STRING.inspect}: #{SOME_STRING.encoding}:"
11+
if SOME_STRING.encoding.name == "ISO-8859-7"
12+
puts "ok"
13+
else
14+
puts "expected #{SOME_STRING.encoding.name} to equal ISO-8859-7"
15+
end
16+
end
17+
end

spec/fixtures/encoding_with_utf8.thor

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# encoding: UTF-8
2+
# frozen_string_literal: true
3+
4+
class EncodingWithUtf8 < Thor
5+
SOME_STRING = "Some λέξεις 一些词 🎉"
6+
7+
desc "encoding", "tests that encoding is correct"
8+
9+
def encoding
10+
puts "#{SOME_STRING.inspect}: #{SOME_STRING.encoding}:"
11+
if SOME_STRING.encoding.name == "UTF-8"
12+
puts "ok"
13+
else
14+
puts "expected #{SOME_STRING.encoding.name} to equal UTF-8"
15+
end
16+
end
17+
end

0 commit comments

Comments
 (0)