Skip to content

Commit 99ac987

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 cb71c93 commit 99ac987

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
@@ -1006,6 +1006,7 @@ class MessageMerge(CommandMixin):
10061006
('directory=', 'D', ''),
10071007
('compendium=', 'C', ''),
10081008
('c-overwrite', '', ''),
1009+
('no-compendium-comment', '', ''),
10091010
('update', 'U', ''),
10101011
('output-file=', 'o', ''),
10111012
('backup', None, ''),
@@ -1058,6 +1059,7 @@ class MessageMerge(CommandMixin):
10581059
'sort-by-file',
10591060
'c-overwrite',
10601061
'backup',
1062+
'no-compendium-comment',
10611063
]
10621064

10631065
option_choices = {
@@ -1067,8 +1069,11 @@ class MessageMerge(CommandMixin):
10671069
def initialize_options(self):
10681070
self.input_files = None #
10691071
self.directory = None
1072+
10701073
self.compendium = None #~
10711074
self.c_overwrite = False #
1075+
self.no_compendium_comment = None #
1076+
10721077
self.update = None #
10731078
self.output_file = None #
10741079
self.backup = False #
@@ -1086,7 +1091,7 @@ def initialize_options(self):
10861091
self.escape = None
10871092
self.force_po = None
10881093
self.indent = None
1089-
self.no_location = None
1094+
self.no_location = None #
10901095
self.add_location = None
10911096
self.strict = None
10921097
self.properties_output = None
@@ -1136,13 +1141,16 @@ def run(self):
11361141

11371142
current.string = message.string
11381143
current.flags = [flag for flag in current.flags if flag != 'fuzzy']
1139-
current.auto_comments.append(self.compendium)
1144+
1145+
if not self.no_compendium_comment:
1146+
current.auto_comments.append(self.compendium)
11401147

11411148
output_path = def_file if self.update else self.output_file
11421149
with open(output_path, 'wb') as outfile:
11431150
write_po(
11441151
outfile,
11451152
catalog,
1153+
no_location=self.no_location,
11461154
width=self.width,
11471155
sort_by_file=self.sort_by_file,
11481156
sort_output=self.sort_output,

tests/messages/test_frontend.py

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

958958
with open(self.compendium, 'wb') as file:
959959
catalog = Catalog()
960+
catalog.add('word1', string='Comp Word 1')
961+
catalog.add('word2', string='Comp Word 2')
960962
catalog.add('word4', string='Word 4')
961963
catalog.add('word5', string='Word 5')
962964
pofile.write_po(file, catalog)
@@ -983,10 +985,19 @@ def test_no_output_file(self):
983985
with pytest.raises(OptionError):
984986
self.cmd.finalize_options()
985987

988+
self.cmd.output_file = '2'
989+
self.cmd.finalize_options()
990+
991+
self.cmd.output_file = None
992+
self.cmd.update = True
993+
self.cmd.finalize_options()
994+
995+
986996
@freeze_time("1994-11-11")
987997
def test_default(self):
988998
self.cmd.input_files = [self.temp_def, self.temp_ref]
989999
self.cmd.output_file = self.output_file
1000+
self.cmd.no_fuzzy_matching = True
9901001
self.cmd.finalize_options()
9911002
self.cmd.run()
9921003

@@ -996,6 +1007,7 @@ def test_default(self):
9961007
# This file is distributed under the same license as the PROJECT project.
9971008
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
9981009
#
1010+
#, fuzzy
9991011
msgid ""
10001012
msgstr ""
10011013
"Project-Id-Version: PROJECT VERSION\n"
@@ -1018,9 +1030,8 @@ def test_default(self):
10181030
msgid "word3"
10191031
msgstr ""
10201032
1021-
#, fuzzy
10221033
msgid "word4"
1023-
msgstr "Word 2"
1034+
msgstr ""
10241035
10251036
"""
10261037

@@ -1033,6 +1044,8 @@ def test_compenidum(self):
10331044
self.cmd.input_files = [self.temp_def, self.temp_ref]
10341045
self.cmd.output_file = self.output_file
10351046
self.cmd.compendium = self.compendium
1047+
self.cmd.no_fuzzy_matching = True
1048+
self.cmd.no_compendium_comment = True
10361049
self.cmd.finalize_options()
10371050
self.cmd.run()
10381051

@@ -1042,6 +1055,7 @@ def test_compenidum(self):
10421055
# This file is distributed under the same license as the PROJECT project.
10431056
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
10441057
#
1058+
#, fuzzy
10451059
msgid ""
10461060
msgstr ""
10471061
"Project-Id-Version: PROJECT VERSION\n"
@@ -1073,6 +1087,60 @@ def test_compenidum(self):
10731087
actual_content = f.read()
10741088
assert expected_content == actual_content
10751089

1090+
@freeze_time("1994-11-11")
1091+
def test_compenidum(self):
1092+
self.cmd.input_files = [self.temp_def, self.temp_ref]
1093+
self.cmd.output_file = self.output_file
1094+
self.cmd.compendium = self.compendium
1095+
self.cmd.no_fuzzy_matching = True
1096+
self.cmd.no_compendium_comment = True
1097+
self.cmd.c_overwrite = True
1098+
self.cmd.finalize_options()
1099+
self.cmd.run()
1100+
1101+
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
1102+
expected_content = fr"""# Translations template for PROJECT.
1103+
# Copyright (C) 1994 ORGANIZATION
1104+
# This file is distributed under the same license as the PROJECT project.
1105+
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
1106+
#
1107+
#, fuzzy
1108+
msgid ""
1109+
msgstr ""
1110+
"Project-Id-Version: PROJECT VERSION\n"
1111+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
1112+
"POT-Creation-Date: {date}\n"
1113+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1114+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1115+
"Language-Team: LANGUAGE <LL@li.org>\n"
1116+
"MIME-Version: 1.0\n"
1117+
"Content-Type: text/plain; charset=utf-8\n"
1118+
"Content-Transfer-Encoding: 8bit\n"
1119+
"Generated-By: Babel {VERSION}\n"
1120+
1121+
msgid "word1"
1122+
msgstr "Comp Word 1"
1123+
1124+
msgid "word2"
1125+
msgstr "Comp Word 2"
1126+
1127+
msgid "word3"
1128+
msgstr ""
1129+
1130+
msgid "word4"
1131+
msgstr "Word 4"
1132+
1133+
#~ msgid "word1"
1134+
#~ msgstr "Word 1"
1135+
1136+
#~ msgid "word2"
1137+
#~ msgstr "Word 2"
1138+
1139+
"""
1140+
1141+
with open(self.output_file, 'r') as f:
1142+
actual_content = f.read()
1143+
assert expected_content == actual_content
10761144

10771145
class CommandLineInterfaceTestCase(unittest.TestCase):
10781146

0 commit comments

Comments
 (0)