From b3fcb5b724caab5db1aad809ed482931a00e4a05 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Thu, 25 Dec 2025 18:15:35 +0200 Subject: [PATCH] De-global pot_file --- tests/messages/consts.py | 1 - tests/messages/frontend/conftest.py | 9 +++++ tests/messages/frontend/test_cli.py | 20 ++++------ tests/messages/frontend/test_extract.py | 49 ++++++++++--------------- 4 files changed, 35 insertions(+), 44 deletions(-) create mode 100644 tests/messages/frontend/conftest.py diff --git a/tests/messages/consts.py b/tests/messages/consts.py index d3295e78..98c9cc05 100644 --- a/tests/messages/consts.py +++ b/tests/messages/consts.py @@ -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 index 00000000..d309255c --- /dev/null +++ b/tests/messages/frontend/conftest.py @@ -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' diff --git a/tests/messages/frontend/test_cli.py b/tests/messages/frontend/test_cli.py index 8c04af47..9eb2c4cd 100644 --- a/tests/messages/frontend/test_cli.py +++ b/tests/messages/frontend/test_cli.py @@ -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") diff --git a/tests/messages/frontend/test_extract.py b/tests/messages/frontend/test_extract.py index 108fea3a..7980edda 100644 --- a/tests/messages/frontend/test_extract.py +++ b/tests/messages/frontend/test_extract.py @@ -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() -- 2.47.3