Bug 1384570 - Part 3 - Normalize whitespace in a non-destructive manner. draft
authorStaś Małolepszy <stas@mozilla.com>
Tue, 05 Sep 2017 15:27:08 +0200
changeset 11618 6b9b9cbb1f629303a7128d04c2d3d76041c72e5b
parent 11617 35a7d1badb7d33264e98db460008431ca8f4f052
push id1776
push usersmalolepszy@mozilla.com
push dateTue, 05 Sep 2017 13:27:25 +0000
bugs1384570
Bug 1384570 - Part 3 - Normalize whitespace in a non-destructive manner. MozReview-Commit-ID: KqoXEveuBj7
cross-channel-l10n/mozxchannel/merge.py
cross-channel-l10n/tests/test-merge-whitespace.py
--- a/cross-channel-l10n/mozxchannel/merge.py
+++ b/cross-channel-l10n/mozxchannel/merge.py
@@ -75,33 +75,24 @@ def merge_two(comments, newer, older):
         return entity
 
     contents = [(key, get_entity(key)) for _, key in diff]
     pruned = [(k, v) for k, v in contents if v is not None]
     return OrderedDict(pruned)
 
 
 def serialize_legacy_resource(entities):
-    return "".join(map(serialize_entity, entities.values()))
+    return reduce(serialize_legacy_entity, entities.values(), "")
 
 
 def serialize_fluent_resource(entities):
     entries = [entity.entry for entity in entities.values()]
     resource = ast.Resource(entries)
     return serialize(resource)
 
 
-def serialize_entity(entity):
-    snippet = entity.all
+def serialize_legacy_entity(acc, entity):
+    # Ensure there's a newline at the end of the resource content so far before
+    # we append the current entity's content.
+    if acc and acc[-1] != "\n":
+        acc += "\n"
 
-    # Normalize _inline_ whitespace around the entity.  This is needed
-    # for when an entity has whitespace in the last blank line following
-    # it.  This may happen at the EOF. Without normalization entities
-    # appended after such entity would be incorrectly indented.
-    snippet = snippet.strip(" \t")
-
-    # Ensure that the last character after normalization is a newline. This
-    # prevents problems in scenarios opposite to above: when entities are
-    # appended after an entity with no EOL at the end.
-    if snippet[-1] != "\n":
-        snippet += "\n"
-
-    return snippet
+    return acc + entity.all
--- a/cross-channel-l10n/tests/test-merge-whitespace.py
+++ b/cross-channel-l10n/tests/test-merge-whitespace.py
@@ -12,17 +12,17 @@ class TestMergeWhitespace(unittest.TestC
         channels = ("""
 foo = Foo 1
       """, """
 foo = Foo 2
 """)
         self.assertEqual(
             merge_channels(self.name, *channels), """
 foo = Foo 1
-""")
+      """)
 
     def test_blank_lines_between_messages(self):
         channels = ("""
 foo = Foo 1
 
 bar = Bar 1
 """, """
 foo = Foo 2