Skip to content

Commit

Permalink
Fixes over-eager unchanged check in write_tree
Browse files Browse the repository at this point in the history
Previously, if the trees were equal, normalization would not happen even
if there were formatting differences.
  • Loading branch information
glebm committed Aug 10, 2017
1 parent b58b8e6 commit 91b593d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v0.9.17

Fixes an issue with normalization not happening in certain cases.

## v0.9.16

Improves handling of interpolations in `translate-missing` when multiple interpolations are present.
Expand Down
14 changes: 9 additions & 5 deletions lib/i18n/tasks/data/file_formats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def adapter_dump(tree, format)
adapter_op :dump, format, tree, write_config(format)
end

# @return [Hash]
def adapter_parse(tree, format)
adapter_op :parse, format, tree, read_config(format)
end
Expand All @@ -34,19 +35,22 @@ def read_config(format)
(config[format] || {})[:read]
end

# @return [Hash]
def load_file(path)
adapter_parse ::File.read(path, encoding: 'UTF-8'), self.class.adapter_name_for_path(path)
adapter_parse read_file(path), self.class.adapter_name_for_path(path)
end

# @return [String]
def read_file(path)
::File.read(path, encoding: 'UTF-8')
end

def write_tree(path, tree)
hash = tree.to_hash(true)
adapter = self.class.adapter_name_for_path(path)
content = adapter_dump(hash, adapter)
# Ignore unchanged data
return if File.file?(path) &&
# Comparing hashes for equality directly would ignore key order.
# Round-trip through the adapter and compare the strings instead:
content == adapter_dump(load_file(path), adapter)
return if File.file?(path) && content == read_file(path)
::FileUtils.mkpath(File.dirname(path))
::File.open(path, 'w') { |f| f.write content }
end
Expand Down

0 comments on commit 91b593d

Please sign in to comment.