[damned-lies] Fixed locale insertion in LINGUAS when in last position



commit 0930ebc333777c1c71a85949f99307d9dd0959dc
Author: Claude Paroz <claude 2xlibre net>
Date:   Fri Aug 22 11:49:33 2014 +0200

    Fixed locale insertion in LINGUAS when in last position
    
    Fixes bug #735209

 stats/models.py      |   12 +------
 stats/tests/tests.py |   85 +++++++++++++++++++++++++++++---------------------
 stats/utils.py       |   13 ++++++++
 3 files changed, 63 insertions(+), 47 deletions(-)
---
diff --git a/stats/models.py b/stats/models.py
index 11226d9..00beaba 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -698,17 +698,7 @@ class Branch(models.Model):
                     # Add locale to LINGUAS
                     linguas_file = os.path.join(commit_dir, "LINGUAS")
                     if os.access(linguas_file, os.F_OK):
-                        fin = open(linguas_file, 'r')
-                        fout = open(linguas_file+"~", 'w')
-                        lang_written = False
-                        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)
-                        fout.close()
-                        fin.close()
-                        os.rename(linguas_file+"~", linguas_file)
+                        utils.insert_locale_in_linguas(linguas_file, locale)
                         utils.run_shell_command(
                             "cd \"%(dest)s\" && git add %(lg_file)s" % var_dict, raise_on_error=True)
                     var_dict['msg'] = "Added %s translation" % language.name
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index 76755c1..49c7473 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -177,40 +177,6 @@ class ModuleTestCase(TestCase):
         self.assertEqual(mail.outbox[0].subject, "String additions to 'gnome-hello.master'")
         self.assertIn('"%s"' % new_string, mail.outbox[0].body)
 
