Skip to content

Commit 6020107

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 a5458fb commit 6020107

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
@@ -1093,7 +1093,7 @@ def initialize_options(self):
10931093
self.sort_by_file = False #
10941094

10951095
def finalize_options(self):
1096-
if len(self.input_files) != 2:
1096+
if not self.input_files or len(self.input_files) != 2:
10971097
raise OptionError('must be two po files')
10981098
if not self.output_file:
10991099
raise OptionError('you must specify the output file')
@@ -1105,11 +1105,6 @@ def finalize_options(self):
11051105
elif self.width is not None:
11061106
self.width = int(self.width)
11071107

1108-
if self.sort_output is None:
1109-
self.sort_output = False
1110-
if self.sort_by_file is None:
1111-
self.sort_by_file = True
1112-
11131108
def run(self):
11141109
def_file, ref_file = self.input_files
11151110
with open(def_file, 'r') as pofile:
@@ -1132,6 +1127,7 @@ def run(self):
11321127
if self.compendium:
11331128
with open(self.compendium, 'r') as pofile:
11341129
compendium_catalog = read_po(pofile)
1130+
11351131
for message in compendium_catalog:
11361132
if message.id in ref_catalog and not ref_catalog[message.id].string:
11371133
ref_catalog[message.id].string = message.string

tests/messages/test_frontend.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,154 @@ def test_more_than(self):
926926
assert expected_content == actual_content
927927

928928

929+
class MergeMessagesTestCase(unittest.TestCase):
930+
931+
@freeze_time("1994-11-11")
932+
def setUp(self):
933+
self.olddir = os.getcwd()
934+
os.chdir(data_dir)
935+
936+
self.dist = Distribution(TEST_PROJECT_DISTRIBUTION_DATA)
937+
self.cmd = frontend.MessageMerge(self.dist)
938+
self.cmd.initialize_options()
939+
940+
self.temp_def = f'{i18n_dir}/msgmerge_def.po'
941+
self.temp_ref = f'{i18n_dir}/msgmerge_ref.pot'
942+
self.compendium = f'{i18n_dir}/compenidum.po'
943+
self.output_file = f'{i18n_dir}/msgmerge.po'
944+
945+
with open(self.temp_ref, 'wb') as file:
946+
catalog = Catalog()
947+
for word in ['word1', 'word2', 'word3', 'word4']:
948+
catalog.add(word)
949+
pofile.write_po(file, catalog)
950+
951+
with open(self.temp_def, 'wb') as file:
952+
catalog = Catalog()
953+
catalog.add('word1', string='Word 1')
954+
catalog.add('word2', string='Word 2')
955+
catalog.add('word3')
956+
pofile.write_po(file, catalog)
957+
958+
with open(self.compendium, 'wb') as file:
959+
catalog = Catalog()
960+
catalog.add('word4', string='Word 4')
961+
catalog.add('word5', string='Word 5')
962+
pofile.write_po(file, catalog)
963+
964+
def tearDown(self):
965+
for file in [self.temp_def, self.temp_ref, self.compendium, self.output_file]:
966+
if os.path.isfile(file):
967+
os.unlink(file)
968+
969+
def test_no_input_files(self):
970+
with pytest.raises(OptionError):
971+
self.cmd.finalize_options()
972+
973+
with pytest.raises(OptionError):
974+
self.cmd.input_files = ['1']
975+
self.cmd.finalize_options()
976+
977+
with pytest.raises(OptionError):
978+
self.cmd.input_files = ['1', '2', '3']
979+
self.cmd.finalize_options()
980+
981+
def test_no_output_file(self):
982+
self.cmd.input_files = ['1', '2']
983+
with pytest.raises(OptionError):
984+
self.cmd.finalize_options()
985+
986+
@freeze_time("1994-11-11")
987+
def test_default(self):
988+
self.cmd.input_files = [self.temp_def, self.temp_ref]
989+
self.cmd.output_file = self.output_file
990+
self.cmd.finalize_options()
991+
self.cmd.run()
992+
993+
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
994+
expected_content = fr"""# Translations template for PROJECT.
995+
# Copyright (C) 1994 ORGANIZATION
996+
# This file is distributed under the same license as the PROJECT project.
997+
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
998+
#
999+
msgid ""
1000+
msgstr ""
1001+
"Project-Id-Version: PROJECT VERSION\n"
1002+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
1003+
"POT-Creation-Date: {date}\n"
1004+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1005+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1006+
"Language-Team: LANGUAGE <LL@li.org>\n"
1007+
"MIME-Version: 1.0\n"
1008+
"Content-Type: text/plain; charset=utf-8\n"
1009+
"Content-Transfer-Encoding: 8bit\n"
1010+
"Generated-By: Babel {VERSION}\n"
1011+
1012+
msgid "word1"
1013+
msgstr "Word 1"
1014+
1015+
msgid "word2"
1016+
msgstr "Word 2"
1017+
1018+
msgid "word3"
1019+
msgstr ""
1020+
1021+
#, fuzzy
1022+
msgid "word4"
1023+
msgstr "Word 2"
1024+
1025+
"""
1026+
1027+
with open(self.output_file, 'r') as f:
1028+
actual_content = f.read()
1029+
assert expected_content == actual_content
1030+
1031+
@freeze_time("1994-11-11")
1032+
def test_compenidum(self):
1033+
self.cmd.input_files = [self.temp_def, self.temp_ref]
1034+
self.cmd.output_file = self.output_file
1035+
self.cmd.compendium = self.compendium
1036+
self.cmd.finalize_options()
1037+
self.cmd.run()
1038+
1039+
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
1040+
expected_content = fr"""# Translations template for PROJECT.
1041+
# Copyright (C) 1994 ORGANIZATION
1042+
# This file is distributed under the same license as the PROJECT project.
1043+
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
1044+
#
1045+
msgid ""
1046+
msgstr ""
1047+
"Project-Id-Version: PROJECT VERSION\n"
1048+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
1049+
"POT-Creation-Date: {date}\n"
1050+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1051+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1052+
"Language-Team: LANGUAGE <LL@li.org>\n"
1053+
"MIME-Version: 1.0\n"
1054+
"Content-Type: text/plain; charset=utf-8\n"
1055+
"Content-Transfer-Encoding: 8bit\n"
1056+
"Generated-By: Babel {VERSION}\n"
1057+
1058+
msgid "word1"
1059+
msgstr "Word 1"
1060+
1061+
msgid "word2"
1062+
msgstr "Word 2"
1063+
1064+
msgid "word3"
1065+
msgstr ""
1066+
1067+
msgid "word4"
1068+
msgstr "Word 4"
1069+
1070+
"""
1071+
1072+
with open(self.output_file, 'r') as f:
1073+
actual_content = f.read()
1074+
assert expected_content == actual_content
1075+
1076+
9291077
class CommandLineInterfaceTestCase(unittest.TestCase):
9301078

9311079
def setUp(self):

0 commit comments

Comments
 (0)