Skip to content

Commit b3fcb5b

Browse files
committed
De-global pot_file
1 parent 90f1b9b commit b3fcb5b

4 files changed

Lines changed: 35 additions & 44 deletions

File tree

‎tests/messages/consts.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
data_dir = os.path.join(this_dir, 'data')
1010
project_dir = os.path.join(data_dir, 'project')
1111
i18n_dir = os.path.join(project_dir, 'i18n')
12-
pot_file = os.path.join(i18n_dir, 'temp.pot') # TODO: this should not be global
1312

1413

1514
def get_po_file_path(locale):
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pathlib
2+
import time
3+
4+
import pytest
5+
6+
7+
@pytest.fixture
8+
def pot_file(tmp_path) -> pathlib.Path:
9+
return tmp_path / f'po-{time.time()}.pot'

‎tests/messages/frontend/test_cli.py‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from babel.messages.frontend import BaseError
3030
from babel.messages.pofile import read_po, write_po
3131
from babel.util import LOCALTZ
32-
from tests.messages.consts import data_dir, get_po_file_path, i18n_dir, pot_file
32+
from tests.messages.consts import data_dir, get_po_file_path, i18n_dir
3333

3434

3535
@pytest.fixture
@@ -116,7 +116,7 @@ def test_help(cli):
116116

117117

