Ensure EOL in serialized entities draft
authorStaś Małolepszy <stas@mozilla.com>
Mon, 04 Sep 2017 18:58:02 +0200
changeset 11612 7163cf52a3b41d10f1246526ccb10dec541dcb22
parent 11611 932f4b9ebb20ffcb97eb804bc53e643782b7a2a4
push id1771
push usersmalolepszy@mozilla.com
push dateMon, 04 Sep 2017 17:22:33 +0000
Ensure EOL in serialized entities MozReview-Commit-ID: JD3oBweZyai
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
@@ -43,9 +43,17 @@ def merge_two(newer, older):
 
 def serialize(entity):
     snippet = entity.all
 
     # 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.
-    return snippet.strip(" \t")
+    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
--- a/cross-channel-l10n/tests/test-merge-whitespace.py
+++ b/cross-channel-l10n/tests/test-merge-whitespace.py
@@ -29,8 +29,20 @@ foo = Foo 2
 bar = Bar 2
 """)
         self.assertEqual(
             merge_channels(self.name, *channels), """
 foo = Foo 1
 
 bar = Bar 1
 """)
+
+    def test_no_eol(self):
+        channels = ("""
+foo = Foo 1""", """
+foo = Foo 2
+bar = Bar 2
+""")
+        self.assertEqual(
+            merge_channels(self.name, *channels), """
+foo = Foo 1
+bar = Bar 2
+""")