Skip to content

Commit 4774889

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 8695769 commit 4774889

2 files changed

Lines changed: 240 additions & 12 deletions

File tree

babel/messages/frontend.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,6 @@ class MessageConcatenation(CommandMixin):
926926
'unique',
927927
'properties-input',
928928
'stringtable-input',
929-
'use-first',
930929
'no-escape',
931930
'escape',
932931
'force-po',
@@ -981,11 +980,6 @@ def finalize_options(self):
981980
if not self.output_file:
982981
raise OptionError('you must specify the output file')
983982

984-
if self.unique is None:
985-
self.unique = False
986-
if self.use_first is None:
987-
self.use_first = True
988-
989983
if self.no_wrap and self.width:
990984
raise OptionError("'--no-wrap' and '--width' are mutually exclusive")
991985
if not self.no_wrap and not self.width:
@@ -1002,11 +996,6 @@ def finalize_options(self):
1002996
if self.unique:
1003997
self.less_than = 2
1004998

1005-
if self.sort_output is None:
1006-
self.sort_output = False
1007-
if self.sort_by_file is None:
1008-
self.sort_by_file = True
1009-
1010999
def _prepare(self):
10111000
self.message_count = defaultdict(int)
10121001

@@ -1017,7 +1006,7 @@ def _prepare(self):
10171006
self.message_count[message.id] += 1
10181007

10191008
def run(self):
1020-
catalog = Catalog(fuzzy=False)
1009+
catalog = Catalog()
10211010
self._prepare()
10221011

