]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Unwrap CLI testcases
authorAarni Koskela <akx@iki.fi>
Thu, 25 Dec 2025 14:58:42 +0000 (16:58 +0200)
committerAarni Koskela <akx@iki.fi>
Thu, 25 Dec 2025 16:07:19 +0000 (18:07 +0200)
tests/messages/frontend/test_cli.py
tests/messages/frontend/test_extract.py
tests/messages/frontend/test_frontend.py
tests/messages/frontend/test_init.py

index 4fea01b227f048b7d748e1b403764fd0038db1fc..8c04af47d607bd12a92de76d653b506a50ba361d 100644 (file)
@@ -17,7 +17,6 @@ import os
 import shutil
 import sys
 import time
-import unittest
 from datetime import datetime, timedelta
 from io import StringIO
 
@@ -30,119 +29,105 @@ from babel.messages import Catalog, frontend
 from babel.messages.frontend import BaseError
 from babel.messages.pofile import read_po, write_po
 from babel.util import LOCALTZ
-from tests.messages.consts import data_dir, get_po_file_path, i18n_dir, pot_file, this_dir
-
-
-class CommandLineInterfaceTestCase(unittest.TestCase):
-
-    def setUp(self):
-        data_dir = os.path.join(this_dir, 'data')
-        self.orig_working_dir = os.getcwd()
-        self.orig_argv = sys.argv
-        self.orig_stdout = sys.stdout
-        self.orig_stderr = sys.stderr
-        sys.argv = ['pybabel']
-        sys.stdout = StringIO()
-        sys.stderr = StringIO()
-        os.chdir(data_dir)
-
-        self._remove_log_handlers()
-        self.cli = frontend.CommandLineInterface()
-
-    def tearDown(self):
-        os.chdir(self.orig_working_dir)
-        sys.argv = self.orig_argv
-        sys.stdout = self.orig_stdout
-        sys.stderr = self.orig_stderr
-        for dirname in ['lv_LV', 'ja_JP']:
-            locale_dir = os.path.join(i18n_dir, dirname)
-            if os.path.isdir(locale_dir):
-                shutil.rmtree(locale_dir)
-        self._remove_log_handlers()
-
-    def _remove_log_handlers(self):
-        # Logging handlers will be reused if possible (#227). This breaks the
-        # implicit assumption that our newly created StringIO for sys.stderr
-        # contains the console output. Removing the old handler ensures that a
-        # new handler with our new StringIO instance will be used.
-        log = logging.getLogger('babel')
-        for handler in log.handlers:
-            log.removeHandler(handler)
-
-    def test_usage(self):
-        try:
-            self.cli.run(sys.argv)
-            self.fail('Expected SystemExit')
-        except SystemExit as e:
-            assert e.code == 2
-            assert sys.stderr.getvalue().lower() == """\
+from tests.messages.consts import data_dir, get_po_file_path, i18n_dir, pot_file
+
+
+@pytest.fixture
+def cli(monkeypatch, capsys):
+    monkeypatch.chdir(data_dir)
+    monkeypatch.setattr(sys, 'argv', ['pybabel'])
+    _remove_log_handlers()
+    yield frontend.CommandLineInterface()
+    for dirname in ['lv_LV', 'ja_JP']:
+        locale_dir = os.path.join(i18n_dir, dirname)
+        if os.path.isdir(locale_dir):
+            shutil.rmtree(locale_dir)
+    _remove_log_handlers()
+
+
+def _remove_log_handlers():
+    # Logging handlers will be reused if possible (#227). This breaks the
+    # implicit assumption that our newly created StringIO for sys.stderr
+    # contains the console output. Removing the old handler ensures that a
+    # new handler with our new StringIO instance will be used.
+    log = logging.getLogger('babel')
+    for handler in log.handlers:
+        log.removeHandler(handler)
+
+
+def test_usage(cli):
+    with pytest.raises(SystemExit) as ei:
+        cli.run(["pybabel"])
+    assert ei.value.code == 2
+    assert sys.stderr.getvalue().lower() == """\
 usage: pybabel command [options] [args]
 
 pybabel: error: no valid command or option passed. try the -h/--help option for more information.
 """
 
-    def test_list_locales(self):
-        """
-        Test the command with the --list-locales arg.
-        """
-        result = self.cli.run(sys.argv + ['--list-locales'])
-        assert not result
-        output = sys.stdout.getvalue()
-        assert 'fr_CH' in output
-        assert 'French (Switzerland)' in output
-        assert "\nb'" not in output  # No bytes repr markers in output
-
-    def _run_init_catalog(self):
-        i18n_dir = os.path.join(data_dir, 'project', 'i18n')
-        pot_path = os.path.join(data_dir, 'project', 'i18n', 'messages.pot')
-        init_argv = sys.argv + ['init', '--locale', 'en_US', '-d', i18n_dir,
-                                '-i', pot_path]
-        self.cli.run(init_argv)
-
-    def test_no_duplicated_output_for_multiple_runs(self):
-        self._run_init_catalog()
-        first_output = sys.stderr.getvalue()
-        self._run_init_catalog()
-        second_output = sys.stderr.getvalue()[len(first_output):]
-
-        # in case the log message is not duplicated we should get the same
-        # output as before
-        assert first_output == second_output
-
-    def test_frontend_can_log_to_predefined_handler(self):
-        custom_stream = StringIO()
-        log = logging.getLogger('babel')
-        log.addHandler(logging.StreamHandler(custom_stream))
-
-        self._run_init_catalog()
-        assert id(sys.stderr) != id(custom_stream)
-        assert not sys.stderr.getvalue()
-        assert custom_stream.getvalue()
-
-    def test_help(self):
-        try:
-            self.cli.run(sys.argv + ['--help'])
-            self.fail('Expected SystemExit')
-        except SystemExit as e:
-            assert not e.code
-            content = sys.stdout.getvalue().lower()
-            assert 'options:' in content
-            assert all(command in content for command in ('init', 'update', 'compile', 'extract'))
-
-    def assert_pot_file_exists(self):
-        assert os.path.isfile(pot_file)
-
-    @freeze_time("1994-11-11")
-    def test_extract_with_default_mapping(self):
-        self.cli.run(sys.argv + ['extract',
-                                 '--copyright-holder', 'FooBar, Inc.',
-                                 '--project', 'TestProject', '--version', '0.1',
-                                 '--msgid-bugs-address', 'bugs.address@email.tld',
-                                 '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
-                                 '-o', pot_file, 'project'])
-        self.assert_pot_file_exists()
-        date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
-        expected_content = fr"""# Translations template for TestProject.
+
+def test_list_locales(cli):
+    """
+    Test the command with the --list-locales arg.
+    """
+    result = cli.run(['pybabel', '--list-locales'])
+    assert not result
+    output = sys.stdout.getvalue()
+    assert 'fr_CH' in output
+    assert 'French (Switzerland)' in output
+    assert "\nb'" not in output  # No bytes repr markers in output
+
+
+def _run_init_catalog(cli):
+    i18n_dir = os.path.join(data_dir, 'project', 'i18n')
+    pot_path = os.path.join(data_dir, 'project', 'i18n', 'messages.pot')
+    cli.run(['pybabel', 'init', '--locale', 'en_US', '-d', i18n_dir, '-i', pot_path])
+
+
+def test_no_duplicated_output_for_multiple_runs(cli):
+    _run_init_catalog(cli)
+    first_output = sys.stderr.getvalue()
+    _run_init_catalog(cli)
+    second_output = sys.stderr.getvalue()[len(first_output):]
+
+    # in case the log message is not duplicated we should get the same
+    # output as before
+    assert first_output == second_output
+
+
+def test_frontend_can_log_to_predefined_handler(cli):
+    custom_stream = StringIO()
+    log = logging.getLogger('babel')
+    log.addHandler(logging.StreamHandler(custom_stream))
+
+    _run_init_catalog(cli)
+    assert id(sys.stderr) != id(custom_stream)
+    assert not sys.stderr.getvalue()
+    assert custom_stream.getvalue()
+
+
+def test_help(cli):
+    with pytest.raises(SystemExit) as ei:
+        cli.run(['pybabel', '--help'])
+    assert not ei.value.code
+    content = sys.stdout.getvalue().lower()
+    assert 'options:' in content
+    assert all(command in content for command in ('init', 'update', 'compile', 'extract'))
+
+
+@freeze_time("1994-11-11")
+def test_extract_with_default_mapping(cli):
+    cli.run([
+        'pybabel',
+        'extract',
+        '--copyright-holder', 'FooBar, Inc.',
+        '--project', 'TestProject', '--version', '0.1',
+        '--msgid-bugs-address', 'bugs.address@email.tld',
+        '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
+        '-o', pot_file, 'project',
+    ])
+    date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
+    expected_content = fr"""# Translations template for TestProject.
 # Copyright (C) {time.strftime('%Y')} FooBar, Inc.
 # This file is distributed under the same license as the TestProject
 # project.
@@ -181,22 +166,25 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-        with open(pot_file) as f:
-            actual_content = f.read()
-        assert expected_content == actual_content
-
-    @freeze_time("1994-11-11")
-    def test_extract_with_mapping_file(self):
-        self.cli.run(sys.argv + ['extract',
-                                 '--copyright-holder', 'FooBar, Inc.',
-                                 '--project', 'TestProject', '--version', '0.1',
-                                 '--msgid-bugs-address', 'bugs.address@email.tld',
-                                 '--mapping', os.path.join(data_dir, 'mapping.cfg'),
-                                 '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
-                                 '-o', pot_file, 'project'])
-        self.assert_pot_file_exists()
-        date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
-        expected_content = fr"""# Translations template for TestProject.
+    with open(pot_file) as f:
+        actual_content = f.read()
+    assert expected_content == actual_content
+
+
+@freeze_time("1994-11-11")
+def test_extract_with_mapping_file(cli):
+    cli.run([
+        'pybabel',
+        'extract',
+        '--copyright-holder', 'FooBar, Inc.',
+        '--project', 'TestProject', '--version', '0.1',
+        '--msgid-bugs-address', 'bugs.address@email.tld',
+        '--mapping', os.path.join(data_dir, 'mapping.cfg'),
+        '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
+        '-o', pot_file, 'project',
+    ])
+    date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
+    expected_content = fr"""# Translations template for TestProject.
 # Copyright (C) {time.strftime('%Y')} FooBar, Inc.
 # This file is distributed under the same license as the TestProject
 # project.
@@ -229,26 +217,29 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-        with open(pot_file) as f:
-            actual_content = f.read()
-        assert expected_content == actual_content
-
-    @freeze_time("1994-11-11")
-    def test_extract_with_exact_file(self):
-        """Tests that we can call extract with a particular file and only
-        strings from that file get extracted. (Note the absence of strings from file1.py)
-        """
-        file_to_extract = os.path.join(data_dir, 'project', 'file2.py')
-        self.cli.run(sys.argv + ['extract',
-                                 '--copyright-holder', 'FooBar, Inc.',
-                                 '--project', 'TestProject', '--version', '0.1',
-                                 '--msgid-bugs-address', 'bugs.address@email.tld',
-                                 '--mapping', os.path.join(data_dir, 'mapping.cfg'),
-                                 '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
-                                 '-o', pot_file, file_to_extract])
-        self.assert_pot_file_exists()
-        date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
-        expected_content = fr"""# Translations template for TestProject.
+    with open(pot_file) as f:
+        actual_content = f.read()
+    assert expected_content == actual_content
+
+
+@freeze_time("1994-11-11")
+def test_extract_with_exact_file(cli):
+    """Tests that we can call extract with a particular file and only
+    strings from that file get extracted. (Note the absence of strings from file1.py)
+    """
+    file_to_extract = os.path.join(data_dir, 'project', 'file2.py')
+    cli.run([
+        'pybabel',
+        'extract',
+        '--copyright-holder', 'FooBar, Inc.',
+        '--project', 'TestProject', '--version', '0.1',
+        '--msgid-bugs-address', 'bugs.address@email.tld',
+        '--mapping', os.path.join(data_dir, 'mapping.cfg'),
+        '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
+        '-o', pot_file, file_to_extract,
+    ])
+    date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
+    expected_content = fr"""# Translations template for TestProject.
 # Copyright (C) {time.strftime('%Y')} FooBar, Inc.
 # This file is distributed under the same license as the TestProject
 # project.
@@ -275,20 +266,23 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-        with open(pot_file) as f:
-            actual_content = f.read()
-        assert expected_content == actual_content
-
-    @freeze_time("1994-11-11")
-    def test_init_with_output_dir(self):
-        po_file = get_po_file_path('en_US')
-        self.cli.run(sys.argv + ['init',
-                                 '--locale', 'en_US',
-                                 '-d', os.path.join(i18n_dir),
-                                 '-i', os.path.join(i18n_dir, 'messages.pot')])
-        assert os.path.isfile(po_file)
-        date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
-        expected_content = fr"""# English (United States) translations for TestProject.
+    with open(pot_file) as f:
+        actual_content = f.read()
+    assert expected_content == actual_content
+
+
+@freeze_time("1994-11-11")
+def test_init_with_output_dir(cli):
+    po_file = get_po_file_path('en_US')
+    cli.run([
+        'pybabel',
+        'init',
+        '--locale', 'en_US',
+        '-d', os.path.join(i18n_dir),
+        '-i', os.path.join(i18n_dir, 'messages.pot'),
+    ])
+    date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
+    expected_content = fr"""# English (United States) translations for TestProject.
 # Copyright (C) 2007 FooBar, Inc.
 # This file is distributed under the same license as the TestProject
 # project.
@@ -322,20 +316,23 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-        with open(po_file) as f:
-            actual_content = f.read()
-        assert expected_content == actual_content
-
-    @freeze_time("1994-11-11")
-    def test_init_singular_plural_forms(self):
-        po_file = get_po_file_path('ja_JP')
-        self.cli.run(sys.argv + ['init',
-                                 '--locale', 'ja_JP',
-                                 '-d', os.path.join(i18n_dir),
-                                 '-i', os.path.join(i18n_dir, 'messages.pot')])
-        assert os.path.isfile(po_file)
-        date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
-        expected_content = fr"""# Japanese (Japan) translations for TestProject.
+    with open(po_file) as f:
+        actual_content = f.read()
+    assert expected_content == actual_content
+
+
+@freeze_time("1994-11-11")
+def test_init_singular_plural_forms(cli):
+    po_file = get_po_file_path('ja_JP')
+    cli.run([
+        'pybabel',
+        'init',
+        '--locale', 'ja_JP',
+        '-d', os.path.join(i18n_dir),
+        '-i', os.path.join(i18n_dir, 'messages.pot'),
+    ])
+    date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
+    expected_content = fr"""# Japanese (Japan) translations for TestProject.
 # Copyright (C) 2007 FooBar, Inc.
 # This file is distributed under the same license as the TestProject
 # project.
@@ -368,20 +365,23 @@ msgid_plural "foobars"
 msgstr[0] ""
 
 """
