Skip to content

Commit a5458fb

Browse files
committed
Write tests for msgcat
* Create basic tests to verify the functionality of msgcat, specifically the concatenation of catalogs, merging of message flags, locations, etc. * Remove the validation of options sort-output, sort-by-file, unique, use-first, as they are initialized in the function initialize_options.
1 parent a1bf8d4 commit a5458fb

2 files changed

Lines changed: 213 additions & 13 deletions

File tree

babel/messages/frontend.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,6 @@ class MessageConcatenation(CommandMixin):
891891
'unique',
892892
'properties-input',
893893
'stringtable-input',
894-
'use-first',
895894
'no-escape',
896895
'escape',
897896
'force-po',
@@ -946,11 +945,6 @@ def finalize_options(self):
946945
if not self.output_file:
947946
raise OptionError('you must specify the output file')
948947

949-
if self.unique is None:
950-
self.unique = False
951-
if self.use_first is None:
952-
self.use_first = True
953-
954948
if self.no_wrap and self.width:
955949
raise OptionError("'--no-wrap' and '--width' are mutually exclusive")
956950
if not self.no_wrap and not self.width:
@@ -967,11 +961,6 @@ def finalize_options(self):
967961
if self.unique:
968962
self.less_than = 2
969963

970-
if self.sort_output is None:
971-
self.sort_output = False
972-
if self.sort_by_file is None:
973-
self.sort_by_file = True
974-
975964
def _prepare(self):
976965
self.message_count = defaultdict(int)
977966

@@ -982,7 +971,7 @@ def _prepare(self):
982971
self.message_count[message.id] += 1
983972

984973
def run(self):
985-
catalog = Catalog(fuzzy=False)
974+
catalog = Catalog()
986975
self._prepare()
987976

988977
for filename in self.input_files:

tests/messages/test_frontend.py

