Skip to content

Commit

Permalink
Merge pull request #782 from timdiggins/respect-implicit-encoding-of-…
Browse files Browse the repository at this point in the history
…thorfiles

Respect implicit encoding of thorfiles
  • Loading branch information
rafaelfranca authored Jun 17, 2022
2 parents b7173a4 + c4f445e commit 8c9817d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/thor/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def find_class_and_command_by_namespace(namespace, fallback = true)
# inside the sandbox to avoid namespacing conflicts.
#
def load_thorfile(path, content = nil, debug = false)
content ||= File.binread(path)
content ||= File.read(path)

begin
Thor::Sandbox.class_eval(content, path)
Expand Down
22 changes: 22 additions & 0 deletions spec/encoding_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "helper"
require "thor/base"


describe "file's encoding" do
def load_thorfile(filename)
Thor::Util.load_thorfile(File.expand_path("./fixtures/#{filename}", __dir__))
end

it "respects explicit UTF-8" do
load_thorfile("encoding_with_utf8.thor")
expect(capture(:stdout) { Thor::Sandbox::EncodingWithUtf8.new.invoke(:encoding) }).to match(/ok/)
end
it "respects explicit non-UTF-8" do
load_thorfile("encoding_other.thor")
expect(capture(:stdout) { Thor::Sandbox::EncodingOther.new.invoke(:encoding) }).to match(/ok/)
end
it "has implicit UTF-8" do
load_thorfile("encoding_implicit.thor")
expect(capture(:stdout) { Thor::Sandbox::EncodingImplicit.new.invoke(:encoding) }).to match(/ok/)
end
end
16 changes: 16 additions & 0 deletions spec/fixtures/encoding_implicit.thor
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class EncodingImplicit < Thor
SOME_STRING = "Some λέξεις 一些词 🎉"

desc "encoding", "tests that encoding is correct"

def encoding
puts "#{SOME_STRING.inspect}: #{SOME_STRING.encoding}:"
if SOME_STRING.encoding.name == "UTF-8"
puts "ok"
else
puts "expected #{SOME_STRING.encoding.name} to equal UTF-8"
end
end
end
17 changes: 17 additions & 0 deletions spec/fixtures/encoding_other.thor
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# encoding: ISO-8859-7
# frozen_string_literal: true

class EncodingOther < Thor
SOME_STRING = "Some ëÝîåéò"

desc "encoding", "tests that encoding is correct"

def encoding
puts "#{SOME_STRING.inspect}: #{SOME_STRING.encoding}:"
if SOME_STRING.encoding.name == "ISO-8859-7"
puts "ok"
else
puts "expected #{SOME_STRING.encoding.name} to equal ISO-8859-7"
end
end
end
17 changes: 17 additions & 0 deletions spec/fixtures/encoding_with_utf8.thor
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# encoding: UTF-8
# frozen_string_literal: true

class EncodingWithUtf8 < Thor
SOME_STRING = "Some λέξεις 一些词 🎉"

desc "encoding", "tests that encoding is correct"

def encoding
puts "#{SOME_STRING.inspect}: #{SOME_STRING.encoding}:"
if SOME_STRING.encoding.name == "UTF-8"
puts "ok"
else
puts "expected #{SOME_STRING.encoding.name} to equal UTF-8"
end
end
end

0 comments on commit 8c9817d

Please sign in to comment.