Debian Codegraph @

tests/messages/frontend/test_extract.py

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