Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ select = [
"SIM300", # Yoda conditions
"UP", # upgrades
"RUF022", # unsorted __all__
"PT006",
"PT007",
]
ignore = [
"C901", # Complexity
Expand Down
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import datetime

import pytest
from freezegun import freeze_time

try:
import zoneinfo
Expand All @@ -19,6 +22,7 @@ def pytest_generate_tests(metafunc):
for mark in metafunc.function.pytestmark:
if mark.name == "all_locales":
from babel.localedata import locale_identifiers

metafunc.parametrize("locale", list(locale_identifiers()))
break

Expand All @@ -37,3 +41,13 @@ def timezone_getter(request):
pytest.skip("zoneinfo not available")
else:
raise NotImplementedError


#: Frozen datetime used in various tests.
FROZEN_DATETIME = datetime.datetime(1994, 11, 11, 00, 00)


@pytest.fixture()
def frozen_time() -> datetime.datetime:
with freeze_time(FROZEN_DATETIME) as frozen:
yield frozen.time_to_freeze
100 changes: 47 additions & 53 deletions tests/messages/frontend/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from io import StringIO

import pytest
from freezegun import freeze_time

from babel import __version__ as VERSION
from babel.dates import format_datetime
Expand Down Expand Up @@ -59,11 +58,7 @@ 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.
"""
assert "error: no valid command or option passed" in sys.stderr.getvalue().lower()


def test_list_locales(cli):
Expand All @@ -88,7 +83,7 @@ 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):]
second_output = sys.stderr.getvalue()[len(first_output) :]

# in case the log message is not duplicated we should get the same
# output as before
Expand All @@ -115,19 +110,19 @@ def test_help(cli):
assert all(command in content for command in ('init', 'update', 'compile', 'extract'))


@freeze_time("1994-11-11")
def test_extract_with_default_mapping(cli, pot_file):
def test_extract_with_default_mapping(formatted_frozen_time, cli, pot_file):
cli.run([
'pybabel',
'extract',
'--copyright-holder', 'FooBar, Inc.',
'--project', 'TestProject', '--version', '0.1',
'--project', 'TestProject',
'--version', '0.1',
'--msgid-bugs-address', 'bugs.address@email.tld',
'-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
'-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.
]) # fmt: skip
expected_content = rf"""# Translations template for TestProject.
# Copyright (C) {time.strftime('%Y')} FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
Expand All @@ -138,7 +133,7 @@ def test_extract_with_default_mapping(cli, pot_file):
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: {date}\n"
"POT-Creation-Date: {formatted_frozen_time}\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"
Expand Down Expand Up @@ -169,20 +164,26 @@ def test_extract_with_default_mapping(cli, pot_file):
assert expected_content == pot_file.read_text()


@freeze_time("1994-11-11")
def test_extract_with_mapping_file(cli, pot_file):
@pytest.fixture()
def formatted_frozen_time(frozen_time) -> str:
"""Freeze time and return it formatted for PO use"""
return format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')


def test_extract_with_mapping_file(formatted_frozen_time, cli, pot_file):
cli.run([
'pybabel',
'extract',
'--copyright-holder', 'FooBar, Inc.',
'--project', 'TestProject', '--version', '0.1',
'--project', 'TestProject',
'--version', '0.1',
'--msgid-bugs-address', 'bugs.address@email.tld',
'--mapping', os.path.join(data_dir, 'mapping.cfg'),
'-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
'-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.
]) # fmt: skip
expected_content = rf"""# Translations template for TestProject.
# Copyright (C) {time.strftime('%Y')} FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
Expand All @@ -193,7 +194,7 @@ def test_extract_with_mapping_file(cli, pot_file):
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: {date}\n"
"POT-Creation-Date: {formatted_frozen_time}\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"
Expand All @@ -218,8 +219,7 @@ def test_extract_with_mapping_file(cli, pot_file):
assert expected_content == pot_file.read_text()


@freeze_time("1994-11-11")
def test_extract_with_exact_file(cli, pot_file):
def test_extract_with_exact_file(formatted_frozen_time, cli, pot_file):
"""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)
"""
Expand All @@ -228,14 +228,15 @@ def test_extract_with_exact_file(cli, pot_file):
'pybabel',
'extract',
'--copyright-holder', 'FooBar, Inc.',
'--project', 'TestProject', '--version', '0.1',
'--project', 'TestProject',
'--version', '0.1',
'--msgid-bugs-address', 'bugs.address@email.tld',
'--mapping', os.path.join(data_dir, 'mapping.cfg'),
'-c', 'TRANSLATOR', '-c', 'TRANSLATORS:',
'-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.
]) # fmt: skip
expected_content = rf"""# Translations template for TestProject.
# Copyright (C) {time.strftime('%Y')} FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
Expand All @@ -246,7 +247,7 @@ def test_extract_with_exact_file(cli, pot_file):
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: {date}\n"
"POT-Creation-Date: {formatted_frozen_time}\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"
Expand All @@ -265,18 +266,16 @@ def test_extract_with_exact_file(cli, pot_file):
assert expected_content == pot_file.read_text()