-        with open(po_file) as f:
-            actual_content = f.read()
-        assert expected_content == actual_content
-
-    @freeze_time("1994-11-11")
-    def test_init_more_than_2_plural_forms(self):
-        po_file = get_po_file_path('lv_LV')
-        self.cli.run(sys.argv + ['init',
-                                 '--locale', 'lv_LV',
-                                 '-d', i18n_dir,
-                                 '-i', os.path.join(i18n_dir, 'messages.pot')])
-        assert os.path.isfile(po_file)
-        date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
-        expected_content = fr"""# Latvian (Latvia) translations for TestProject.
+    with open(po_file) as f:
+        actual_content = f.read()
+    assert expected_content == actual_content
+
+
+@freeze_time("1994-11-11")
+def test_init_more_than_2_plural_forms(cli):
+    po_file = get_po_file_path('lv_LV')
+    cli.run([
+        'pybabel',
+        'init',
+        '--locale', 'lv_LV',
+        '-d', i18n_dir,
+        '-i', os.path.join(i18n_dir, 'messages.pot'),
+    ])
+    date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
+    expected_content = fr"""# Latvian (Latvia) translations for TestProject.
 # Copyright (C) 2007 FooBar, Inc.
 # This file is distributed under the same license as the TestProject
 # project.
@@ -417,293 +417,239 @@ msgstr[1] ""
 msgstr[2] ""
 
 """
-        with open(po_file) as f:
-            actual_content = f.read()
-        assert expected_content == actual_content
-
-    def test_compile_catalog(self):
-        po_file = get_po_file_path('de_DE')
-        mo_file = po_file.replace('.po', '.mo')
-        self.cli.run(sys.argv + ['compile',
-                                 '--locale', 'de_DE',
-                                 '-d', i18n_dir])
-        assert not os.path.isfile(mo_file), f'Expected no file at {mo_file!r}'
-        assert sys.stderr.getvalue() == f'catalog {po_file} is marked as fuzzy, skipping\n'
-
-    def test_compile_fuzzy_catalog(self):
-        po_file = get_po_file_path('de_DE')
-        mo_file = po_file.replace('.po', '.mo')
-        try:
-            self.cli.run(sys.argv + ['compile',
-                                     '--locale', 'de_DE', '--use-fuzzy',
-                                     '-d', i18n_dir])
+    with open(po_file) as f:
+        actual_content = f.read()
+    assert expected_content == actual_content
+
+
+def test_compile_catalog(cli):
+    po_file = get_po_file_path('de_DE')
+    mo_file = po_file.replace('.po', '.mo')
+    cli.run(['pybabel', 'compile', '--locale', 'de_DE', '-d', i18n_dir])
+    assert not os.path.isfile(mo_file), f'Expected no file at {mo_file!r}'
+    assert sys.stderr.getvalue() == f'catalog {po_file} is marked as fuzzy, skipping\n'
+
+
+def test_compile_fuzzy_catalog(cli):
+    po_file = get_po_file_path('de_DE')
+    mo_file = po_file.replace('.po', '.mo')
+    try:
+        cli.run(['pybabel', 'compile', '--locale', 'de_DE', '--use-fuzzy', '-d', i18n_dir])
+        assert os.path.isfile(mo_file)
+        assert sys.stderr.getvalue() == f'compiling catalog {po_file} to {mo_file}\n'
+    finally:
+        if os.path.isfile(mo_file):
+            os.unlink(mo_file)
+
+
+def test_compile_catalog_with_more_than_2_plural_forms(cli):
+    po_file = get_po_file_path('ru_RU')
+    mo_file = po_file.replace('.po', '.mo')
+    try:
+        cli.run(['pybabel', 'compile', '--locale', 'ru_RU', '--use-fuzzy', '-d', i18n_dir])
+        assert os.path.isfile(mo_file)
+        assert sys.stderr.getvalue() == f'compiling catalog {po_file} to {mo_file}\n'
+    finally:
+        if os.path.isfile(mo_file):
+            os.unlink(mo_file)
+
+
+def test_compile_catalog_multidomain(cli):
+    po_foo = os.path.join(i18n_dir, 'de_DE', 'LC_MESSAGES', 'foo.po')
+    po_bar = os.path.join(i18n_dir, 'de_DE', 'LC_MESSAGES', 'bar.po')
+    mo_foo = po_foo.replace('.po', '.mo')
+    mo_bar = po_bar.replace('.po', '.mo')
+    try:
+        cli.run([
+            'pybabel', 'compile',
+            '--locale', 'de_DE',
+            '--domain', 'foo bar',
+            '--use-fuzzy',
+            '-d', i18n_dir,
+        ])
+        for mo_file in [mo_foo, mo_bar]:
             assert os.path.isfile(mo_file)
-            assert sys.stderr.getvalue() == f'compiling catalog {po_file} to {mo_file}\n'
-        finally:
-            if os.path.isfile(mo_file):
-                os.unlink(mo_file)
+        assert sys.stderr.getvalue() == (
+            f'compiling catalog {po_foo} to {mo_foo}\n'
+            f'compiling catalog {po_bar} to {mo_bar}\n'
+        )
 
-    def test_compile_catalog_with_more_than_2_plural_forms(self):
-        po_file = get_po_file_path('ru_RU')
-        mo_file = po_file.replace('.po', '.mo')
-        try:
-            self.cli.run(sys.argv + ['compile',
-                                     '--locale', 'ru_RU', '--use-fuzzy',
-                                     '-d', i18n_dir])
-            assert os.path.isfile(mo_file)
-            assert sys.stderr.getvalue() == f'compiling catalog {po_file} to {mo_file}\n'
-        finally:
+    finally:
+        for mo_file in [mo_foo, mo_bar]:
             if os.path.isfile(mo_file):
                 os.unlink(mo_file)
 
-    def test_compile_catalog_multidomain(self):
-        po_foo = os.path.join(i18n_dir, 'de_DE', 'LC_MESSAGES', 'foo.po')
-        po_bar = os.path.join(i18n_dir, 'de_DE', 'LC_MESSAGES', 'bar.po')
-        mo_foo = po_foo.replace('.po', '.mo')
-        mo_bar = po_bar.replace('.po', '.mo')
-        try:
-            self.cli.run(sys.argv + ['compile',
-                                     '--locale', 'de_DE', '--domain', 'foo bar', '--use-fuzzy',
-                                     '-d', i18n_dir])
-            for mo_file in [mo_foo, mo_bar]:
-                assert os.path.isfile(mo_file)
-            assert sys.stderr.getvalue() == (
-                f'compiling catalog {po_foo} to {mo_foo}\n'
-                f'compiling catalog {po_bar} to {mo_bar}\n'
-            )
-
-        finally:
-            for mo_file in [mo_foo, mo_bar]:
-                if os.path.isfile(mo_file):
-                    os.unlink(mo_file)
-
-    def test_update(self):
-        template = Catalog()
-        template.add("1")
-        template.add("2")
-        template.add("3")
-        tmpl_file = os.path.join(i18n_dir, 'temp-template.pot')
-        with open(tmpl_file, "wb") as outfp:
-            write_po(outfp, template)
-        po_file = os.path.join(i18n_dir, 'temp1.po')
-        self.cli.run(sys.argv + ['init',
-                                 '-l', 'fi',
-                                 '-o', po_file,
-                                 '-i', tmpl_file,
-                                 ])
-        with open(po_file) as infp:
-            catalog = read_po(infp)
-            assert len(catalog) == 3
-
-        # Add another entry to the template
-
-        template.add("4")
-
-        with open(tmpl_file, "wb") as outfp:
-            write_po(outfp, template)
-
-        self.cli.run(sys.argv + ['update',
-                                 '-l', 'fi_FI',
-                                 '-o', po_file,
-                                 '-i', tmpl_file])
-
-        with open(po_file) as infp:
-            catalog = read_po(infp)
-            assert len(catalog) == 4  # Catalog was updated
-
-    def test_update_pot_creation_date(self):
-        template = Catalog()
-        template.add("1")
-        template.add("2")
-        template.add("3")
-        tmpl_file = os.path.join(i18n_dir, 'temp-template.pot')
-        with open(tmpl_file, "wb") as outfp:
-            write_po(outfp, template)
-        po_file = os.path.join(i18n_dir, 'temp1.po')
-        self.cli.run(sys.argv + ['init',
-                                 '-l', 'fi',
-                                 '-o', po_file,
-                                 '-i', tmpl_file,
-                                 ])
-        with open(po_file) as infp:
-            catalog = read_po(infp)
-            assert len(catalog) == 3
-        original_catalog_creation_date = catalog.creation_date
-
-        # Update the template creation date
-        template.creation_date -= timedelta(minutes=3)
-        with open(tmpl_file, "wb") as outfp:
-            write_po(outfp, template)
-
-        self.cli.run(sys.argv + ['update',
-                                 '-l', 'fi_FI',
-                                 '-o', po_file,
-                                 '-i', tmpl_file])
-
-        with open(po_file) as infp:
-            catalog = read_po(infp)
-            # We didn't ignore the creation date, so expect a diff
-            assert catalog.creation_date != original_catalog_creation_date
-
-        # Reset the "original"
-        original_catalog_creation_date = catalog.creation_date
-
-        # Update the template creation date again
-        # This time, pass the ignore flag and expect the times are different
-        template.creation_date -= timedelta(minutes=5)
-        with open(tmpl_file, "wb") as outfp:
-            write_po(outfp, template)
-
-        self.cli.run(sys.argv + ['update',
-                                 '-l', 'fi_FI',
-                                 '-o', po_file,
-                                 '-i', tmpl_file,
-                                 '--ignore-pot-creation-date'])
-
-        with open(po_file) as infp:
-            catalog = read_po(infp)
-            # We ignored creation date, so it should not have changed
-            assert catalog.creation_date == original_catalog_creation_date
-
-    def test_check(self):
-        template = Catalog()
-        template.add("1")
-        template.add("2")
-        template.add("3")
-        tmpl_file = os.path.join(i18n_dir, 'temp-template.pot')
-        with open(tmpl_file, "wb") as outfp:
-            write_po(outfp, template)
-        po_file = os.path.join(i18n_dir, 'temp1.po')
-        self.cli.run(sys.argv + ['init',
-                                 '-l', 'fi_FI',
-                                 '-o', po_file,
-                                 '-i', tmpl_file,
-                                 ])
-
-        # Update the catalog file
-        self.cli.run(sys.argv + ['update',
-                                 '-l', 'fi_FI',
-                                 '-o', po_file,
-                                 '-i', tmpl_file])
-
-        # Run a check without introducing any changes to the template
-        self.cli.run(sys.argv + ['update',
-                                 '--check',
-                                 '-l', 'fi_FI',
-                                 '-o', po_file,
-                                 '-i', tmpl_file])
-
-        # Add a new entry and expect the check to fail
-        template.add("4")
-        with open(tmpl_file, "wb") as outfp:
-            write_po(outfp, template)
-
-        with pytest.raises(BaseError):
-            self.cli.run(sys.argv + ['update',
-                                     '--check',
-                                     '-l', 'fi_FI',
-                                     '-o', po_file,
-                                     '-i', tmpl_file])
-
-        # Write the latest changes to the po-file
-        self.cli.run(sys.argv + ['update',
-                                 '-l', 'fi_FI',
-                                 '-o', po_file,
-                                 '-i', tmpl_file])
-
-        # Update an entry and expect the check to fail
-        template.add("4", locations=[("foo.py", 1)])
-        with open(tmpl_file, "wb") as outfp:
-            write_po(outfp, template)
-
-        with pytest.raises(BaseError):
-            self.cli.run(sys.argv + ['update',
-                                     '--check',
-                                     '-l', 'fi_FI',
-                                     '-o', po_file,
-                                     '-i', tmpl_file])
-
-    def test_check_pot_creation_date(self):
-        template = Catalog()
-        template.add("1")
-        template.add("2")
-        template.add("3")
-        tmpl_file = os.path.join(i18n_dir, 'temp-template.pot')
-        with open(tmpl_file, "wb") as outfp:
-            write_po(outfp, template)
-        po_file = os.path.join(i18n_dir, 'temp1.po')
-        self.cli.run(sys.argv + ['init',
-                                 '-l', 'fi_FI',
-                                 '-o', po_file,
-                                 '-i', tmpl_file,
-                                 ])
-
-        # Update the catalog file
-        self.cli.run(sys.argv + ['update',
-                                 '-l', 'fi_FI',
-                                 '-o', po_file,
-                                 '-i', tmpl_file])
-
-        # Run a check without introducing any changes to the template
-        self.cli.run(sys.argv + ['update',
-                                 '--check',
-                                 '-l', 'fi_FI',
-                                 '-o', po_file,
-                                 '-i', tmpl_file])
-
-        # Run a check after changing the template creation date
-        template.creation_date = datetime.now() - timedelta(minutes=5)
-        with open(tmpl_file, "wb") as outfp:
-            write_po(outfp, template)
-
-        # Should fail without --ignore-pot-creation-date flag
-        with pytest.raises(BaseError):
-            self.cli.run(sys.argv + ['update',
-                                     '--check',
-                                     '-l', 'fi_FI',
-                                     '-o', po_file,
-                                     '-i', tmpl_file])
-        # Should pass with --ignore-pot-creation-date flag
-        self.cli.run(sys.argv + ['update',
-                                 '--check',
-                                 '-l', 'fi_FI',
-                                 '-o', po_file,
-                                 '-i', tmpl_file,
-                                 '--ignore-pot-creation-date'])
-
-    def test_update_init_missing(self):
-        template = Catalog()
-        template.add("1")
-        template.add("2")
-        template.add("3")
-        tmpl_file = os.path.join(i18n_dir, 'temp2-template.pot')
-        with open(tmpl_file, "wb") as outfp:
-            write_po(outfp, template)
-        po_file = os.path.join(i18n_dir, 'temp2.po')
-
-        self.cli.run(sys.argv + ['update',
-                                 '--init-missing',
-                                 '-l', 'fi',
-                                 '-o', po_file,
-                                 '-i', tmpl_file])
-
-        with open(po_file) as infp:
-            catalog = read_po(infp)
-            assert len(catalog) == 3
-
-        # Add another entry to the template
-
-        template.add("4")
-
-        with open(tmpl_file, "wb") as outfp:
-            write_po(outfp, template)
-
-        self.cli.run(sys.argv + ['update',
-                                 '--init-missing',
-                                 '-l', 'fi_FI',
-                                 '-o', po_file,
-                                 '-i', tmpl_file])
-
-        with open(po_file) as infp:
-            catalog = read_po(infp)
-            assert len(catalog) == 4  # Catalog was updated
+
+def test_update(cli):
+    template = Catalog()
+    template.add("1")
+    template.add("2")
+    template.add("3")
+    tmpl_file = os.path.join(i18n_dir, 'temp-template.pot')
+    with open(tmpl_file, "wb") as outfp:
+        write_po(outfp, template)
+    po_file = os.path.join(i18n_dir, 'temp1.po')
+    cli.run(['pybabel', 'init', '-l', 'fi', '-o', po_file, '-i', tmpl_file])
+    with open(po_file) as infp:
+        catalog = read_po(infp)
+        assert len(catalog) == 3
+
+    # Add another entry to the template
+
+    template.add("4")
+
+    with open(tmpl_file, "wb") as outfp:
+        write_po(outfp, template)
+
+    cli.run(['pybabel', 'update', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file])
+
+    with open(po_file) as infp:
+        catalog = read_po(infp)
+        assert len(catalog) == 4  # Catalog was updated
+
+
+def test_update_pot_creation_date(cli):
+    template = Catalog()
+    template.add("1")
+    template.add("2")
+    template.add("3")
+    tmpl_file = os.path.join(i18n_dir, 'temp-template.pot')
+    with open(tmpl_file, "wb") as outfp:
+        write_po(outfp, template)
+    po_file = os.path.join(i18n_dir, 'temp1.po')
+    cli.run(['pybabel', 'init', '-l', 'fi', '-o', po_file, '-i', tmpl_file])
+    with open(po_file) as infp:
+        catalog = read_po(infp)
+        assert len(catalog) == 3
+    original_catalog_creation_date = catalog.creation_date
+
+    # Update the template creation date
+    template.creation_date -= timedelta(minutes=3)
+    with open(tmpl_file, "wb") as outfp:
+        write_po(outfp, template)
+
+    cli.run(['pybabel', 'update', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file])
+
+    with open(po_file) as infp:
+        catalog = read_po(infp)
+        # We didn't ignore the creation date, so expect a diff
+        assert catalog.creation_date != original_catalog_creation_date
+
+    # Reset the "original"
+    original_catalog_creation_date = catalog.creation_date
+
+    # Update the template creation date again
+    # This time, pass the ignore flag and expect the times are different
+    template.creation_date -= timedelta(minutes=5)
+    with open(tmpl_file, "wb") as outfp:
+        write_po(outfp, template)
+
+    cli.run(['pybabel', 'update', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file, '--ignore-pot-creation-date'])
+
+    with open(po_file) as infp:
+        catalog = read_po(infp)
+        # We ignored creation date, so it should not have changed
+        assert catalog.creation_date == original_catalog_creation_date
+
+
+def test_check(cli):
+    template = Catalog()
+    template.add("1")
+    template.add("2")
+    template.add("3")
+    tmpl_file = os.path.join(i18n_dir, 'temp-template.pot')
+    with open(tmpl_file, "wb") as outfp:
+        write_po(outfp, template)
+    po_file = os.path.join(i18n_dir, 'temp1.po')
+    cli.run(['pybabel', 'init', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file])
+
+    # Update the catalog file
+    cli.run(['pybabel', 'update', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file])
+
+    # Run a check without introducing any changes to the template
+    cli.run(['pybabel', 'update', '--check', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file])
+
+    # Add a new entry and expect the check to fail
+    template.add("4")
+    with open(tmpl_file, "wb") as outfp:
+        write_po(outfp, template)
+
+    with pytest.raises(BaseError):
+        cli.run(['pybabel', 'update', '--check', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file])
+
+    # Write the latest changes to the po-file
+    cli.run(['pybabel', 'update', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file])
+
+    # Update an entry and expect the check to fail
+    template.add("4", locations=[("foo.py", 1)])
+    with open(tmpl_file, "wb") as outfp:
+        write_po(outfp, template)
+
+    with pytest.raises(BaseError):
+        cli.run(['pybabel', 'update', '--check', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file])
+
+
+def test_check_pot_creation_date(cli):
+    template = Catalog()
+    template.add("1")
+    template.add("2")
+    template.add("3")
+    tmpl_file = os.path.join(i18n_dir, 'temp-template.pot')
+    with open(tmpl_file, "wb") as outfp:
+        write_po(outfp, template)
+    po_file = os.path.join(i18n_dir, 'temp1.po')
+    cli.run(['pybabel', 'init', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file])
+
+    # Update the catalog file
+    cli.run(['pybabel', 'update', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file])
+
+    # Run a check without introducing any changes to the template
+    cli.run(['pybabel', 'update', '--check', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file])
+
+    # Run a check after changing the template creation date
+    template.creation_date = datetime.now() - timedelta(minutes=5)
+    with open(tmpl_file, "wb") as outfp:
+        write_po(outfp, template)
+
+    # Should fail without --ignore-pot-creation-date flag
+    with pytest.raises(BaseError):
+        cli.run(['pybabel', 'update', '--check', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file])
+    # Should pass with --ignore-pot-creation-date flag
+    cli.run([
+        'pybabel', 'update',
+        '--check',
+        '-l', 'fi_FI',
+        '-o', po_file,
+        '-i', tmpl_file,
+        '--ignore-pot-creation-date',
+    ])
+
+
+def test_update_init_missing(cli):
+    template = Catalog()
+    template.add("1")
+    template.add("2")
+    template.add("3")
+    tmpl_file = os.path.join(i18n_dir, 'temp2-template.pot')
+    with open(tmpl_file, "wb") as outfp:
+        write_po(outfp, template)
+    po_file = os.path.join(i18n_dir, 'temp2.po')
+
+    cli.run(['pybabel', 'update', '--init-missing', '-l', 'fi', '-o', po_file, '-i', tmpl_file])
+
+    with open(po_file) as infp:
+        catalog = read_po(infp)
+        assert len(catalog) == 3
+
+    # Add another entry to the template
+
+    template.add("4")
+
+    with open(tmpl_file, "wb") as outfp:
+        write_po(outfp, template)
+
+    cli.run(['pybabel', 'update', '--init-missing', '-l', 'fi_FI', '-o', po_file, '-i', tmpl_file])
+
+    with open(po_file) as infp:
+        catalog = read_po(infp)
+        assert len(catalog) == 4  # Catalog was updated
index 3239356509f64efd6257959aefb070aa240a853d..108fea3a2a4ebe334c2424c2a2f377fe5fc5891b 100644 (file)
@@ -77,7 +77,7 @@ def test_input_paths_is_treated_as_list(extract_cmd):
         catalog = read_po(f)
     msg = catalog.get('bar')
     assert len(msg.locations) == 1
-    assert ('file1.py' in msg.locations[0][0])
+    assert 'file1.py' in msg.locations[0][0]
 
 
 def test_input_paths_handle_spaces_after_comma(extract_cmd):
@@ -113,8 +113,6 @@ def test_extraction_with_default_mapping(extract_cmd):
     extract_cmd.finalize_options()
     extract_cmd.run()
 
-    assert os.path.isfile(pot_file)
-
     date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
     expected_content = fr"""# Translations template for TestProject.
 # Copyright (C) {time.strftime('%Y')} FooBar, Inc.
