changeset 117:41506e62701f

Made new frontend tests more ''unit-y'', i.e. calling the APIs directly instead of launching the scripts.
author cmlenz
date Fri, 15 Jun 2007 19:26:35 +0000
parents 9a02e198d47a
children de3d43b538a4
files babel/messages/frontend.py babel/messages/tests/data/mapping.cfg babel/messages/tests/data/project/templates/index.html babel/messages/tests/data/project/templates/index.xhtml babel/messages/tests/data/project/templates/layout.html babel/messages/tests/data/project/templates/layout.xhtml babel/messages/tests/data/setup.cfg babel/messages/tests/frontend.py doc/setup.txt
diffstat 9 files changed, 125 insertions(+), 96 deletions(-) [+]
line wrap: on
line diff
--- a/babel/messages/frontend.py
+++ b/babel/messages/frontend.py
@@ -101,7 +101,8 @@
 
     def initialize_options(self):
         self.charset = 'utf-8'
-        self.keywords = self._keywords = DEFAULT_KEYWORDS.copy()
+        self.keywords = ''
+        self._keywords = DEFAULT_KEYWORDS.copy()
         self.no_default_keywords = False
         self.mapping_file = None
         self.no_location = False
@@ -123,10 +124,11 @@
                                        'disable the default ones')
         if self.no_default_keywords:
             self._keywords = {}
-        if isinstance(self.keywords, basestring):
+        if self.keywords:
             self._keywords.update(parse_keywords(self.keywords.split()))
-        self.keywords = self._keywords
 
+        if not self.output_file:
+            raise DistutilsOptionError('no output file specified')
         if self.no_wrap and self.width:
             raise DistutilsOptionError("'--no-wrap' and '--width' are mutually "
                                        "exclusive")
@@ -170,7 +172,7 @@
                              % (filepath, optstr))
 
                 extracted = extract_from_dir(dirname, method_map, options_map,
-                                             keywords=self.keywords,
+                                             keywords=self._keywords,
                                              comment_tags=self._add_comments,
                                              callback=callback)
                 for filename, lineno, message, comments in extracted:
--- a/babel/messages/tests/data/mapping.cfg
+++ b/babel/messages/tests/data/mapping.cfg
@@ -3,7 +3,3 @@
 
 # Extraction from Python source files
 [python: **.py]
