changeset 129:d6aef0675953

Add a couple of CLI tests.
author cmlenz
date Mon, 18 Jun 2007 17:05:00 +0000
parents 24a711a3c174
children 383b18bfcc95
files babel/messages/frontend.py babel/messages/tests/frontend.py babel/numbers.py
diffstat 3 files changed, 206 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/babel/messages/frontend.py
+++ b/babel/messages/frontend.py
@@ -337,7 +337,7 @@
 
         :param argv: list of arguments passed on the command-line
         """
-        self.parser = OptionParser(usage=self.usage % ('subcommand', '[args]'),
+        self.parser = OptionParser(usage=self.usage % ('command', '[args]'),
                               version=self.version)
         self.parser.disable_interspersed_args()
         self.parser.print_help = self._help
@@ -347,15 +347,15 @@
 
         cmdname = args[0]
         if cmdname not in self.commands:
-            self.parser.error('unknown subcommand "%s"' % cmdname)
+            self.parser.error('unknown command "%s"' % cmdname)
 
         getattr(self, cmdname)(args[1:])
 
     def _help(self):
         print self.parser.format_help()
-        print "Subcommands:"
+        print "commands:"
         longest = max([len(command) for command in self.commands])
-        format = "    %" + str(longest) + "s  %s"
+        format = "  %%-%ds %%s" % max(11, longest)
         self.commands.sort()
         for command in self.commands:
             print format % (command, self.command_descriptions[command])
--- a/babel/messages/tests/frontend.py
+++ b/babel/messages/tests/frontend.py
@@ -17,6 +17,8 @@
 import doctest
 import os
 import shutil
+from StringIO import StringIO
+import sys
 import time
 import unittest
 
@@ -306,11 +308,210 @@
        open(po_file, 'U').read())
 
 
+class CommandLineInterfaceTestCase(unittest.TestCase):
+
+    def setUp(self):
+        self.datadir = os.path.join(os.path.dirname(__file__), 'data')
+        self.orig_stdout = sys.stdout
+        self.orig_stderr = sys.stderr
+        sys.stdout = StringIO()
+        sys.stderr = StringIO()
+        self.cli = frontend.CommandLineInterface()
+
+    def tearDown(self):
+        sys.stdout = self.orig_stdout
+        sys.stderr = self.orig_stderr
+
+    def test_usage(self):
+        try:
+            self.cli.run(['babel'])
+            self.fail('Expected SystemExit')
+        except SystemExit, e:
+            self.assertEqual(2, e.code)
+            self.assertEqual("""\
+usage: setup.py command [options] [args]
+
+setup.py: error: incorrect number of arguments
+""", sys.stderr.getvalue())
+
+    def test_help(self):
+        try:
+            self.cli.run(['babel', '--help'])
+            self.fail('Expected SystemExit')
+        except SystemExit, e:
+            self.assertEqual(0, e.code)
+            self.assertEqual("""\
+usage: setup.py command [options] [args]
+
+options:
+  --version   show program's version number and exit
+  -h, --help  show this help message and exit
+
+commands:
+  extract     extract messages from source files and generate a POT file
+  init        create new message catalogs from a template
+""", sys.stdout.getvalue())
+
+    def test_extract_with_default_mapping(self):
+        pot_file = os.path.join(self.datadir, 'project', 'i18n', 'temp.pot')
+        try:
+            self.cli.run(['babel', 'extract',
+                '--copyright-holder', 'FooBar, Inc.',
+                '--msgid-bugs-address', 'bugs.address@email.tld',
+                '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
+                '-o', pot_file, os.path.join(self.datadir, 'project')])
+        except SystemExit, e:
+            self.assertEqual(0, e.code)
+            assert os.path.isfile(pot_file)
+            self.assertEqual(
+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] ""
+
+#: project/CVS/this_wont_normally_be_here.py:11
+msgid "FooBar"
+msgid_plural "FooBars"
+msgstr[0] ""
+msgstr[1] ""
+
+""" % {'version': VERSION,
+       'year': time.strftime('%Y'),
+       'date': time.strftime('%Y-%m-%d %H:%M%z')}, open(pot_file, 'U').read())
+
+    def test_extract_with_mapping_file(self):
+        pot_file = os.path.join(self.datadir, 'project', 'i18n', 'temp.pot')
+        try:
+            self.cli.run(['babel', 'extract',
+                '--copyright-holder', 'FooBar, Inc.',
+                '--msgid-bugs-address', 'bugs.address@email.tld',
+                '--mapping', os.path.join(self.datadir, 'mapping.cfg'),
+                '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
+                '-o', pot_file, os.path.join(self.datadir, 'project')])
+        except SystemExit, e:
+            self.assertEqual(0, e.code)
+            assert os.path.isfile(pot_file)
+            self.assertEqual(
+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')}, open(pot_file, 'U').read())
+
+    def test_init_with_output_dir(self):
+        po_file = os.path.join(self.datadir, 'project', 'i18n', 'en_US',
+                               'LC_MESSAGES', 'messages.po')
+        try:
+            self.cli.run(['babel', 'init',
+                '--locale', 'en_US',
+                '-d', os.path.join(self.datadir, 'project', 'i18n'),
+                '-i', os.path.join(self.datadir, 'project', 'i18n',
+                                   'messages.pot')])
+        except SystemExit, e:
+            self.assertEqual(0, e.code)
+            assert os.path.isfile(pot_file)
+            self.assertEqual(
+r"""# English (United States) translations for TestProject.
+# Copyright (C) 2007 FooBar, Inc.
+# This file is distributed under the same license as the TestProject
+# project.
+# FIRST AUTHOR <EMAIL@ADDRESS>, 2007.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: TestProject 0.1\n"
+"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
+"POT-Creation-Date: 2007-04-01 15:30+0200\n"
+"PO-Revision-Date: %(date)s\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: en_US <LL@li.org>\n"
+"Plural-Forms: nplurals=2; plural=(n != 1)\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,
+       'date': time.strftime('%Y-%m-%d %H:%M%z')},
+       open(po_file, 'U').read())
+
+
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(doctest.DocTestSuite(frontend))
     suite.addTest(unittest.makeSuite(ExtractMessagesTestCase))
     suite.addTest(unittest.makeSuite(NewCatalogTestCase))
+    suite.addTest(unittest.makeSuite(CommandLineInterfaceTestCase))
     return suite
 
 if __name__ == '__main__':
--- a/babel/numbers.py
+++ b/babel/numbers.py
@@ -242,7 +242,7 @@
 NUMBER_PATTERN = r"(?P<number>%s+)" % NUMBER_TOKEN
 SUFFIX_PATTERN = r"(?P<suffix>.*)"
 
-number_re = re.compile(r"%s%s%s" % (PREFIX_PATTERN, NUMBER_PATTERN, 
+number_re = re.compile(r"%s%s%s" % (PREFIX_PATTERN, NUMBER_PATTERN,
                                     SUFFIX_PATTERN))
 
 # TODO:
Copyright (C) 2012-2017 Edgewall Software