@@ -171,8 +169,6 @@ def test_extraction_with_mapping_file(extract_cmd):
     extract_cmd.finalize_options()
     extract_cmd.run()
 
-    assert os.path.isfile(pot_file)
-
     date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
     expected_content = fr"""# Translations template for TestProject.
 # Copyright (C) {time.strftime('%Y')} FooBar, Inc.
@@ -228,8 +224,6 @@ def test_extraction_with_mapping_dict(extract_cmd):
     extract_cmd.finalize_options()
     extract_cmd.run()
 
-    assert os.path.isfile(pot_file)
-
     date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
     expected_content = fr"""# Translations template for TestProject.
 # Copyright (C) {time.strftime('%Y')} FooBar, Inc.
@@ -283,8 +277,6 @@ def test_extraction_add_location_file(extract_cmd):
     extract_cmd.finalize_options()
     extract_cmd.run()
 
-    assert os.path.isfile(pot_file)
-
     expected_content = r"""#: project/file1.py
 msgid "bar"
 msgstr ""
index bb196e1e79d49ce4557c24295130ebaff66164c4..a77d5d679ce89dff7fedd587908796b4e1caf134 100644 (file)
@@ -134,8 +134,7 @@ def test_parse_mapping(data: str, parser, preprocess, is_toml):
 
 
 def test_parse_keywords():
-    kw = frontend.parse_keywords(['_', 'dgettext:2',
-                                  'dngettext:2,3', 'pgettext:1c,2'])
+    kw = frontend.parse_keywords(['_', 'dgettext:2', 'dngettext:2,3', 'pgettext:1c,2'])
     assert kw == {
         '_': None,
         'dgettext': (2,),
@@ -239,7 +238,6 @@ def test_update_catalog_boolean_args():
     assert cmdinst.previous is False  # Mutually exclusive with no_fuzzy_matching
 
 
-
 def test_compile_catalog_dir(tmp_path):
     """
     Test that `compile` can compile all locales in a directory.
