From: Aarni Koskela Date: Sat, 31 Jan 2026 12:58:42 +0000 (+0100) Subject: Add frozen_time fixture X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=03419ad5a472742322b2685d3162f0cb3ad29857;p=thirdparty%2Fbabel.git Add frozen_time fixture --- diff --git a/tests/conftest.py b/tests/conftest.py index dab67a9a..8832d864 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,7 @@ +import datetime + import pytest +from freezegun import freeze_time try: import zoneinfo @@ -37,3 +40,9 @@ def timezone_getter(request): 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 diff --git a/tests/messages/frontend/test_cli.py b/tests/messages/frontend/test_cli.py index fb085984..1aeaf986 100644 --- a/tests/messages/frontend/test_cli.py +++ b/tests/messages/frontend/test_cli.py @@ -21,7 +21,6 @@ from datetime import datetime, timedelta from io import StringIO import pytest -from freezegun import freeze_time from babel import __version__ as VERSION from babel.dates import format_datetime @@ -111,8 +110,7 @@ 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(frozen_time, cli, pot_file): cli.run([ 'pybabel', 'extract', @@ -122,7 +120,7 @@ def test_extract_with_default_mapping(cli, pot_file): '-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 @@ -165,8 +163,7 @@ msgstr[1] "" 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', @@ -177,7 +174,7 @@ def test_extract_with_mapping_file(cli, pot_file): '-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 @@ -214,8 +211,7 @@ msgstr[1] "" 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) """ @@ -230,7 +226,7 @@ def test_extract_with_exact_file(cli, pot_file): '-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 @@ -261,8 +257,7 @@ msgstr[1] "" 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', @@ -271,7 +266,7 @@ def test_init_with_output_dir(cli): '-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 @@ -311,8 +306,7 @@ msgstr[1] "" 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', @@ -321,7 +315,7 @@ def test_init_singular_plural_forms(cli): '-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 @@ -360,8 +354,7 @@ msgstr[0] "" 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', @@ -370,7 +363,7 @@ def test_init_more_than_2_plural_forms(cli): '-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 diff --git a/tests/messages/frontend/test_extract.py b/tests/messages/frontend/test_extract.py index 712200fb..dd98a24d 100644 --- a/tests/messages/frontend/test_extract.py +++ b/tests/messages/frontend/test_extract.py @@ -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 @@ -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 @@ -110,7 +107,7 @@ 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') + 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 @@ -153,8 +150,7 @@ msgstr[1] "" 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' @@ -164,7 +160,7 @@ 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') + 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 @@ -201,9 +197,8 @@ msgstr[1] "" 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), @@ -218,7 +213,7 @@ 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') + 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 diff --git a/tests/messages/frontend/test_init.py b/tests/messages/frontend/test_init.py index e69e5cce..2f5c6043 100644 --- a/tests/messages/frontend/test_init.py +++ b/tests/messages/frontend/test_init.py @@ -14,10 +14,8 @@ from __future__ import annotations 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 @@ -59,8 +57,7 @@ def test_no_locale(init_cmd): 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' @@ -68,7 +65,7 @@ def test_with_output_dir(init_cmd): 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 @@ -108,8 +105,7 @@ msgstr[1] "" 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' @@ -117,7 +113,7 @@ def test_keeps_catalog_non_fuzzy(init_cmd): 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 @@ -157,8 +153,7 @@ msgstr[1] "" 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' @@ -166,7 +161,7 @@ def test_correct_init_more_than_2_plurals(init_cmd): 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 @@ -208,8 +203,7 @@ msgstr[2] "" 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' @@ -217,7 +211,7 @@ def test_correct_init_singular_plural_forms(init_cmd): 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 @@ -256,8 +250,7 @@ msgstr[0] "" 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' @@ -274,7 +267,7 @@ def test_supports_no_wrap(init_cmd): 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 @@ -314,8 +307,7 @@ msgstr[1] "" 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' @@ -331,7 +323,7 @@ def test_supports_width(init_cmd): 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 diff --git a/tests/test_dates.py b/tests/test_dates.py index 12bb2343..4e04ff14 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -753,10 +753,8 @@ def test_zh_TW_format(): 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