+import datetime
+
import pytest
+from freezegun import freeze_time
try:
import zoneinfo
pytest.skip("zoneinfo not available")
else:
raise NotImplementedError
+
+
+@pytest.fixture()
+def frozen_time() -> datetime.datetime:
+ with freeze_time(datetime.frozen_time) as frozen:
+ yield frozen.time_to_freeze
from io import StringIO
import pytest
-from freezegun import freeze_time
from babel import __version__ as VERSION
from babel.dates import format_datetime
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(frozen_time, cli, pot_file):
cli.run([
'pybabel',
'extract',
'-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')
+ date = format_datetime(frozen_time, '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
assert expected_content == pot_file.read_text()
-@freeze_time("1994-11-11")
-def test_extract_with_mapping_file(cli, pot_file):
+def test_extract_with_mapping_file(frozen_time, cli, pot_file):
cli.run([
'pybabel',
'extract',
'-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')
+ date = format_datetime(frozen_time, '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
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(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)
"""
'-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')
+ date = format_datetime(frozen_time, '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
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(frozen_time, cli):
po_file = get_po_file_path('en_US')
cli.run([
'pybabel',
'-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')
+ date = format_datetime(frozen_time, '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
assert expected_content == actual_content
-@freeze_time("1994-11-11")
-def test_init_singular_plural_forms(cli):
+def test_init_singular_plural_forms(frozen_time, cli):
po_file = get_po_file_path('ja_JP')
cli.run([
'pybabel',
'-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')
+ date = format_datetime(frozen_time, '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
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(frozen_time, cli):
po_file = get_po_file_path('lv_LV')
cli.run([
'pybabel',
'-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')
+ date = format_datetime(frozen_time, '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
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
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
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')
+ date = format_datetime(frozen_time, '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
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'
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')
+ date = format_datetime(frozen_time, '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
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),
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')
+ date = format_datetime(frozen_time, '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
import os
import shutil
-from datetime import datetime
import pytest
-from freezegun import freeze_time
from babel import __version__ as VERSION
from babel.dates import format_datetime
init_cmd.finalize_options()
-@freeze_time("1994-11-11")
-def test_with_output_dir(init_cmd):
+def test_with_output_dir(frozen_time, init_cmd):
init_cmd.input_file = 'project/i18n/messages.pot'
init_cmd.locale = 'en_US'
init_cmd.output_dir = 'project/i18n'
init_cmd.finalize_options()
init_cmd.run()
- date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
+ date = format_datetime(frozen_time, '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
assert expected_content == actual_content
-@freeze_time("1994-11-11")
-def test_keeps_catalog_non_fuzzy(init_cmd):
+def test_keeps_catalog_non_fuzzy(frozen_time, init_cmd):
init_cmd.input_file = 'project/i18n/messages_non_fuzzy.pot'
init_cmd.locale = 'en_US'
init_cmd.output_dir = 'project/i18n'
init_cmd.finalize_options()
init_cmd.run()
- date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
+ date = format_datetime(frozen_time, '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
assert expected_content == actual_content
-@freeze_time("1994-11-11")
-def test_correct_init_more_than_2_plurals(init_cmd):
+def test_correct_init_more_than_2_plurals(frozen_time, init_cmd):
init_cmd.input_file = 'project/i18n/messages.pot'
init_cmd.locale = 'lv_LV'
init_cmd.output_dir = 'project/i18n'
init_cmd.finalize_options()
init_cmd.run()
- date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
+ date = format_datetime(frozen_time, '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
assert expected_content == actual_content
-@freeze_time("1994-11-11")
-def test_correct_init_singular_plural_forms(init_cmd):
+def test_correct_init_singular_plural_forms(frozen_time, init_cmd):
init_cmd.input_file = 'project/i18n/messages.pot'
init_cmd.locale = 'ja_JP'
init_cmd.output_dir = 'project/i18n'
init_cmd.finalize_options()
init_cmd.run()
- date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='ja_JP')
+ date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='ja_JP')
expected_content = fr"""# Japanese (Japan) translations for TestProject.
# Copyright (C) 2007 FooBar, Inc.
# This file is distributed under the same license as the TestProject
assert expected_content == actual_content
-@freeze_time("1994-11-11")
-def test_supports_no_wrap(init_cmd):
+def test_supports_no_wrap(frozen_time, init_cmd):
init_cmd.input_file = 'project/i18n/long_messages.pot'
init_cmd.locale = 'en_US'
init_cmd.output_dir = 'project/i18n'
init_cmd.finalize_options()
init_cmd.run()
- date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US')
+ date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US')
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
assert expected_content == actual_content
-@freeze_time("1994-11-11")
-def test_supports_width(init_cmd):
+def test_supports_width(frozen_time, init_cmd):
init_cmd.input_file = 'project/i18n/long_messages.pot'
init_cmd.locale = 'en_US'
init_cmd.output_dir = 'project/i18n'
init_cmd.finalize_options()
init_cmd.run()
- date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US')
+ date = format_datetime(frozen_time, 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US')
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
assert dates.format_time(datetime(2016, 4, 8, 12, 34, 56), locale='zh_TW') == '中午12:34:56'
-def test_format_current_moment():
- frozen_instant = datetime.now(UTC)
- with freezegun.freeze_time(time_to_freeze=frozen_instant):
- assert dates.format_datetime(locale="en_US") == dates.format_datetime(frozen_instant, locale="en_US")
+def test_format_current_moment(frozen_time):
+ assert dates.format_datetime(locale="en_US") == dates.format_datetime(frozen_time, locale="en_US")
@pytest.mark.all_locales