]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Add frozen_time fixture rtests-pre
authorAarni Koskela <akx@iki.fi>
Sat, 31 Jan 2026 12:58:42 +0000 (13:58 +0100)
committerAarni Koskela <akx@iki.fi>
Sun, 1 Feb 2026 17:45:50 +0000 (18:45 +0100)
tests/conftest.py
tests/messages/frontend/test_cli.py
tests/messages/frontend/test_extract.py
tests/messages/frontend/test_init.py
tests/test_dates.py

index dab67a9a3886abbd009b16531723e8be05714b24..8832d864cd8476a7c4de790d8f96937c15a17638 100644 (file)
@@ -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
index fb085984e8db9fdf341b2da5ab710119df3ff417..1aeaf986df8841288000b9bd979f86a11ad9c080 100644 (file)
@@ -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
index 712200fbb8aa4cf21115b564bf273c052776f12f..dd98a24df729e74f49efffee9d6120757c28b6b8 100644 (file)
 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
index e69e5cce374ad245854dc0da7ea37ac05b933d27..2f5c6043cba36b986a323eff2b952459a7e0be89 100644 (file)
@@ -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
index 12bb2343335f1903d6ef8e6068fa88aab568c3fd..4e04ff146a09e921a32e2becfe3be9a461408dd0 100644 (file)
@@ -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