1212
1313from __future__ import annotations
1414
15- import os
1615import time
1716from datetime import datetime
1817
2524from babel .messages .frontend import OptionError
2625from babel .messages .pofile import read_po
2726from 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
2928from 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
4340def 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):
153150msgstr[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):
203198msgstr[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):
258251msgstr[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):
288279msgstr[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