-    def test_read_file_variable(self):
-        file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "help_docbook", "Makefile.am")
-        var_content = utils.search_variable_in_file(file_path, "DOC_INCLUDES")
-        self.assertEqual(var_content.split(), ['rnusers.xml', 'rnlookingforward.xml', '$(NULL)'])
-
-    def test_generate_doc_potfile_docbook(self):
-        """
-        Test Docbook-style help
-        """
-        help_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "help_docbook")
-        self.addCleanup(os.remove, os.path.join(help_path, 'C', "release-notes.pot"))
-        potfile, errs, doc_format = utils.generate_doc_pot_file(help_path, 'release-notes', 'release-notes')
-        self.assertEqual(doc_format.__dict__, {'tool': 'xml2po', 'format': 'docbook'})
-        pot_path = os.path.join(help_path, "C", "release-notes.pot")
-        self.assertTrue(os.access(pot_path, os.R_OK))
-        res = utils.get_fig_stats(pot_path, doc_format=doc_format)
-        self.assertEqual(len(res), 1)
-        self.assertEqual(res[0]['path'], "figures/rnusers.nautilus.png")
-
-    def test_generate_doc_potfile_mallard(self):
-        """
-        Test Mallard-style help (with itstool)
-        """
-        help_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "help_mallard")
-        self.addCleanup(os.remove, os.path.join(help_path, 'C', 'gnome-hello.pot'))
-        potfile, errs, doc_format = utils.generate_doc_pot_file(help_path, 'gnome-hello', 'gnome-hello')
-        self.assertEqual(potfile, os.path.join(help_path, 'C', 'gnome-hello.pot'))
-        self.assertEqual(errs, [])
-        self.assertEqual(doc_format.__dict__, {'tool': 'itstool', 'format': 'mallard'})
-        res = utils.get_fig_stats(potfile, doc_format=doc_format)
-        self.assertEqual(len(res), 1)
-        self.assertEqual(res[0]['path'], "figures/gnome-hello-logo.png")
-        self.assertEqual(res[0]['hash'], "1ae47b7a7c4fbeb1f6bb72c0cf18d389")
-
     @test_scratchdir
     def test_http_pot(self):
         dom = Domain.objects.create(
@@ -276,6 +242,10 @@ class ModuleTestCase(TestCase):
                 self.assertIn(cmd, cmds[idx])
             self.assertIn('intltool-update -g', cmds[-1])
 
+        # Check lang has been inserted in LINGUAS file, before 'fr'
+        with open(os.path.join(branch.co_path(), domain.directory, 'LINGUAS')) as fh:
+            self.assertIn('bem\nfr', fh.read())
+
         # Documentation
         domain = self.mod.domain_set.get(name='help')
         git_ops = ('git checkout master', 'git pull', 'git add fr/fr.po',
@@ -377,16 +347,59 @@ class FigureTests(TestCase):
     @test_scratchdir
     def test_identical_figure_warning(self):
         """ Detect warning if translated figure is identical to original figure """
-        from stats.utils import check_identical_figures
         branch = Branch.objects.get(module__name='gnome-hello', name='master')
         pot_stat = Statistics.objects.get(branch=branch, domain__name='help', language__isnull=True)
         fig_path = os.path.join(pot_stat.branch.co_path(), pot_stat.domain.directory, 'C', 
pot_stat.get_figures()[0]['path'])
         shutil.copyfile(fig_path, fig_path.replace('/C/', '/fr/'))
         doc_stat = Statistics.objects.get(branch=branch, domain__name='help', language__locale='fr')
-        errs = check_identical_figures(doc_stat.get_figures(), os.path.join(branch.co_path(), 'help'), 'fr')
+        errs = utils.check_identical_figures(doc_stat.get_figures(), os.path.join(branch.co_path(), 'help'), 
'fr')
         self.assertEqual(len(errs), 1)
         self.assertTrue(errs[0][1].startswith("Figures should not be copied"))
 
+
+class UtilsTests(TestCase):
+    def test_read_file_variable(self):
+        file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "help_docbook", "Makefile.am")
+        var_content = utils.search_variable_in_file(file_path, "DOC_INCLUDES")
+        self.assertEqual(var_content.split(), ['rnusers.xml', 'rnlookingforward.xml', '$(NULL)'])
+
+    def test_generate_doc_potfile_docbook(self):
+        """
+        Test Docbook-style help
+        """
+        help_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "help_docbook")
+        self.addCleanup(os.remove, os.path.join(help_path, 'C', "release-notes.pot"))
+        potfile, errs, doc_format = utils.generate_doc_pot_file(help_path, 'release-notes', 'release-notes')
+        self.assertEqual(doc_format.__dict__, {'tool': 'xml2po', 'format': 'docbook'})
+        pot_path = os.path.join(help_path, "C", "release-notes.pot")
+        self.assertTrue(os.access(pot_path, os.R_OK))
+        res = utils.get_fig_stats(pot_path, doc_format=doc_format)
+        self.assertEqual(len(res), 1)
+        self.assertEqual(res[0]['path'], "figures/rnusers.nautilus.png")
+
+    def test_generate_doc_potfile_mallard(self):
+        """
+        Test Mallard-style help (with itstool)
+        """
+        help_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "help_mallard")
+        self.addCleanup(os.remove, os.path.join(help_path, 'C', 'gnome-hello.pot'))
+        potfile, errs, doc_format = utils.generate_doc_pot_file(help_path, 'gnome-hello', 'gnome-hello')
+        self.assertEqual(potfile, os.path.join(help_path, 'C', 'gnome-hello.pot'))
+        self.assertEqual(errs, [])
+        self.assertEqual(doc_format.__dict__, {'tool': 'itstool', 'format': 'mallard'})
+        res = utils.get_fig_stats(potfile, doc_format=doc_format)
+        self.assertEqual(len(res), 1)
+        self.assertEqual(res[0]['path'], "figures/gnome-hello-logo.png")
+        self.assertEqual(res[0]['hash'], "1ae47b7a7c4fbeb1f6bb72c0cf18d389")
+
+    @test_scratchdir
+    def test_insert_locale_in_linguas(self):
+        linguas_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 
'scratch/git/gnome-hello/po/LINGUAS')
+        utils.insert_locale_in_linguas(linguas_path, 'xx')
+        with open(linguas_path) as fh:
+            self.assertTrue(fh.read().endswith('xx\n'))
+
+
 class OtherTests(TestCase):
     def test_bugzilla_linkify(self):
         from stats.templatetags.stats_extras import bugzilla_linkify
diff --git a/stats/utils.py b/stats/utils.py
index 3a56592..c6a3106 100644
--- a/stats/utils.py
+++ b/stats/utils.py
@@ -406,6 +406,19 @@ def read_linguas_file(full_path):
     return {'langs':langs,
             'error': ugettext_noop("Entry for this language is not present in LINGUAS file.") }
 
+def insert_locale_in_linguas(linguas_path, locale):
+    with open(linguas_path, 'r') as fin:
+        with open(linguas_path + "~", 'w') as fout:
+            lang_written = False
+            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")
+    os.rename(linguas_path + "~", linguas_path)
+
 def get_ui_linguas(module_path, po_path):
     """Get language list in one of po/LINGUAS, configure.ac or configure.in"""
 


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