diff --git a/lib/thor/shell/basic.rb b/lib/thor/shell/basic.rb index 71ce4071..fba02349 100644 --- a/lib/thor/shell/basic.rb +++ b/lib/thor/shell/basic.rb @@ -314,7 +314,7 @@ def show_diff(destination, content) #:nodoc: diff_cmd = ENV["THOR_DIFF"] || ENV["RAILS_DIFF"] || "diff -u" require "tempfile" - Tempfile.open(File.basename(destination), File.dirname(destination)) do |temp| + Tempfile.open(File.basename(destination), File.dirname(destination), binmode: true) do |temp| temp.write content temp.rewind system %(#{diff_cmd} "#{destination}" "#{temp.path}") diff --git a/spec/actions/file_manipulation_spec.rb b/spec/actions/file_manipulation_spec.rb index 72919e09..4f8b5c9a 100644 --- a/spec/actions/file_manipulation_spec.rb +++ b/spec/actions/file_manipulation_spec.rb @@ -81,6 +81,29 @@ def file expect(File.stat(original).mode).to eq(File.stat(copy).mode) end + it "shows the diff when there is a collision and source has utf-8 characters" do + previous_internal = Encoding.default_internal + + silence_warnings do + Encoding.default_internal = Encoding::UTF_8 + end + + destination = File.join(destination_root, "encoding_with_utf8.thor") + FileUtils.mkdir_p(destination_root) + + File.write(destination, "blabla") + + expect(Thor::LineEditor).to receive(:readline).and_return("d", "y") + expect(runner.shell).to receive(:system).with(/diff -u/) + action :copy_file, "encoding_with_utf8.thor" + + exists_and_identical?("encoding_with_utf8.thor", "encoding_with_utf8.thor") + ensure + silence_warnings do + Encoding.default_internal = previous_internal + end + end + it "logs status" do expect(action(:copy_file, "command.thor")).to eq(" create command.thor\n") end