Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/thor/actions/create_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def initialize(base, destination, data, config = {})
# Boolean:: true if it is identical, false otherwise.
#
def identical?
exists? && File.binread(destination) == render
# binread uses ASCII-8BIT, so to avoid false negatives, the string must use the same
exists? && File.binread(destination) == String.new(render).force_encoding("ASCII-8BIT")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a comment here indicating that binread defaults to this encoding to explain why this works.

end

# Holds the content to be added to the file.
Expand Down
11 changes: 9 additions & 2 deletions spec/actions/create_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
::FileUtils.rm_rf(destination_root)
end

def create_file(destination = nil, config = {}, options = {})
def create_file(destination = nil, config = {}, options = {}, contents = "CONFIGURATION")
@base = MyCounter.new([1, 2], options, :destination_root => destination_root)
allow(@base).to receive(:file_name).and_return("rdoc")

@action = Thor::Actions::CreateFile.new(@base, destination, "CONFIGURATION", {:verbose => !@silence}.merge(config))
@action = Thor::Actions::CreateFile.new(@base, destination, contents, {:verbose => !@silence}.merge(config))
end

def invoke!
Expand Down Expand Up @@ -196,5 +196,12 @@ def silence!
invoke!
expect(@action.identical?).to be true
end

it "returns true if the destination file exists and is identical and contains multi-byte UTF-8 codepoints" do
create_file("doc/config.rb", {}, {}, "€")
expect(@action.identical?).to be false
invoke!
expect(@action.identical?).to be true
end
end
end