]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
De-global pot_file unwrap-testcases 1240/head
authorAarni Koskela <akx@iki.fi>
Thu, 25 Dec 2025 16:15:35 +0000 (18:15 +0200)
committerAarni Koskela <akx@iki.fi>
Thu, 25 Dec 2025 16:15:35 +0000 (18:15 +0200)
tests/messages/consts.py
tests/messages/frontend/conftest.py [new file with mode: 0644]
tests/messages/frontend/test_cli.py
tests/messages/frontend/test_extract.py

index d3295e78860ce37dd4f761725ca1099df1a6a970..98c9cc05a5fcf22c41172c39a3b1c495a9f883f3 100644 (file)
@@ -9,7 +9,6 @@ this_dir = os.path.abspath(os.path.dirname(__file__))
 data_dir = os.path.join(this_dir, 'data')
 project_dir = os.path.join(data_dir, 'project')
 i18n_dir = os.path.join(project_dir, 'i18n')
-pot_file = os.path.join(i18n_dir, 'temp.pot')  # TODO: this should not be global
 
 
 def get_po_file_path(locale):
diff --git a/tests/messages/frontend/conftest.py b/tests/messages/frontend/conftest.py
new file mode 100644 (file)
index 0000000..d309255
--- /dev/null
@@ -0,0 +1,9 @@
+import pathlib
+import time
+
+import pytest
+
+
+@pytest.fixture
+def pot_file(tmp_path) -> pathlib.Path:
+    return tmp_path / f'po-{time.time()}.pot'
index 8c04af47d607bd12a92de76d653b506a50ba361d..9eb2c4cd0af3f6fc032ed61b33f8508284e705ce 100644 (file)
@@ -29,7 +29,7 @@ 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
+from tests.messages.consts import data_dir, get_po_file_path, i18n_dir
 
 
 @pytest.fixture
@@ -116,7 +116,7 @@ def test_help(cli):
 
 
 @freeze_time("1994-11-11")