@@ -280,7 +278,6 @@ def test_compile_catalog_explicit(tmp_path):
     assert mo_file.exists()
 
 
-
 @pytest.mark.parametrize("explicit_locale", (None, 'fi_FI'), ids=("implicit", "explicit"))
 def test_update_dir(tmp_path, explicit_locale: bool):
     """
@@ -290,7 +287,7 @@ def test_update_dir(tmp_path, explicit_locale: bool):
     template.add("1")
     template.add("2")
     template.add("3")
-    tmpl_file = (tmp_path / 'temp-template.pot')
+    tmpl_file = tmp_path / 'temp-template.pot'
     with tmpl_file.open("wb") as outfp:
         write_po(outfp, template)
     locales = ("fi_FI", "sv_SE")
index 0dbe89fba94e26c1ad349449b34ca558bbb12a25..e69e5cce374ad245854dc0da7ea37ac05b933d27 100644 (file)
@@ -24,10 +24,10 @@ from babel.dates import format_datetime
 from babel.messages import frontend
 from babel.util import LOCALTZ
 from tests.messages.consts import (
-TEST_PROJECT_DISTRIBUTION_DATA,
-data_dir,
-get_po_file_path,
-i18n_dir,
+    TEST_PROJECT_DISTRIBUTION_DATA,
+    data_dir,
+    get_po_file_path,
+    i18n_dir,
 )
 from tests.messages.utils import Distribution
 
@@ -68,9 +68,6 @@ def test_with_output_dir(init_cmd):
     init_cmd.finalize_options()
     init_cmd.run()
 
-    po_file = get_po_file_path('en_US')
-    assert os.path.isfile(po_file)
-
     date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
     expected_content = fr"""# English (United States) translations for TestProject.
 # Copyright (C) 2007 FooBar, Inc.