Lines changed: 212 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from babel import __version__ as VERSION
2929
from babel.dates import format_datetime
30-
from babel.messages import Catalog, extract, frontend
30+
from babel.messages import Catalog, extract, frontend, pofile
3131
from babel.messages.frontend import (
3232
BaseError,
3333
CommandLineInterface,
@@ -715,6 +715,217 @@ def test_supports_width(self):
715715
assert expected_content == actual_content
716716

717717

718+
class ConcatanationMessagesTestCase(unittest.TestCase):
719+
720+
def setUp(self):
721+
self.olddir = os.getcwd()
722+
os.chdir(data_dir)
723+
724+
self.dist = Distribution(TEST_PROJECT_DISTRIBUTION_DATA)
725+
self.cmd = frontend.MessageConcatenation(self.dist)
726+
self.cmd.initialize_options()
727+
728+
self.temp1 = f'{i18n_dir}/msgcat_temp1.po'
729+
self.temp2 = f'{i18n_dir}/msgcat_temp2.po'
730+
self.output_file = f'{i18n_dir}/msgcat.po'
731+
732+
with open(self.temp1, 'wb') as file:
733+
catalog = Catalog()
734+
catalog.add('other1', string='Other 1', locations=[('simple.py', 1)], flags=['flag1000'])
735+
catalog.add('other2', string='Other 2', locations=[('simple.py', 10)])
736+
catalog.add('same', string='Same', locations=[('simple.py', 100)], flags=['flag1', 'flag1.2'])
737+
catalog.add('almost_same', string='Almost same', locations=[('simple.py', 1000)], flags=['flag2'])
738+
pofile.write_po(file, catalog)
739+
740+
with open(self.temp2, 'wb') as file:
741+
catalog = Catalog()
742+
catalog.add('other3', string='Other 3', locations=[('hard.py', 1)])
743+
catalog.add('other4', string='Other 4', locations=[('hard.py', 10)])
744+
catalog.add('almost_same', string='A bit same', locations=[('hard.py', 1000)], flags=['flag3'])
745+
catalog.add('same', string='Same', locations=[('hard.py', 100)], flags=['flag4'])
746+
pofile.write_po(file, catalog)
747+
748+
def tearDown(self):
749+
for file in [self.temp1, self.temp2, self.output_file]:
750+
if os.path.isfile(file):
751+
os.unlink(file)
752+
753+
def test_no_input_files(self):
754+
with pytest.raises(OptionError):
755+
self.cmd.finalize_options()
756+
757+
def test_no_output_file(self):
758+
self.cmd.input_files = ['project/i18n/messages.pot']
759+
with pytest.raises(OptionError):
760+
self.cmd.finalize_options()
761+
762+
@freeze_time("1994-11-11")
763+
def test_default(self):
764+
self.cmd.input_files = [self.temp1, self.temp2]
765+
self.cmd.output_file = self.output_file
766+
767+
self.cmd.finalize_options()
768+
self.cmd.run()
769+
770+
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
771+
expected_content = fr"""# Translations template for PROJECT.
772+
# Copyright (C) 1994 ORGANIZATION
773+
# This file is distributed under the same license as the PROJECT project.
774+
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
775+
#
776+
#, fuzzy
777+
msgid ""
778+
msgstr ""
779+
"Project-Id-Version: PROJECT VERSION\n"
780+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
781+
"POT-Creation-Date: {date}\n"
782+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
783+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
784+
"Language-Team: LANGUAGE <LL@li.org>\n"
785+
"MIME-Version: 1.0\n"
786+
"Content-Type: text/plain; charset=utf-8\n"
787+
"Content-Transfer-Encoding: 8bit\n"
788+
"Generated-By: Babel {VERSION}\n"
789+
790+
#: simple.py:1
791+
#, flag1000
792+
msgid "other1"
793+
msgstr "Other 1"
794+
795+
#: simple.py:10
796+
msgid "other2"
797+
msgstr "Other 2"
798+
799+
#: hard.py:100 simple.py:100
800+
#, flag1, flag1.2, flag4
801+
msgid "same"
802+
msgstr "Same"
803+
804+
#: hard.py:1000 simple.py:1000
805+
#, flag2, flag3
806+
msgid "almost_same"
807+
msgstr "Almost same"
808+
809+
#: hard.py:1
810+
msgid "other3"
811+
msgstr "Other 3"
812+
813+
#: hard.py:10
814+
msgid "other4"
815+
msgstr "Other 4"
816+
817+
"""
818+
819+
with open(self.output_file, 'r') as f:
820+
actual_content = f.read()
821+
assert expected_content == actual_content
822+
823+
@freeze_time("1994-11-11")
824+
def test_unique(self):
825+
self.cmd.input_files = [self.temp1, self.temp2]
826+
self.cmd.output_file = self.output_file
827+
self.cmd.unique = True
828+
829+
self.cmd.finalize_options()
830+
self.cmd.run()
831+
832+
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
833+
expected_content = fr"""# Translations template for PROJECT.
834+
# Copyright (C) 1994 ORGANIZATION
835+
# This file is distributed under the same license as the PROJECT project.
836+
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
837+
#
838+
#, fuzzy
839+
msgid ""
840+
msgstr ""
841+
"Project-Id-Version: PROJECT VERSION\n"
842+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
843+
"POT-Creation-Date: {date}\n"
844+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
845+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
846+
"Language-Team: LANGUAGE <LL@li.org>\n"
847+
"MIME-Version: 1.0\n"
848+
"Content-Type: text/plain; charset=utf-8\n"
849+
"Content-Transfer-Encoding: 8bit\n"
850+
"Generated-By: Babel {VERSION}\n"
851+
852+
#: simple.py:1
853+
#, flag1000
854+
msgid "other1"
855+
msgstr "Other 1"
856+
857+
#: simple.py:10
858+
msgid "other2"
859+
msgstr "Other 2"
860+
861+
#: hard.py:1
862+
msgid "other3"
863+
msgstr "Other 3"
864+
865+
#: hard.py:10
866+
msgid "other4"
867+
msgstr "Other 4"
868+
869+
"""
870+
871+
with open(self.output_file, 'r') as f:
872+
actual_content = f.read()
873+
assert expected_content == actual_content
874+
875+
self.cmd.less_than = 2
876+
self.cmd.finalize_options()
877+
self.cmd.run()
878+
879+
with open(self.output_file, 'r') as f:
880+
actual_content = f.read()
881+
assert expected_content == actual_content
882+
883+
@freeze_time("1994-11-11")
884+
def test_more_than(self):
885+
self.cmd.input_files = [self.temp1, self.temp2]
886+
self.cmd.output_file = self.output_file
887+
self.cmd.more_than = 1
888+
889+
self.cmd.finalize_options()
890+
self.cmd.run()
891+
892+
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
893+
expected_content = fr"""# Translations template for PROJECT.
894+
# Copyright (C) 1994 ORGANIZATION
895+
# This file is distributed under the same license as the PROJECT project.
896+
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
897+
#
898+
#, fuzzy
899+
msgid ""
900+
msgstr ""
901+
"Project-Id-Version: PROJECT VERSION\n"
902+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
903+
"POT-Creation-Date: {date}\n"
904+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
905+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
906+
"Language-Team: LANGUAGE <LL@li.org>\n"
907+
"MIME-Version: 1.0\n"
908+
"Content-Type: text/plain; charset=utf-8\n"
909+
"Content-Transfer-Encoding: 8bit\n"
910+
"Generated-By: Babel {VERSION}\n"
911+
912+
#: hard.py:100 simple.py:100
913+
#, flag1, flag1.2, flag4
914+
msgid "same"
915+
msgstr "Same"
916+
917+
#: hard.py:1000 simple.py:1000
918+
#, flag2, flag3
919+
msgid "almost_same"
920+
msgstr "Almost same"
921+
922+
"""
923+
924+
with open(self.output_file, 'r') as f:
925+
actual_content = f.read()
926+
assert expected_content == actual_content
927+
928+
718929
class CommandLineInterfaceTestCase(unittest.TestCase):
719930

720931
def setUp(self):

0 commit comments

Comments
 (0)