-def test_extract_with_default_mapping(cli):
+def test_extract_with_default_mapping(cli, pot_file):
     cli.run([
         'pybabel',
         'extract',
@@ -166,13 +166,11 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-    with open(pot_file) as f:
-        actual_content = f.read()
-    assert expected_content == actual_content
+    assert expected_content == pot_file.read_text()
 
 
 @freeze_time("1994-11-11")
-def test_extract_with_mapping_file(cli):
+def test_extract_with_mapping_file(cli, pot_file):
     cli.run([
         'pybabel',
         'extract',
@@ -217,13 +215,11 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-    with open(pot_file) as f:
-        actual_content = f.read()
-    assert expected_content == actual_content
+    assert expected_content == pot_file.read_text()
 
 
 @freeze_time("1994-11-11")
-def test_extract_with_exact_file(cli):
+def test_extract_with_exact_file(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)
     """
@@ -266,9 +262,7 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-    with open(pot_file) as f:
-        actual_content = f.read()
-    assert expected_content == actual_content
+    assert expected_content == pot_file.read_text()
 
 
 @freeze_time("1994-11-11")
index 108fea3a2a4ebe334c2424c2a2f377fe5fc5891b..7980eddad54767bfa6bf00bbc0200d92277b4d00 100644 (file)
@@ -12,7 +12,6 @@
 
 from __future__ import annotations
 
-import os
 import time
 from datetime import datetime
 
@@ -25,7 +24,7 @@ from babel.messages import frontend
 from babel.messages.frontend import OptionError
 from babel.messages.pofile import read_po
 from babel.util import LOCALTZ
-from tests.messages.consts import TEST_PROJECT_DISTRIBUTION_DATA, data_dir, pot_file, this_dir
+from tests.messages.consts import TEST_PROJECT_DISTRIBUTION_DATA, data_dir, this_dir
 from tests.messages.utils import Distribution
 
 
@@ -35,9 +34,7 @@ def extract_cmd(monkeypatch):
     dist = Distribution(TEST_PROJECT_DISTRIBUTION_DATA)
     extract_cmd = frontend.ExtractMessages(dist)
     extract_cmd.initialize_options()
-    yield extract_cmd
-    if os.path.isfile(pot_file):
-        os.unlink(pot_file)
+    return extract_cmd
 
 
 def test_neither_default_nor_custom_keywords(extract_cmd):
@@ -67,27 +64,27 @@ def test_invalid_file_or_dir_input_path(extract_cmd):
         extract_cmd.finalize_options()
 
 
-def test_input_paths_is_treated_as_list(extract_cmd):
+def test_input_paths_is_treated_as_list(extract_cmd, pot_file):
     extract_cmd.input_paths = data_dir
     extract_cmd.output_file = pot_file
     extract_cmd.finalize_options()
     extract_cmd.run()
 
-    with open(pot_file) as f:
+    with pot_file.open() as f:
         catalog = read_po(f)
     msg = catalog.get('bar')
     assert len(msg.locations) == 1
     assert 'file1.py' in msg.locations[0][0]
 
 
-def test_input_paths_handle_spaces_after_comma(extract_cmd):
+def test_input_paths_handle_spaces_after_comma(extract_cmd, pot_file):
     extract_cmd.input_paths = f"{this_dir},  {data_dir}"
     extract_cmd.output_file = pot_file
     extract_cmd.finalize_options()
     assert extract_cmd.input_paths == [this_dir, data_dir]
 
 
-def test_input_dirs_is_alias_for_input_paths(extract_cmd):
+def test_input_dirs_is_alias_for_input_paths(extract_cmd, pot_file):
     extract_cmd.input_dirs = this_dir
     extract_cmd.output_file = pot_file
     extract_cmd.finalize_options()
@@ -95,7 +92,7 @@ def test_input_dirs_is_alias_for_input_paths(extract_cmd):
     assert extract_cmd.input_paths == [extract_cmd.input_dirs]
 
 
-def test_input_dirs_is_mutually_exclusive_with_input_paths(extract_cmd):
+def test_input_dirs_is_mutually_exclusive_with_input_paths(extract_cmd, pot_file):
     extract_cmd.input_dirs = this_dir
     extract_cmd.input_paths = this_dir
     extract_cmd.output_file = pot_file
@@ -104,10 +101,10 @@ def test_input_dirs_is_mutually_exclusive_with_input_paths(extract_cmd):
 
 
 @freeze_time("1994-11-11")
-def test_extraction_with_default_mapping(extract_cmd):
+def test_extraction_with_default_mapping(extract_cmd, pot_file):
     extract_cmd.copyright_holder = 'FooBar, Inc.'
     extract_cmd.msgid_bugs_address = 'bugs.address@email.tld'
-    extract_cmd.output_file = 'project/i18n/temp.pot'
+    extract_cmd.output_file = pot_file
     extract_cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
 
     extract_cmd.finalize_options()
@@ -153,17 +150,15 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-    with open(pot_file) as f:
-        actual_content = f.read()
-    assert expected_content == actual_content
+    assert expected_content == pot_file.read_text()
 
 
 @freeze_time("1994-11-11")
-def test_extraction_with_mapping_file(extract_cmd):
+def test_extraction_with_mapping_file(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.output_file = 'project/i18n/temp.pot'
+    extract_cmd.output_file = pot_file
     extract_cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
 
     extract_cmd.finalize_options()
@@ -203,13 +198,11 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-    with open(pot_file) as f:
-        actual_content = f.read()
-    assert expected_content == actual_content
+    assert expected_content == pot_file.read_text()
 
 
 @freeze_time("1994-11-11")
-def test_extraction_with_mapping_dict(extract_cmd):
+def test_extraction_with_mapping_dict(extract_cmd, pot_file):
     extract_cmd.distribution.message_extractors = {
         'project': [
             ('**/ignored/**.*', 'ignore', None),
@@ -218,7 +211,7 @@ def test_extraction_with_mapping_dict(extract_cmd):
     }
     extract_cmd.copyright_holder = 'FooBar, Inc.'
     extract_cmd.msgid_bugs_address = 'bugs.address@email.tld'
-    extract_cmd.output_file = 'project/i18n/temp.pot'
+    extract_cmd.output_file = pot_file
     extract_cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
 
     extract_cmd.finalize_options()
@@ -258,19 +251,17 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-    with open(pot_file) as f:
-        actual_content = f.read()
-    assert expected_content == actual_content
+    assert expected_content == pot_file.read_text()
 
 
-def test_extraction_add_location_file(extract_cmd):
+def test_extraction_add_location_file(extract_cmd, pot_file):
     extract_cmd.distribution.message_extractors = {
         'project': [
             ('**/ignored/**.*', 'ignore', None),
             ('**.py', 'python', None),
         ],
     }
-    extract_cmd.output_file = 'project/i18n/temp.pot'
+    extract_cmd.output_file = pot_file
     extract_cmd.add_location = 'file'
     extract_cmd.omit_header = True
 
@@ -288,6 +279,4 @@ msgstr[0] ""
 msgstr[1] ""
 
 """
-    with open(pot_file) as f:
-        actual_content = f.read()
-    assert expected_content == actual_content
+    assert expected_content == pot_file.read_text()