@freeze_time("1994-11-11")
def test_init_with_output_dir(cli):
def test_init_with_output_dir(formatted_frozen_time, 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.
]) # fmt: skip
expected_content = rf"""# English (United States) translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
Expand All @@ -287,7 +286,7 @@ def test_init_with_output_dir(cli):
"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}\n"
"PO-Revision-Date: {formatted_frozen_time}\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: en_US\n"
"Language-Team: en_US <LL@li.org>\n"
Expand Down Expand Up @@ -315,18 +314,16 @@ def test_init_with_output_dir(cli):
assert expected_content == actual_content


@freeze_time("1994-11-11")
def test_init_singular_plural_forms(cli):
def test_init_singular_plural_forms(formatted_frozen_time, 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.
]) # fmt: skip
expected_content = rf"""# Japanese (Japan) translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
Expand All @@ -337,7 +334,7 @@ def test_init_singular_plural_forms(cli):
"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}\n"
"PO-Revision-Date: {formatted_frozen_time}\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: ja_JP\n"
"Language-Team: ja_JP <LL@li.org>\n"
Expand All @@ -364,18 +361,16 @@ def test_init_singular_plural_forms(cli):
assert expected_content == actual_content


@freeze_time("1994-11-11")
def test_init_more_than_2_plural_forms(cli):
def test_init_more_than_2_plural_forms(formatted_frozen_time, 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.
]) # fmt: skip
expected_content = rf"""# Latvian (Latvia) translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
Expand All @@ -386,7 +381,7 @@ def test_init_more_than_2_plural_forms(cli):
"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}\n"
"PO-Revision-Date: {formatted_frozen_time}\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: lv_LV\n"
"Language-Team: lv_LV <LL@li.org>\n"
Expand Down Expand Up @@ -460,12 +455,11 @@ def test_compile_catalog_multidomain(cli):
'--domain', 'foo bar',
'--use-fuzzy',
'-d', i18n_dir,
])
]) # fmt: skip
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'
f'compiling catalog {po_foo} to {mo_foo}\ncompiling catalog {po_bar} to {mo_bar}\n'
)

finally:
Expand Down Expand Up @@ -616,7 +610,7 @@ def test_check_pot_creation_date(cli):
'-o', po_file,
'-i', tmpl_file,
'--ignore-pot-creation-date',
])
]) # fmt: skip


def test_update_init_missing(cli):
Expand Down
23 changes: 9 additions & 14 deletions tests/messages/frontend/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
from __future__ import annotations

import time
from datetime import datetime

import pytest
from freezegun import freeze_time

from babel import __version__ as VERSION
from babel.dates import format_datetime
Expand Down Expand Up @@ -100,8 +98,7 @@ def test_input_dirs_is_mutually_exclusive_with_input_paths(extract_cmd, pot_file
extract_cmd.finalize_options()


@freeze_time("1994-11-11")
def test_extraction_with_default_mapping(extract_cmd, pot_file):
def test_extraction_with_default_mapping(frozen_time, extract_cmd, pot_file):
extract_cmd.copyright_holder = 'FooBar, Inc.'
extract_cmd.msgid_bugs_address = 'bugs.address@email.tld'
extract_cmd.output_file = pot_file
Expand All @@ -110,8 +107,8 @@ def test_extraction_with_default_mapping(extract_cmd, pot_file):
extract_cmd.finalize_options()
extract_cmd.run()

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.
date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
expected_content = rf"""# Translations template for TestProject.
# Copyright (C) {time.strftime('%Y')} FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
Expand Down Expand Up @@ -153,8 +150,7 @@ def test_extraction_with_default_mapping(extract_cmd, pot_file):
assert expected_content == pot_file.read_text()


@freeze_time("1994-11-11")
def test_extraction_with_mapping_file(extract_cmd, pot_file):
def test_extraction_with_mapping_file(frozen_time, extract_cmd, pot_file):
extract_cmd.copyright_holder = 'FooBar, Inc.'
extract_cmd.msgid_bugs_address = 'bugs.address@email.tld'
extract_cmd.mapping_file = 'mapping.cfg'
Expand All @@ -164,8 +160,8 @@ def test_extraction_with_mapping_file(extract_cmd, pot_file):
extract_cmd.finalize_options()
extract_cmd.run()

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.
date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
expected_content = rf"""# Translations template for TestProject.
# Copyright (C) {time.strftime('%Y')} FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
Expand Down Expand Up @@ -201,9 +197,8 @@ def test_extraction_with_mapping_file(extract_cmd, pot_file):
assert expected_content == pot_file.read_text()


@freeze_time("1994-11-11")
@pytest.mark.parametrize("ignore_pattern", ['**/ignored/**.*', 'ignored'])
def test_extraction_with_mapping_dict(extract_cmd, pot_file, ignore_pattern):
def test_extraction_with_mapping_dict(frozen_time, extract_cmd, pot_file, ignore_pattern):
extract_cmd.distribution.message_extractors = {
'project': [
(ignore_pattern, 'ignore', None),
Expand All @@ -218,8 +213,8 @@ def test_extraction_with_mapping_dict(extract_cmd, pot_file, ignore_pattern):
extract_cmd.finalize_options()
extract_cmd.run()

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.
date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
expected_content = rf"""# Translations template for TestProject.
# Copyright (C) {time.strftime('%Y')} FooBar, Inc.
# This file is distributed under the same license as the TestProject
# project.
Expand Down
Loading
Loading