118118
@freeze_time("1994-11-11")
119-
def test_extract_with_default_mapping(cli):
119+
def test_extract_with_default_mapping(cli, pot_file):
120120
cli.run([
121121
'pybabel',
122122
'extract',
@@ -166,13 +166,11 @@ def test_extract_with_default_mapping(cli):
166166
msgstr[1] ""
167167
168168
"""
169-
with open(pot_file) as f:
170-
actual_content = f.read()
171-
assert expected_content == actual_content
169+
assert expected_content == pot_file.read_text()
172170

173171

174172
@freeze_time("1994-11-11")
175-
def test_extract_with_mapping_file(cli):
173+
def test_extract_with_mapping_file(cli, pot_file):
176174
cli.run([
177175
'pybabel',
178176
'extract',
@@ -217,13 +215,11 @@ def test_extract_with_mapping_file(cli):
217215
msgstr[1] ""
218216
219217
"""
220-
with open(pot_file) as f:
221-
actual_content = f.read()
222-
assert expected_content == actual_content
218+
assert expected_content == pot_file.read_text()
223219

224220

225221
@freeze_time("1994-11-11")
226-
def test_extract_with_exact_file(cli):
222+
def test_extract_with_exact_file(cli, pot_file):
227223
"""Tests that we can call extract with a particular file and only
228224
strings from that file get extracted. (Note the absence of strings from file1.py)
229225
"""
@@ -266,9 +262,7 @@ def test_extract_with_exact_file(cli):
266262
msgstr[1] ""
267263
268264
"""
269-
with open(pot_file) as f:
270-
actual_content = f.read()
271-
assert expected_content == actual_content
265+
assert expected_content == pot_file.read_text()
272266

273267

274268
@freeze_time("1994-11-11")

‎tests/messages/frontend/test_extract.py‎

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from __future__ import annotations
1414

15-
import os
1615
import time
1716
from datetime import datetime
1817

@@ -25,7 +24,7 @@
2524
from babel.messages.frontend import OptionError
2625
from babel.messages.pofile import read_po
2726
from babel.util import LOCALTZ
28-
from tests.messages.consts import TEST_PROJECT_DISTRIBUTION_DATA, data_dir, pot_file, this_dir
27+
from tests.messages.consts import TEST_PROJECT_DISTRIBUTION_DATA, data_dir, this_dir
2928
from tests.messages.utils import Distribution
3029

3130

@@ -35,9 +34,7 @@ def extract_cmd(monkeypatch):
3534
dist = Distribution(TEST_PROJECT_DISTRIBUTION_DATA)
3635
extract_cmd = frontend.ExtractMessages(dist)
3736
extract_cmd.initialize_options()
38-
yield extract_cmd
39-
if os.path.isfile(pot_file):
40-
os.unlink(pot_file)
37+
return extract_cmd
4138

4239

4340
def test_neither_default_nor_custom_keywords(extract_cmd):
@@ -67,35 +64,35 @@ def test_invalid_file_or_dir_input_path(extract_cmd):
6764
extract_cmd.finalize_options()
6865

6966

70-
def test_input_paths_is_treated_as_list(extract_cmd):
67+
def test_input_paths_is_treated_as_list(extract_cmd, pot_file):
7168
extract_cmd.input_paths = data_dir
7269
extract_cmd.output_file = pot_file
7370
extract_cmd.finalize_options()
7471
extract_cmd.run()
7572

76-
with open(pot_file) as f:
73+
with pot_file.open() as f:
7774
catalog = read_po(f)
7875
msg = catalog.get('bar')
7976
assert len(msg.locations) == 1
8077
assert 'file1.py' in msg.locations[0][0]
8178

8279

83-
def test_input_paths_handle_spaces_after_comma(extract_cmd):
80+
def test_input_paths_handle_spaces_after_comma(extract_cmd, pot_file):
8481
extract_cmd.input_paths = f"{this_dir}, {data_dir}"
8582
extract_cmd.output_file = pot_file
8683
extract_cmd.finalize_options()
8784
assert extract_cmd.input_paths == [this_dir, data_dir]
8885

8986

90-
def test_input_dirs_is_alias_for_input_paths(extract_cmd):
87+
def test_input_dirs_is_alias_for_input_paths(extract_cmd, pot_file):
9188
extract_cmd.input_dirs = this_dir
9289
extract_cmd.output_file = pot_file
9390
extract_cmd.finalize_options()
9491
# Gets listified in `finalize_options`:
9592
assert extract_cmd.input_paths == [extract_cmd.input_dirs]
9693

9794

98-
def test_input_dirs_is_mutually_exclusive_with_input_paths(extract_cmd):
95+
def test_input_dirs_is_mutually_exclusive_with_input_paths(extract_cmd, pot_file):
9996
extract_cmd.input_dirs = this_dir
10097
extract_cmd.input_paths = this_dir
10198
extract_cmd.output_file = pot_file
@@ -104,10 +101,10 @@ def test_input_dirs_is_mutually_exclusive_with_input_paths(extract_cmd):
104101

105102

106103
@freeze_time("1994-11-11")
107-
def test_extraction_with_default_mapping(extract_cmd):
104+
def test_extraction_with_default_mapping(extract_cmd, pot_file):
108105
extract_cmd.copyright_holder = 'FooBar, Inc.'
109106
extract_cmd.msgid_bugs_address = 'bugs.address@email.tld'
110-
extract_cmd.output_file = 'project/i18n/temp.pot'
107+
extract_cmd.output_file = pot_file
111108
extract_cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
112109

113110
extract_cmd.finalize_options()
@@ -153,17 +150,15 @@ def test_extraction_with_default_mapping(extract_cmd):
153150
msgstr[1] ""
154151
155152
"""
156-
with open(pot_file) as f:
157-
actual_content = f.read()
158-
assert expected_content == actual_content
153+
assert expected_content == pot_file.read_text()
159154

160155

161156
@freeze_time("1994-11-11")
162-
def test_extraction_with_mapping_file(extract_cmd):
157+
def test_extraction_with_mapping_file(extract_cmd, pot_file):
163158
extract_cmd.copyright_holder = 'FooBar, Inc.'
164159
extract_cmd.msgid_bugs_address = 'bugs.address@email.tld'
165160
extract_cmd.mapping_file = 'mapping.cfg'
166-
extract_cmd.output_file = 'project/i18n/temp.pot'
161+
extract_cmd.output_file = pot_file
167162
extract_cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
168163

169164
extract_cmd.finalize_options()
@@ -203,13 +198,11 @@ def test_extraction_with_mapping_file(extract_cmd):
203198
msgstr[1] ""
204199
205200
"""
206-
with open(pot_file) as f:
207-
actual_content = f.read()
208-
assert expected_content == actual_content
201+
assert expected_content == pot_file.read_text()
209202

210203

211204
@freeze_time("1994-11-11")
212-
def test_extraction_with_mapping_dict(extract_cmd):
205+
def test_extraction_with_mapping_dict(extract_cmd, pot_file):
213206
extract_cmd.distribution.message_extractors = {
214207
'project': [
215208
('**/ignored/**.*', 'ignore', None),
@@ -218,7 +211,7 @@ def test_extraction_with_mapping_dict(extract_cmd):
218211
}
219212
extract_cmd.copyright_holder = 'FooBar, Inc.'
220213
extract_cmd.msgid_bugs_address = 'bugs.address@email.tld'
221-
extract_cmd.output_file = 'project/i18n/temp.pot'
214+
extract_cmd.output_file = pot_file
222215
extract_cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
223216

224217
extract_cmd.finalize_options()
@@ -258,19 +251,17 @@ def test_extraction_with_mapping_dict(extract_cmd):
258251
msgstr[1] ""
259252
260253
"""
261-
with open(pot_file) as f:
262-
actual_content = f.read()
263-
assert expected_content == actual_content
254+
assert expected_content == pot_file.read_text()
264255

265256

266-
def test_extraction_add_location_file(extract_cmd):
257+
def test_extraction_add_location_file(extract_cmd, pot_file):
267258
extract_cmd.distribution.message_extractors = {
268259
'project': [
269260
('**/ignored/**.*', 'ignore', None),
270261
('**.py', 'python', None),
271262
],
272263
}
273-
extract_cmd.output_file = 'project/i18n/temp.pot'
264+
extract_cmd.output_file = pot_file
274265
extract_cmd.add_location = 'file'
275266
extract_cmd.omit_header = True
276267

@@ -288,6 +279,4 @@ def test_extraction_add_location_file(extract_cmd):
288279
msgstr[1] ""
289280
290281
"""
291-
with open(pot_file) as f:
292-
actual_content = f.read()
293-
assert expected_content == actual_content
282+
assert expected_content == pot_file.read_text()

0 commit comments

Comments
 (0)