@@ -106,7 +103,7 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-    with open(po_file) as f:
+    with open(get_po_file_path('en_US')) as f:
         actual_content = f.read()
     assert expected_content == actual_content
 
@@ -120,9 +117,6 @@ def test_keeps_catalog_non_fuzzy(init_cmd):
     init_cmd.finalize_options()
     init_cmd.run()
 
-    po_file = get_po_file_path('en_US')
-    assert os.path.isfile(po_file)
-
     date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
     expected_content = fr"""# English (United States) translations for TestProject.
 # Copyright (C) 2007 FooBar, Inc.
@@ -158,7 +152,7 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-    with open(po_file) as f:
+    with open(get_po_file_path('en_US')) as f:
         actual_content = f.read()
     assert expected_content == actual_content
 
@@ -172,9 +166,6 @@ def test_correct_init_more_than_2_plurals(init_cmd):
     init_cmd.finalize_options()
     init_cmd.run()
 
-    po_file = get_po_file_path('lv_LV')
-    assert os.path.isfile(po_file)
-
     date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
     expected_content = fr"""# Latvian (Latvia) translations for TestProject.
 # Copyright (C) 2007 FooBar, Inc.
@@ -212,7 +203,7 @@ msgstr[1] ""
 msgstr[2] ""
 
 """
-    with open(po_file) as f:
+    with open(get_po_file_path('lv_LV')) as f:
         actual_content = f.read()
     assert expected_content == actual_content
 
@@ -226,9 +217,6 @@ def test_correct_init_singular_plural_forms(init_cmd):
     init_cmd.finalize_options()
     init_cmd.run()
 
-    po_file = get_po_file_path('ja_JP')
-    assert os.path.isfile(po_file)
-
     date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='ja_JP')
     expected_content = fr"""# Japanese (Japan) translations for TestProject.
 # Copyright (C) 2007 FooBar, Inc.
