Skip to content

Commit b00f215

Browse files
committed
Add test for msgmerge compendium overwrite mode with no comments
* Implement a test for `msgmerge` that validates the new mode where compendium entries overwrite messages in the output PO file. * Include the `no_compendium_comment` option to ensure comments about translations sourced from the compendium are not included. * Utilize the `no-location` option to exclude location comments from the output.
1 parent f7ddd85 commit b00f215

2 files changed

Lines changed: 80 additions & 4 deletions

File tree

babel/messages/frontend.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,7 @@ class MessageMerge(CommandMixin):
10411041
('directory=', 'D', ''),
10421042
('compendium=', 'C', ''),
10431043
('c-overwrite', '', ''),
1044+
('no-compendium-comment', '', ''),
10441045
('update', 'U', ''),
10451046
('output-file=', 'o', ''),
10461047
('backup', None, ''),
@@ -1093,6 +1094,7 @@ class MessageMerge(CommandMixin):
10931094
'sort-by-file',
10941095
'c-overwrite',
10951096
'backup',
1097+
'no-compendium-comment',
10961098
]
10971099

10981100
option_choices = {
@@ -1102,8 +1104,11 @@ class MessageMerge(CommandMixin):
11021104
def initialize_options(self):
11031105
self.input_files = None #
11041106
self.directory = None
1107+
11051108
self.compendium = None #~
11061109
self.c_overwrite = False #
1110+
self.no_compendium_comment = None #
1111+
11071112
self.update = None #
11081113
self.output_file = None #
11091114
self.backup = False #
@@ -1121,7 +1126,7 @@ def initialize_options(self):
11211126
self.escape = None
11221127
self.force_po = None
11231128
self.indent = None
1124-
self.no_location = None
1129+
self.no_location = None #
11251130
self.add_location = None
11261131
self.strict = None
11271132
self.properties_output = None
@@ -1171,13 +1176,16 @@ def run(self):
11711176

11721177
current.string = message.string
11731178
current.flags = [flag for flag in current.flags if flag != 'fuzzy']
1174-
current.auto_comments.append(self.compendium)
1179+
1180+
if not self.no_compendium_comment:
1181+
current.auto_comments.append(self.compendium)
11751182

11761183
output_path = def_file if self.update else self.output_file
11771184
with open(output_path, 'wb') as outfile:
11781185
write_po(
11791186
outfile,
11801187
catalog,
1188+
no_location=self.no_location,
11811189
width=self.width,
11821190
sort_by_file=self.sort_by_file,
11831191
sort_output=self.sort_output,

tests/messages/frontend/test_concat_merge.py

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ def setUp(self):
270270

271271
with open(self.compendium, 'wb') as file:
272272
catalog = Catalog()
273+
catalog.add('word1', string='Comp Word 1')
274+
catalog.add('word2', string='Comp Word 2')
273275
catalog.add('word4', string='Word 4')
274276
catalog.add('word5', string='Word 5')
275277
pofile.write_po(file, catalog)
@@ -296,10 +298,18 @@ def test_no_output_file(self):
296298
with pytest.raises(OptionError):
297299
self.cmd.finalize_options()
298300

301+
self.cmd.output_file = '2'
302+
self.cmd.finalize_options()
303+
304+
self.cmd.output_file = None
305+
self.cmd.update = True
306+
self.cmd.finalize_options()
307+
299308
@freeze_time("1994-11-11")
300309
def test_default(self):
301310
self.cmd.input_files = [self.temp_def, self.temp_ref]
302311
self.cmd.output_file = self.output_file
312+
self.cmd.no_fuzzy_matching = True
303313
self.cmd.finalize_options()
304314
self.cmd.run()
305315

@@ -309,6 +319,7 @@ def test_default(self):
309319
# This file is distributed under the same license as the PROJECT project.
310320
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
311321
#
322+
#, fuzzy
312323
msgid ""
313324
msgstr ""
314325
"Project-Id-Version: PROJECT VERSION\n"
@@ -331,9 +342,8 @@ def test_default(self):
331342
msgid "word3"
332343
msgstr ""
333344
334-
#, fuzzy
335345
msgid "word4"
336-
msgstr "Word 2"
346+
msgstr ""
337347
338348
"""
339349

@@ -346,6 +356,8 @@ def test_compenidum(self):
346356
self.cmd.input_files = [self.temp_def, self.temp_ref]
347357
self.cmd.output_file = self.output_file
348358
self.cmd.compendium = self.compendium
359+
self.cmd.no_fuzzy_matching = True
360+
self.cmd.no_compendium_comment = True
349361
self.cmd.finalize_options()
350362
self.cmd.run()
351363

@@ -355,6 +367,7 @@ def test_compenidum(self):
355367
# This file is distributed under the same license as the PROJECT project.
356368
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
357369
#
370+
#, fuzzy
358371
msgid ""
359372
msgstr ""
360373
"Project-Id-Version: PROJECT VERSION\n"
@@ -380,6 +393,61 @@ def test_compenidum(self):
380393
msgid "word4"
381394
msgstr "Word 4"
382395
396+
"""
397+
398+
with open(self.output_file, 'r') as f:
399+
actual_content = f.read()
400+
assert expected_content == actual_content
401+
402+
@freeze_time("1994-11-11")
403+
def test_compendium_overwrite(self):
404+
self.cmd.input_files = [self.temp_def, self.temp_ref]
405+
self.cmd.output_file = self.output_file
406+
self.cmd.compendium = self.compendium
407+
self.cmd.no_fuzzy_matching = True
408+
self.cmd.no_compendium_comment = True
409+
self.cmd.c_overwrite = True
410+
self.cmd.finalize_options()
411+
self.cmd.run()
412+
413+
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
414+
expected_content = fr"""# Translations template for PROJECT.
415+
# Copyright (C) 1994 ORGANIZATION
416+
# This file is distributed under the same license as the PROJECT project.
417+
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
418+
#
419+
#, fuzzy
420+
msgid ""
421+
msgstr ""
422+
"Project-Id-Version: PROJECT VERSION\n"
423+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
424+
"POT-Creation-Date: {date}\n"
425+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
426+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
427+
"Language-Team: LANGUAGE <LL@li.org>\n"
428+
"MIME-Version: 1.0\n"
429+
"Content-Type: text/plain; charset=utf-8\n"
430+
"Content-Transfer-Encoding: 8bit\n"
431+
"Generated-By: Babel {VERSION}\n"
432+
433+
msgid "word1"
434+
msgstr "Comp Word 1"
435+
436+
msgid "word2"
437+
msgstr "Comp Word 2"
438+
439+
msgid "word3"
440+
msgstr ""
441+
442+
msgid "word4"
443+
msgstr "Word 4"
444+
445+
#~ msgid "word1"
446+
#~ msgstr "Word 1"
447+
448+
#~ msgid "word2"
449+
#~ msgstr "Word 2"
450+
383451
"""
384452

385453
with open(self.output_file, 'r') as f:

0 commit comments

Comments
 (0)