10231012
for filename in self.input_files:
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
#
2+
# Copyright (C) 2007-2011 Edgewall Software, 2013-2025 the Babel team
3+
# All rights reserved.
4+
#
5+
# This software is licensed as described in the file LICENSE, which
6+
# you should have received as part of this distribution. The terms
7+
# are also available at https://github.com/python-babel/babel/blob/master/LICENSE.
8+
#
9+
# This software consists of voluntary contributions made by many
10+
# individuals. For the exact contribution history, see the revision
11+
# history and logs, available at https://github.com/python-babel/babel/commits/master/.
12+
13+
from __future__ import annotations
14+
15+
import os
16+
import unittest
17+
from datetime import datetime
18+
19+
import pytest
20+
from freezegun import freeze_time
21+
22+
from babel import __version__ as VERSION
23+
from babel.dates import format_datetime
24+
from babel.messages import Catalog, frontend, pofile
25+
from babel.messages.frontend import OptionError
26+
from babel.util import LOCALTZ
27+
from tests.messages.consts import TEST_PROJECT_DISTRIBUTION_DATA, data_dir, i18n_dir
28+
from tests.messages.utils import Distribution
29+
30+
31+
class ConcatanationMessagesTestCase(unittest.TestCase):
32+
33+
def setUp(self):
34+
self.olddir = os.getcwd()
35+
os.chdir(data_dir)
36+
37+
self.dist = Distribution(TEST_PROJECT_DISTRIBUTION_DATA)
38+
self.cmd = frontend.MessageConcatenation(self.dist)
39+
self.cmd.initialize_options()
40+
41+
self.temp1 = f'{i18n_dir}/msgcat_temp1.po'
42+
self.temp2 = f'{i18n_dir}/msgcat_temp2.po'
43+
self.output_file = f'{i18n_dir}/msgcat.po'
44+
45+
with open(self.temp1, 'wb') as file:
46+
catalog = Catalog()
47+
catalog.add('other1', string='Other 1', locations=[('simple.py', 1)], flags=['flag1000'])
48+
catalog.add('other2', string='Other 2', locations=[('simple.py', 10)])
49+
catalog.add('same', string='Same', locations=[('simple.py', 100)], flags=['flag1', 'flag1.2'])
50+
catalog.add('almost_same', string='Almost same', locations=[('simple.py', 1000)], flags=['flag2'])
51+
pofile.write_po(file, catalog)
52+
53+
with open(self.temp2, 'wb') as file:
54+
catalog = Catalog()
55+
catalog.add('other3', string='Other 3', locations=[('hard.py', 1)])
56+
catalog.add('other4', string='Other 4', locations=[('hard.py', 10)])
57+
catalog.add('almost_same', string='A bit same', locations=[('hard.py', 1000)], flags=['flag3'])
58+
catalog.add('same', string='Same', locations=[('hard.py', 100)], flags=['flag4'])
59+
pofile.write_po(file, catalog)
60+
61+
def tearDown(self):
62+
for file in [self.temp1, self.temp2, self.output_file]:
63+
if os.path.isfile(file):
64+
os.unlink(file)
65+
66+
def test_no_input_files(self):
67+
with pytest.raises(OptionError):
68+
self.cmd.finalize_options()
69+
70+
def test_no_output_file(self):
71+
self.cmd.input_files = ['project/i18n/messages.pot']
72+
with pytest.raises(OptionError):
73+
self.cmd.finalize_options()
74+
75+
@freeze_time("1994-11-11")
76+
def test_default(self):
77+
self.cmd.input_files = [self.temp1, self.temp2]
78+
self.cmd.output_file = self.output_file
79+
80+
self.cmd.finalize_options()
81+
self.cmd.run()
82+
83+
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
84+
expected_content = fr"""# Translations template for PROJECT.
85+
# Copyright (C) 1994 ORGANIZATION
86+
# This file is distributed under the same license as the PROJECT project.
87+
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
88+
#
89+
#, fuzzy
90+
msgid ""
91+
msgstr ""
92+
"Project-Id-Version: PROJECT VERSION\n"
93+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
94+
"POT-Creation-Date: {date}\n"
95+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
96+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
97+
"Language-Team: LANGUAGE <LL@li.org>\n"
98+
"MIME-Version: 1.0\n"
99+
"Content-Type: text/plain; charset=utf-8\n"
100+
"Content-Transfer-Encoding: 8bit\n"
101+
"Generated-By: Babel {VERSION}\n"
102+
103+
#: simple.py:1
104+
#, flag1000
105+
msgid "other1"
106+
msgstr "Other 1"
107+
108+
#: simple.py:10
109+
msgid "other2"
110+
msgstr "Other 2"
111+
112+
#: hard.py:100 simple.py:100
113+
#, flag1, flag1.2, flag4
114+
msgid "same"
115+
msgstr "Same"
116+
117+
#: hard.py:1000 simple.py:1000
118+
#, flag2, flag3
119+
msgid "almost_same"
120+
msgstr "Almost same"
121+
122+
#: hard.py:1
123+
msgid "other3"
124+
msgstr "Other 3"
125+
126+
#: hard.py:10
127+
msgid "other4"
128+
msgstr "Other 4"
129+
130+
"""
131+
132+
with open(self.output_file, 'r') as f:
133+
actual_content = f.read()
134+
assert expected_content == actual_content
135+
136+
@freeze_time("1994-11-11")
137+
def test_unique(self):
138+
self.cmd.input_files = [self.temp1, self.temp2]
139+
self.cmd.output_file = self.output_file
140+
self.cmd.unique = True
141+
142+
self.cmd.finalize_options()
143+
self.cmd.run()
144+
145+
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
146+
expected_content = fr"""# Translations template for PROJECT.
147+
# Copyright (C) 1994 ORGANIZATION
148+
# This file is distributed under the same license as the PROJECT project.
149+
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
150+
#
151+
#, fuzzy
152+
msgid ""
153+
msgstr ""
154+
"Project-Id-Version: PROJECT VERSION\n"
155+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
156+
"POT-Creation-Date: {date}\n"
157+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
158+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
159+
"Language-Team: LANGUAGE <LL@li.org>\n"
160+
"MIME-Version: 1.0\n"
161+
"Content-Type: text/plain; charset=utf-8\n"
162+
"Content-Transfer-Encoding: 8bit\n"
163+
"Generated-By: Babel {VERSION}\n"
164+
165+
#: simple.py:1
166+
#, flag1000
167+
msgid "other1"
168+
msgstr "Other 1"
169+
170+
#: simple.py:10
171+
msgid "other2"
172+
msgstr "Other 2"
173+
174+
#: hard.py:1
175+
msgid "other3"
176+
msgstr "Other 3"
177+
178+
#: hard.py:10
179+
msgid "other4"
180+
msgstr "Other 4"
181+
182+
"""
183+
184+
with open(self.output_file, 'r') as f:
185+
actual_content = f.read()
186+
assert expected_content == actual_content
187+
188+
self.cmd.less_than = 2
189+
self.cmd.finalize_options()
190+
self.cmd.run()
191+
192+
with open(self.output_file, 'r') as f:
193+
actual_content = f.read()
194+
assert expected_content == actual_content
195+
196+
@freeze_time("1994-11-11")
197+
def test_more_than(self):
198+
self.cmd.input_files = [self.temp1, self.temp2]
199+
self.cmd.output_file = self.output_file
200+
self.cmd.more_than = 1
201+
202+
self.cmd.finalize_options()
203+
self.cmd.run()
204+
205+
date = format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')
206+
expected_content = fr"""# Translations template for PROJECT.
207+
# Copyright (C) 1994 ORGANIZATION
208+
# This file is distributed under the same license as the PROJECT project.
209+
# FIRST AUTHOR <EMAIL@ADDRESS>, 1994.
210+
#
211+
#, fuzzy
212+
msgid ""
213+
msgstr ""
214+
"Project-Id-Version: PROJECT VERSION\n"
215+
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
216+
"POT-Creation-Date: {date}\n"
217+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
218+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
219+
"Language-Team: LANGUAGE <LL@li.org>\n"
220+
"MIME-Version: 1.0\n"
221+
"Content-Type: text/plain; charset=utf-8\n"
222+
"Content-Transfer-Encoding: 8bit\n"
223+
"Generated-By: Babel {VERSION}\n"
224+
225+
#: hard.py:100 simple.py:100
226+
#, flag1, flag1.2, flag4
227+
msgid "same"
228+
msgstr "Same"
229+
230+
#: hard.py:1000 simple.py:1000
231+
#, flag2, flag3
232+
msgid "almost_same"
233+
msgstr "Almost same"
234+
235+
"""
236+
237+
with open(self.output_file, 'r') as f:
238+
actual_content = f.read()
239+
assert expected_content == actual_content

0 commit comments

Comments
 (0)