Skip to content

Commit 5a445c4

Browse files
committed
Write tests for msgmerge
* Create basic tests to verify the functionality of msgmerge, specifically the merging of messages and their integration with a compendium. * Remove the definition of sort-output and sort-by-file, and add an additional check for input-files.
1 parent 4774889 commit 5a445c4

2 files changed

Lines changed: 150 additions & 6 deletions

File tree

babel/messages/frontend.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ def initialize_options(self):
11281128
self.sort_by_file = False #
11291129

11301130
def finalize_options(self):
1131-
if len(self.input_files) != 2:
1131+
if not self.input_files or len(self.input_files) != 2:
11321132
raise OptionError('must be two po files')
11331133
if not self.output_file:
11341134
raise OptionError('you must specify the output file')
@@ -1140,11 +1140,6 @@ def finalize_options(self):
11401140
elif self.width is not None:
11411141
self.width = int(self.width)
11421142

1143-
if self.sort_output is None:
1144-
self.sort_output = False
1145-
if self.sort_by_file is None:
1146-
self.sort_by_file = True
1147-
11481143
def run(self):
11491144
def_file, ref_file = self.input_files
11501145
with open(def_file, 'r') as pofile:
@@ -1167,6 +1162,7 @@ def run(self):
11671162
if self.compendium:
11681163
with open(self.compendium, 'r') as pofile:
11691164
compendium_catalog = read_po(pofile)
1165+
11701166
for message in compendium_catalog:
11711167
if message.id in ref_catalog and not ref_catalog[message.id].string:
11721168
ref_catalog[message.id].string = message.string

tests/messages/frontend/test_concat_merge.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,151 @@ def test_more_than(self):
237237
with open(self.output_file, 'r') as f:
238238
actual_content = f.read()
239239
assert expected_content == actual_content
240+
241+
242+
class MergeMessagesTestCase(unittest.TestCase):
243+
244+
@freeze_time("1994-11-11")
245+
def setUp(self):
246+
self.olddir = os.getcwd()
247+
os.chdir(data_dir)
248+
249+
self.dist = Distribution(TEST_PROJECT_DISTRIBUTION_DATA)
250+
self.cmd = frontend.MessageMerge(self.dist)
251+
self.cmd.initialize_options()
252+
253+
self.temp_def = f'{i18n_dir}/msgmerge_def.po'
254+
self.temp_ref = f'{i18n_dir}/msgmerge_ref.pot'
255+
self.compendium = f'{i18n_dir}/compenidum.po'
256+
self.output_file = f'{i18n_dir}/msgmerge.po'
257+
258+
with open(self.temp_ref, 'wb') as file:
259+
catalog = Catalog()
260+
for word in ['word1', 'word2', 'word3', 'word4']:
261+
catalog.add(word)
262+
pofile.write_po(file, catalog)
263+
264+
with open(self.temp_def, 'wb') as file:
265+
catalog = Catalog()
266+
catalog.add('word1', string='Word 1')
267+
catalog.add('word2', string='Word 2')
268+
catalog.add('word3')
269+
pofile.write_po(file, catalog)
270+
271+
with open(self.compendium, 'wb') as file:
272+
catalog = Catalog()
273+
catalog.add('word4', string='Word 4')
274+
catalog.add('word5', string='Word 5')
275+
pofile.write_po(file, catalog)
276+
277+
def tearDown(self):
278+
for file in [self.temp_def, self.temp_ref, self.compendium, self.output_file]:
279+
if os.path.isfile(file):
280+
os.unlink(file)
281+
282+
def test_no_input_files(self):
283+
with pytest.raises(OptionError):
284+
self.cmd.finalize_options()
285+
286+
with pytest.raises(OptionError):
287+
self.cmd.input_files = ['1']
288+
self.cmd.finalize_options()
289+
290+
with pytest.raises(OptionError):
291+
self.cmd.input_files = ['1', '2', '3']
292+
self.cmd.finalize_options()
293+
294+
def test_no_output_file(self):
295+
self.cmd.input_files = ['1', '2']
296+
with pytest.raises(OptionError):
297+
self.cmd.finalize_options()
298+
299+
@freeze_time("1994-11-11")
300+
def test_default(self):
301+
self.cmd.input_files = [self.temp_def, self.temp_ref]
302+
self.cmd.output_file = self.output_file
303+
self.cmd.finalize_options()
304+
self.cmd.run()
305+
306+
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
307+
expected_content = fr"""# Translations template for PROJECT.
308+
# Copyright (C) 1994 ORGANIZATION
309+
# This file is distributed under the same license as the PROJECT project.
310+
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
311+
#
312+
msgid ""
313+
msgstr ""
314+
"Project-Id-Version: PROJECT VERSION\n"
315+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
316+
"POT-Creation-Date: {date}\n"
317+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
318+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
319+
"Language-Team: LANGUAGE <LL@li.org>\n"
320+
"MIME-Version: 1.0\n"
321+
"Content-Type: text/plain; charset=utf-8\n"
322+
"Content-Transfer-Encoding: 8bit\n"
323+
"Generated-By: Babel {VERSION}\n"
324+
325+
msgid "word1"
326+
msgstr "Word 1"
327+
328+
msgid "word2"
329+
msgstr "Word 2"
330+
331+
msgid "word3"
332+
msgstr ""
333+
334+
#, fuzzy
335+
msgid "word4"
336+
msgstr "Word 2"
337+
338+
"""
339+
340+
with open(self.output_file, 'r') as f:
341+
actual_content = f.read()
342+
assert expected_content == actual_content
343+
344+
@freeze_time("1994-11-11")
345+
def test_compenidum(self):
346+
self.cmd.input_files = [self.temp_def, self.temp_ref]
347+
self.cmd.output_file = self.output_file
348+
self.cmd.compendium = self.compendium
349+
self.cmd.finalize_options()
350+
self.cmd.run()
351+
352+
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
353+
expected_content = fr"""# Translations template for PROJECT.
354+
# Copyright (C) 1994 ORGANIZATION
355+
# This file is distributed under the same license as the PROJECT project.
356+
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
357+
#
358+
msgid ""
359+
msgstr ""
360+
"Project-Id-Version: PROJECT VERSION\n"
361+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
362+
"POT-Creation-Date: {date}\n"
363+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
364+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
365+
"Language-Team: LANGUAGE <LL@li.org>\n"
366+
"MIME-Version: 1.0\n"
367+
"Content-Type: text/plain; charset=utf-8\n"
368+
"Content-Transfer-Encoding: 8bit\n"
369+
"Generated-By: Babel {VERSION}\n"
370+
371+
msgid "word1"
372+
msgstr "Word 1"
373+
374+
msgid "word2"
375+
msgstr "Word 2"
376+
377+
msgid "word3"
378+
msgstr ""
379+
380+
msgid "word4"
381+
msgstr "Word 4"
382+
383+
"""
384+
385+
with open(self.output_file, 'r') as f:
386+
actual_content = f.read()
387+
assert expected_content == actual_content

0 commit comments

Comments
 (0)