[damned-lies] Ensure all new languages are on their own line in LINGUAS files



commit 040bdfd01dabd8887593372532109623082ffbcb
Author: Claude Paroz <claude 2xlibre net>
Date:   Mon Jul 5 18:31:36 2021 +0200

    Ensure all new languages are on their own line in LINGUAS files

 stats/tests/tests.py | 12 +++++++++++-
 stats/utils.py       |  3 ++-
 2 files changed, 13 insertions(+), 2 deletions(-)
---
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index aa7cacb3..d5feab6d 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -929,7 +929,17 @@ class UtilsTests(TestModuleBase):
     def test_insert_locale_in_linguas(self):
         linguas_path = settings.SCRATCHDIR / 'git' / 'gnome-hello' / 'po' / 'LINGUAS'
         utils.insert_locale_in_linguas(linguas_path, 'xx')
-        self.assertTrue(linguas_path.read_text().endswith('xx\n'))
+        self.assertTrue(linguas_path.read_text().endswith('\nxx\n'))
+        # Try with file without ending new line
+        with linguas_path.open('rb+') as fh:
+            fh.seek(-1, os.SEEK_END)
+            fh.truncate()
+        utils.insert_locale_in_linguas(linguas_path, 'xy')
+        self.assertTrue(linguas_path.read_text().endswith('\nxy\n'))
+        # Also works if file is initially empty
+        os.truncate(linguas_path, 0)
+        utils.insert_locale_in_linguas(linguas_path, 'xz')
+        self.assertEqual(linguas_path.read_text(), 'xz\n')
 
     def test_exclude_messages_from_potfile(self):
         pot_file = Path(__file__).parent / 'donottranslate.pot'
diff --git a/stats/utils.py b/stats/utils.py
index c76fcd4d..c2123c02 100644
--- a/stats/utils.py
+++ b/stats/utils.py
@@ -609,13 +609,14 @@ def insert_locale_in_linguas(linguas_path, locale):
     temp_linguas = linguas_path.parent / (linguas_path.name + "~")
     with linguas_path.open('r') as fin, temp_linguas.open('w') as fout:
         lang_written = False
+        line = '\n'
         for line in fin:
             if not lang_written and line[0] != "#" and line[:5] > locale[:5]:
                 fout.write(locale + "\n")
                 lang_written = True
             fout.write(line)
         if not lang_written:
-            fout.write(locale + "\n")
+            fout.write(('\n' if not line.endswith('\n') else '') + locale + "\n")
     temp_linguas.replace(linguas_path)
 
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]