-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathtest_extract.py
More file actions
330 lines (273 loc) · 10.1 KB
/
test_extract.py
File metadata and controls
330 lines (273 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#
# Copyright (C) 2007-2011 Edgewall Software, 2013-2025 the Babel team
# All rights reserved.
#
# This software is licensed as described in the file LICENSE, which
# you should have received as part of this distribution. The terms
# are also available at https://github.com/python-babel/babel/blob/master/LICENSE.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at https://github.com/python-babel/babel/commits/master/.
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
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, this_dir
from tests.messages.utils import Distribution
@pytest.fixture()
def extract_cmd(monkeypatch):
monkeypatch.chdir(data_dir)
dist = Distribution(TEST_PROJECT_DISTRIBUTION_DATA)
extract_cmd = frontend.ExtractMessages(dist)
extract_cmd.initialize_options()
return extract_cmd
def test_neither_default_nor_custom_keywords(extract_cmd):
extract_cmd.output_file = 'dummy'
extract_cmd.no_default_keywords = True
with pytest.raises(OptionError):
extract_cmd.finalize_options()
def test_no_output_file_specified(extract_cmd):
with pytest.raises(OptionError):
extract_cmd.finalize_options()
def test_both_sort_output_and_sort_by_file(extract_cmd):
extract_cmd.output_file = 'dummy'
extract_cmd.sort_output = True
extract_cmd.sort_by_file = True
with pytest.raises(OptionError):
extract_cmd.finalize_options()
def test_invalid_file_or_dir_input_path(extract_cmd):
extract_cmd.input_paths = 'nonexistent_path'
extract_cmd.output_file = 'dummy'
with pytest.raises(OptionError):
extract_cmd.finalize_options()
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 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, 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, pot_file):
extract_cmd.input_dirs = this_dir
extract_cmd.output_file = pot_file
extract_cmd.finalize_options()
# Gets listified in `finalize_options`:
assert extract_cmd.input_paths == [extract_cmd.input_dirs]
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
with pytest.raises(OptionError):
extract_cmd.finalize_options()
@freeze_time("1994-11-11")
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 = pot_file
extract_cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
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')
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
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, {time.strftime('%Y')}.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: {date}\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. TRANSLATOR: This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
#: project/ignored/this_wont_normally_be_here.py:11
msgid "FooBar"
msgid_plural "FooBars"
msgstr[0] ""
msgstr[1] ""
"""
assert expected_content == pot_file.read_text()
@freeze_time("1994-11-11")
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 = pot_file
extract_cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
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')
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
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, {time.strftime('%Y')}.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: {date}\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. TRANSLATOR: This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
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):
extract_cmd.distribution.message_extractors = {
'project': [
(ignore_pattern, 'ignore', None),
('**.py', 'python', None),
],
}
extract_cmd.copyright_holder = 'FooBar, Inc.'
extract_cmd.msgid_bugs_address = 'bugs.address@email.tld'
extract_cmd.output_file = pot_file
extract_cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:'
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')
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
# project.
# FIRST AUTHOR <EMAIL@ADDRESS>, {time.strftime('%Y')}.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: TestProject 0.1\n"
"Report-Msgid-Bugs-To: bugs.address@email.tld\n"
"POT-Creation-Date: {date}\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel {VERSION}\n"
#. TRANSLATOR: This will be a translator coment,
#. that will include several lines
#: project/file1.py:8
msgid "bar"
msgstr ""
#: project/file2.py:9
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
"""
assert expected_content == pot_file.read_text()
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 = pot_file
extract_cmd.add_location = 'file'
extract_cmd.omit_header = True
extract_cmd.finalize_options()
extract_cmd.run()
expected_content = r"""#: project/file1.py
msgid "bar"
msgstr ""
#: project/file2.py
msgid "foobar"
msgid_plural "foobars"
msgstr[0] ""
msgstr[1] ""
"""
assert expected_content == pot_file.read_text()
def test_extraction_with_mapping_file_with_keywords(extract_cmd, pot_file):
"""
Test that keywords specified in mapping config file are properly parsed,
and merged with default keywords.
"""
extract_cmd.mapping_file = 'mapping_with_keywords.cfg'
extract_cmd.output_file = pot_file
extract_cmd.input_paths = 'project'
extract_cmd.finalize_options()
extract_cmd.run()
with pot_file.open() as f:
catalog = read_po(f)
for msgid in ('bar', 'Choice X', 'Choice Y', 'Option C', 'Option A'):
msg = catalog[msgid]
assert not msg.auto_comments # This configuration didn't specify SPECIAL:...
assert msg.pluralizable == (msgid == 'Option A')
def test_extraction_with_mapping_file_with_comments(extract_cmd, pot_file):
"""
Test that add_comments specified in mapping config file are properly parsed.
Uses TOML format to test that code path.
"""
extract_cmd.mapping_file = 'mapping_with_keywords_and_comments.toml'
extract_cmd.output_file = pot_file
extract_cmd.input_paths = 'project/issue_1224_test.py'
extract_cmd.finalize_options()
extract_cmd.run()
with pot_file.open() as f:
catalog = read_po(f)
# Check that messages were extracted and have the expected auto_comments
for msgid, expected_comment in [
('Choice X', 'extracted'),
('Choice Y', 'special'),
('Option C', None),
('Option A', None),
]:
msg = catalog[msgid]
if expected_comment:
assert any('SPECIAL' in comment and expected_comment in comment for comment in msg.auto_comments)
else:
assert not msg.auto_comments
assert msg.pluralizable == (msgid == 'Option A')