From 7608275089c1962284b3fee73366f0802a62c2ab Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Sun, 3 Mar 2013 18:13:41 -0800 Subject: [PATCH] Fix dynamic multi-byte utf-8 files rebuilding. Thor compares the new contents with the existing file by using File.binread(destination) == new_content. File.binread returns a string with ASCII_8BIT encoding, which will not match the new_content if new_content contains multi-byte utf-8. This patch simply encodes the new_content to ASCII_8BIT before passing it to Thor. --- .../features/unicode_filecontents.feature | 13 +++++++++++++ middleman-core/lib/middleman-core/cli/build.rb | 9 ++++++++- .../step_definitions/builder_steps.rb | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 middleman-core/features/unicode_filecontents.feature diff --git a/middleman-core/features/unicode_filecontents.feature b/middleman-core/features/unicode_filecontents.feature new file mode 100644 index 00000000..80b676a3 --- /dev/null +++ b/middleman-core/features/unicode_filecontents.feature @@ -0,0 +1,13 @@ +Feature: Unicode filecontents + In order to support non-ASCII characters in file contents + + Scenario: Rebuild with files containing unicode characters in their name + Given a fixture app "clean-app" + And a file named "source/index.html.erb" with: + """ + 你好 + """ + And a successfully built app at "clean-app" + And a modification time for a file named "build/index.html" + And a successfully built app at "clean-app" + Then the file "build/index.html" should not have been updated diff --git a/middleman-core/lib/middleman-core/cli/build.rb b/middleman-core/lib/middleman-core/cli/build.rb index 26680123..5fb11b20 100644 --- a/middleman-core/lib/middleman-core/cli/build.rb +++ b/middleman-core/lib/middleman-core/cli/build.rb @@ -137,7 +137,7 @@ module Middleman::Cli response = self.class.shared_rack.get(URI.escape(resource.destination_path)) if response.status == 200 - create_file(output_file, response.body) + create_file(output_file, binary_encode(response.body)) else handle_error(output_file, response.body) end @@ -160,6 +160,13 @@ module Middleman::Cli self.shell.error(response) end end + + def binary_encode(string) + if string.respond_to?(:force_encoding) + string.force_encoding("ascii-8bit") + end + string + end } end diff --git a/middleman-core/lib/middleman-core/step_definitions/builder_steps.rb b/middleman-core/lib/middleman-core/step_definitions/builder_steps.rb index be1bb082..954583b2 100644 --- a/middleman-core/lib/middleman-core/step_definitions/builder_steps.rb +++ b/middleman-core/lib/middleman-core/step_definitions/builder_steps.rb @@ -1,5 +1,9 @@ require 'fileutils' +Before do + @modification_times = Hash.new +end + Given /^app "([^\"]*)" is using config "([^\"]*)"$/ do |path, config_name| target = File.join(PROJECT_ROOT_PATH, "fixtures", path) config_path = File.join(current_dir, "config-#{config_name}.rb") @@ -50,6 +54,16 @@ Given /^a successfully built app at "([^\"]*)" with flags "([^\"]*)"$/ do |path, step %Q{was successfully built} end +Given /^a modification time for a file named "([^\"]*)"$/ do |file| + target = File.join(current_dir, file) + @modification_times[target] = File.mtime(target) +end + +Then /^the file "([^\"]*)" should not have been updated$/ do |file| + target = File.join(current_dir, file) + File.mtime(target).should == @modification_times[target] +end + # Provide this Aruba overload in case we're matching something with quotes in it Then /^the file "([^"]*)" should contain '([^']*)'$/ do |file, partial_content| check_file_content(file, partial_content, true)