Skip to content

Commit 2824cd4

Browse files
authored
Merge pull request #786 from tomclose/fix-create-file-identical-utf-8-bug
CreateFile#identical? fixed for files containing multi-byte UTF-8 codepoints
2 parents 005597b + 00b0829 commit 2824cd4

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/thor/actions/create_file.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def initialize(base, destination, data, config = {})
4343
# Boolean:: true if it is identical, false otherwise.
4444
#
4545
def identical?
46-
exists? && File.binread(destination) == render
46+
# binread uses ASCII-8BIT, so to avoid false negatives, the string must use the same
47+
exists? && File.binread(destination) == String.new(render).force_encoding("ASCII-8BIT")
4748
end
4849

4950
# Holds the content to be added to the file.

spec/actions/create_file_spec.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
::FileUtils.rm_rf(destination_root)
88
end
99

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

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

1717
def invoke!
@@ -204,5 +204,12 @@ def silence!
204204
invoke!
205205
expect(@action.identical?).to be true
206206
end
207+
208+
it "returns true if the destination file exists and is identical and contains multi-byte UTF-8 codepoints" do
209+
create_file("doc/config.rb", {}, {}, "€")
210+
expect(@action.identical?).to be false
211+
invoke!
212+
expect(@action.identical?).to be true
213+
end
207214
end
208215
end

0 commit comments

Comments
 (0)