@@ -263,7 +251,7 @@ msgid_plural "foobars"
 msgstr[0] ""
 
 """
-    with open(po_file) as f:
+    with open(get_po_file_path('ja_JP')) as f:
         actual_content = f.read()
     assert expected_content == actual_content
 
@@ -286,8 +274,6 @@ def test_supports_no_wrap(init_cmd):
     init_cmd.finalize_options()
     init_cmd.run()
 
-    po_file = get_po_file_path('en_US')
-    assert os.path.isfile(po_file)
     date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US')
     expected_content = fr"""# English (United States) translations for TestProject.
 # Copyright (C) 2007 FooBar, Inc.
@@ -323,7 +309,7 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-    with open(po_file) as f:
+    with open(get_po_file_path('en_US')) as f:
         actual_content = f.read()
     assert expected_content == actual_content
 
@@ -345,8 +331,6 @@ def test_supports_width(init_cmd):
     init_cmd.finalize_options()
     init_cmd.run()
 
-    po_file = get_po_file_path('en_US')
-    assert os.path.isfile(po_file)
     date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US')
     expected_content = fr"""# English (United States) translations for TestProject.
 # Copyright (C) 2007 FooBar, Inc.
@@ -382,6 +366,6 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-    with open(po_file) as f:
+    with open(get_po_file_path('en_US')) as f:
         actual_content = f.read()
     assert expected_content == actual_content