-
-# Ignore extraction from HTML and XHTML templates
-[ignore: **/templates/**.html]
-[ignore: **/templates/**.xhtml]
deleted file mode 100644
--- a/babel/messages/tests/data/project/templates/index.html
+++ /dev/null
@@ -1,14 +0,0 @@
-<!DOCTYPE html
-    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
-    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml"
-      xmlns:py="http://genshi.edgewall.org/"
-      xmlns:xi="http://www.w3.org/2001/XInclude">
-  <head>
-    <title>Just a Tests Test</title>
-  </head>
-
-  <body>
-  </body>
-</html>
-
deleted file mode 100644
--- a/babel/messages/tests/data/project/templates/index.xhtml
+++ /dev/null
@@ -1,14 +0,0 @@
-<!DOCTYPE html
-    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
-    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml"
-      xmlns:py="http://genshi.edgewall.org/"
-      xmlns:xi="http://www.w3.org/2001/XInclude">
-  <head>
-    <title>Just a Tests Test</title>
-  </head>
-
-  <body>
-  </body>
-</html>
-
deleted file mode 100644
--- a/babel/messages/tests/data/project/templates/layout.html
+++ /dev/null
@@ -1,14 +0,0 @@
-<!DOCTYPE html
-    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
-    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml"
-      xmlns:py="http://genshi.edgewall.org/"
-      xmlns:xi="http://www.w3.org/2001/XInclude">
-  <head>
-    <title>Just a Tests Test</title>
-  </head>
-
-  <body>
-  </body>
-</html>
-
deleted file mode 100644
--- a/babel/messages/tests/data/project/templates/layout.xhtml
+++ /dev/null
@@ -1,14 +0,0 @@
-<!DOCTYPE html
-    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
-    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml"
-      xmlns:py="http://genshi.edgewall.org/"
-      xmlns:xi="http://www.w3.org/2001/XInclude">
-  <head>
-    <title>Just a Tests Test</title>
-  </head>
-
-  <body>
-  </body>
-</html>
-
--- a/babel/messages/tests/data/setup.cfg
+++ b/babel/messages/tests/data/setup.cfg
@@ -3,4 +3,3 @@
 copyright_holder = FooBar, TM
 add_comments = TRANSLATOR:,TRANSLATORS:
 output_file = project/i18n/project.pot
-
--- a/babel/messages/tests/frontend.py
+++ b/babel/messages/tests/frontend.py
@@ -11,33 +11,67 @@
 # individuals. For the exact contribution history, see the revision
 # history and logs, available at http://babel.edgewall.org/log/.
 
+from distutils.dist import Distribution
+from distutils.errors import DistutilsOptionError, DistutilsSetupError
+from distutils.log import _global_log
 import doctest
+import os
+import time
 import unittest
-import os
-import subprocess
 
-from babel.messages import frontend
 from babel import __version__ as VERSION
-import time
+from babel.messages import frontend
 
-class TestDistutilsSetuptoolsFrontend(unittest.TestCase):
-    
+
+class ExtractMessagesTestCase(unittest.TestCase):
+
     def setUp(self):
         self.olddir = os.getcwd()
         self.datadir = os.path.join(os.path.dirname(__file__), 'data')
         os.chdir(self.datadir)
-        # Do we need to run egg_info?
-        if not os.path.exists(os.path.join(self.datadir,
-                                           'TestProject.egg-info')):
-            subprocess.call(['./setup.py', 'egg_info'])
-    
-    def test_extracted_pot_no_mapping(self):
-        subprocess.call([os.path.join(self.datadir, 'setup.py'),
-                         'extract_messages'])
+        _global_log.threshold = 5 # shut up distutils logging
+
+        self.dist = Distribution(dict(
+            name='TestProject',
+            version='0.1',
+            packages=['project']
+        ))
+        self.cmd = frontend.extract_messages(self.dist)
+        self.cmd.initialize_options()
+
+    def tearDown(self):
+        pot_file = os.path.join(self.datadir, 'project', 'i18n', 'messages.pot')
+        if os.path.isfile(pot_file):
+            os.unlink(pot_file)
+
+        os.chdir(self.olddir)
+
+    def test_neither_default_nor_custom_keywords(self):
+        self.cmd.output_file = 'foobar'
+        self.cmd.no_default_keywords = True
+        self.assertRaises(DistutilsOptionError, self.cmd.finalize_options)
+
+    def test_no_output_file_specified(self):
+        self.assertRaises(DistutilsOptionError, self.cmd.finalize_options)
+
+    def test_both_sort_output_and_sort_by_file(self):
+        self.cmd.output_file = 'foobar'
+        self.cmd.sort_output = True
+        self.cmd.sort_by_file = True
+        self.assertRaises(DistutilsOptionError, self.cmd.finalize_options)
+
+    def test_extraction_with_default_mapping(self):
+        self.cmd.copyright_holder = 'FooBar, Inc.'
+        self.cmd.msgid_bugs_address = 'bugs.address@email.tld'
+        self.cmd.output_file = 'project/i18n/messages.pot'
+        self.cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
+
+        self.cmd.finalize_options()
+        self.cmd.run()
         self.assertEqual(open(os.path.join(self.datadir, 'project', 'i18n',
-                                           'project.pot'), 'r').read(), \
+                                           'messages.pot'), 'U').read(),
 r"""# Translations template for TestProject.
-# Copyright (C) %(year)s FooBar, TM
+# Copyright (C) %(year)s FooBar, Inc.
 # This file is distributed under the same license as the TestProject
 # project.
 # FIRST AUTHOR <EMAIL@ADDRESS>, %(year)s.
@@ -77,15 +111,20 @@
 """ % {'version': VERSION,
        'year': time.strftime('%Y'),
        'date': time.strftime('%Y-%m-%d %H:%M%z')})
-        
-    def test_extracted_pot_with_mapping(self):
-        subprocess.call([os.path.join(self.datadir, 'setup.py'),
-                         'extract_messages', '-F',
-                         os.path.join(self.datadir, 'mapping.cfg')])
+
+    def test_extraction_with_mapping_file(self):
+        self.cmd.copyright_holder = 'FooBar, Inc.'
+        self.cmd.msgid_bugs_address = 'bugs.address@email.tld'
+        self.cmd.mapping_file = 'mapping.cfg'
+        self.cmd.output_file = 'project/i18n/messages.pot'
+        self.cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
+
+        self.cmd.finalize_options()
+        self.cmd.run()
         self.assertEqual(open(os.path.join(self.datadir, 'project', 'i18n',
-                                           'project.pot'), 'r').read(),
+                                           'messages.pot'), 'r').read(),
 r"""# Translations template for TestProject.
-# Copyright (C) %(year)s FooBar, TM
+# Copyright (C) %(year)s FooBar, Inc.
 # This file is distributed under the same license as the TestProject
 # project.
 # FIRST AUTHOR <EMAIL@ADDRESS>, %(year)s.
@@ -118,16 +157,65 @@
 
 """ % {'version': VERSION,
        'year': time.strftime('%Y'),
-       'date': time.strftime('%Y-%m-%d %H:%M%z')})                                           
-        
-    def tearDown(self):
-        os.chdir(self.olddir)
+       'date': time.strftime('%Y-%m-%d %H:%M%z')})
+
+    def test_extraction_with_mapping_dict(self):
+        self.dist.message_extractors = {
+            'project': [
+                ('**/CVS/**.*', 'ignore',   None),
+                ('**.py',       'python',   None),
+            ]
+        }
+        self.cmd.copyright_holder = 'FooBar, Inc.'
+        self.cmd.msgid_bugs_address = 'bugs.address@email.tld'
+        self.cmd.output_file = 'project/i18n/messages.pot'
+        self.cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
+
+        self.cmd.finalize_options()
+        self.cmd.run()
+        self.assertEqual(open(os.path.join(self.datadir, 'project', 'i18n',
+                                           'messages.pot'), 'r').read(),
+r"""# Translations template for TestProject.
+# Copyright (C) %(year)s FooBar, Inc.
+# This file is distributed under the same license as the TestProject
+# project.
+# FIRST AUTHOR <EMAIL@ADDRESS>, %(year)s.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: TestProject 0.1\n"
+"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
+"POT-Creation-Date: %(date)s\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=utf-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Generated-By: Babel %(version)s\n"
+
+#. This will be a translator coment,
+#. that will include several lines
+#: project/file1.py:8
+msgid "bar"
+msgstr ""
+
+#: project/file2.py:9
+msgid "foobar"
+msgid_plural "foobars"
+msgstr[0] ""
+msgstr[1] ""
+
+""" % {'version': VERSION,
+       'year': time.strftime('%Y'),
+       'date': time.strftime('%Y-%m-%d %H:%M%z')})
 
 
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(doctest.DocTestSuite(frontend))
-    suite.addTest(unittest.makeSuite(TestDistutilsSetuptoolsFrontend))
+    suite.addTest(unittest.makeSuite(ExtractMessagesTestCase))
     return suite
 
 if __name__ == '__main__':
--- a/doc/setup.txt
+++ b/doc/setup.txt
@@ -74,9 +74,9 @@
         
         message_extractors = {
             'foobar': [
-                ('**.py',                ('python', None),
-                ('**/templates/**.html', ('genshi', None),
-                ('**/templates/**.txt',  ('genshi', {
+                ('**.py',                'python', None),
+                ('**/templates/**.html', 'genshi', None),
+                ('**/templates/**.txt',  'genshi', {
                     'template_class': 'genshi.template.text.TextTemplate'
                 })
             ],
Copyright (C) 2012-2017 Edgewall Software