Debian Codegraph @

translation-canary/tests/unittests/test_translated.py

raw symbols SCIP
1 # Copyright (C) 2015 Red Hat, Inc.
2 #
3 # This copyrighted material is made available to anyone wishing to use,
4 # modify, copy, or redistribute it subject to the terms and conditions of
5 # the GNU Lesser General Public License v.2, or (at your option) any later
6 # version. This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY expressed or implied, including the implied
8 # warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
9 # the GNU Lesser General Public License for more details. You should have
10 # received a copy of the GNU Lesser General Public License along with this
11 # program; if not, write to the Free Software Foundation, Inc., 51 Franklin
12 # Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat trademarks
13 # that are incorporated in the source code or documentation are not subject
14 # to the GNU Lesser General Public License and may only be used or
15 # replicated with the express permission of Red Hat, Inc.
16 #
17 # Red Hat Author(s): David Shea <[email protected]>
18
19 import unittest
20 import unittest.mock
21 import tempfile
22 import warnings
23 import polib
24 import os
25
26 from translation_canary.translated.test_markup import test_markup
27 from translation_canary.translated.test_percentage import test_percentage
28 from translation_canary.translated.test_usability import test_usability, test_msgfmt
29
30 from translation_canary.translated import testFile, testSourceTree
31
32 # convert a polib.POFile into a NamedTemporaryFile
33 def pofile(poobj):
34 f = tempfile.NamedTemporaryFile(suffix='.po')
35 poobj.save(f.name)
36 return f
37
38 # convenience function for creating a single-entry mofile
39 def pofile_from_entry(*args, **kwargs):
40 poobj = polib.POFile()
41 poobj.metadata["Content-Type"] = "text/plain; charset=UTF-8"
42 poobj.append(polib.POEntry(*args, **kwargs))
43 return pofile(poobj)
44
45 class TestMarkup(unittest.TestCase):
46 # I know, pylint, that's the point
47 # pylint: disable=invalid-markup
48
49 def test_ok(self):
50 # no markup
51 with pofile_from_entry(msgid="test string", msgstr="estay ingstray") as p:
52 test_markup(p.name)
53
54 # matching markup
55 with pofile_from_entry(msgid="<b>bold</b> string", msgstr="<b>oldbay</b> ingstray") as p:
56 test_markup(p.name)
57
58 # matching plural
59 with pofile_from_entry(msgid="%d <b>bold</b> string", msgid_plural="%d <b>bold</b> strings",
60 msgstr_plural={0: "%d <b>oldbay</b> ingstray", 1: "%d <b>oldbay</b> instrays"}) as p:
61 test_markup(p.name)
62
63 def test_missing(self):
64 with pofile_from_entry(msgid="<b>bold</b> string", msgstr="oldbay ingstray") as p:
65 self.assertRaises(AssertionError, test_markup, p.name)
66
67 def test_mismatch(self):
68 with pofile_from_entry(msgid="<b>bold</b> string", msgstr="<i>oldbay</i> ingstray") as p:
69 self.assertRaises(AssertionError, test_markup, p.name)
70
71 def test_typo(self):
72 with pofile_from_entry(msgid="<b>bold</b> string", msgstr="<boldbay</b> ingstray") as p:
73 self.assertRaises(AssertionError, test_markup, p.name)
74
75 def test_mismatch_plural(self):
76 with pofile_from_entry(msgid="%d <b>bold</b> string", msgid_plural="%d <b>bold</b> strings",
77 msgstr_plural={0: "%d <b>olbday</b> ingstray", 1: "%d oldbay ingstrays"}) as p:
78 self.assertRaises(AssertionError, test_markup, p.name)
79
80 def test_invalid(self):
81 # Tags themselves are valid, but the XML is not
82 with pofile_from_entry(msgid="<b>bold</b> string", msgstr="<b/>oldbay</b> ingstray") as p:
83 self.assertRaises(AssertionError, test_markup, p.name)
84
85 class TestPercentage(unittest.TestCase):
86 def setUp(self):
87 # Convert warnings into exceptions to make them easier to test for
88 warnings.simplefilter("error")
89 # polib throws a DeprecationWarnings so ignore that
90 warnings.simplefilter("default", DeprecationWarning)
91
92 def tearDown(self):
93 warnings.resetwarnings()
94
95 def test_ok(self):
96 # 100%
97 with pofile_from_entry(msgid="test string", msgstr="estay ingstray") as p:
98 test_percentage(p.name)
99
100 def test_not_ok(self):
101 # 0%
102 with pofile_from_entry(msgid="test string", msgstr="") as p:
103 self.assertRaises(Warning, test_percentage, p.name)
104
105 class TestUsability(unittest.TestCase):
106 def test_ok(self):
107 # what lt's Plural-Forms is supposed to look like
108 poobj = polib.POFile()
109 poobj.metadata["Plural-Forms"] = "nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2)\n"
110
111 with pofile(poobj) as p:
112 test_usability(p.name)
113
114 def test_busted_plural_forms(self):
115 # https://bugzilla.redhat.com/show_bug.cgi?id=1283599
116 poobj = polib.POFile()
117 poobj.metadata["Plural-Forms"] = "nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 or n%100>=20) ? 1 : 2)\n"
118
119 with pofile(poobj) as p:
120 self.assertRaises(Exception, test_usability, p.name)
121
122 class TestMsgFmt(unittest.TestCase):
123 def test_ok(self):
124 with pofile_from_entry(msgid="test string", msgstr="estay ingstray") as p:
125 test_msgfmt(p.name)
126
127 # Test a few cases that msgfmt will catch
128 def test_busted_newlines(self):
129 with pofile_from_entry(msgid="multi\nline\nstring", msgstr="ultimay\ninelay\ningstray\n") as p:
130 self.assertRaises(AssertionError, test_msgfmt, p.name)
131
132 def test_busted_format(self):
133 with pofile_from_entry(msgid="test %(type)s", msgstr="estay", flags=["python-format"]) as p:
134 self.assertRaises(AssertionError, test_msgfmt, p.name)
135
136 def test_translated_format(self):
137 with pofile_from_entry(msgid="test %(type)s", msgstr="estay %(ypetay)", flags=["python-format"]) as p:
138 self.assertRaises(AssertionError, test_msgfmt, p.name)
139
140 # Test the top-level functions
141 # fake tests for testing with
142 def _true_test(_p):
143 pass
144
145 def _false_test(_p):
146 raise AssertionError("no good")
147
148 def _picky_test(p):
149 if os.path.basename(p).startswith("p"):
150 raise AssertionError("I don't like this one")
151
152 class TestTestFile(unittest.TestCase):
153 @unittest.mock.patch("translation_canary.translated._tests", [_true_test])
154 def test_success(self):
155 with pofile_from_entry() as p:
156 self.assertTrue(testFile(p.name))
157
158 @unittest.mock.patch("translation_canary.translated._tests", [_false_test])
159 def test_failure(self):
160 with pofile_from_entry() as p:
161 self.assertFalse(testFile(p.name))
162
163 @unittest.mock.patch("translation_canary.translated._tests", [_true_test])
164 def test_release_mode_success(self):
165 with pofile_from_entry() as p:
166 self.assertTrue(testFile(p.name, releaseMode=True))
167 self.assertTrue(os.path.exists(p.name))
168
169 @unittest.mock.patch("translation_canary.translated._tests", [_false_test])
170 def test_release_mode_failure(self):
171 p = tempfile.NamedTemporaryFile(suffix='.po', delete=False)
172 try:
173 # testFile should return True but delete the file
174 self.assertTrue(testFile(p.name, releaseMode=True))
175 self.assertFalse(os.path.exists(p.name))
176 finally:
177 try:
178 p.close()
179 os.unlink(p.name)
180 except FileNotFoundError:
181 pass
182
183 @unittest.mock.patch("translation_canary.translated._tests", [_false_test])
184 def test_release_mode_failure_with_lingua(self):
185 with tempfile.TemporaryDirectory() as d:
186 open(os.path.join(d, "test.po"), "w").close()
187 with open(os.path.join(d, "LINGUAS"), "w") as linguas:
188 linguas.write("test other\n")
189
190 # Check that test.po is removed and test is removed from LINGUAS
191 self.assertTrue(testFile(os.path.join(d, "test.po"), releaseMode=True))
192 self.assertFalse(os.path.exists(os.path.join(d, "test.po")))
193
194 with open(os.path.join(d, "LINGUAS"), "r") as linguas:
195 self.assertEqual(linguas.read().strip(), "other")
196
197 @unittest.mock.patch("translation_canary.translated._tests", [_false_test])
198 def test_release_mode_failure_with_lingua_no_modify(self):
199 with tempfile.TemporaryDirectory() as d:
200 open(os.path.join(d, "test.po"), "w").close()
201 with open(os.path.join(d, "LINGUAS"), "w") as linguas:
202 linguas.write("test other\n")
203
204 # Check that test.po is removed and test is *not* removed from LINGUAS
205 self.assertTrue(testFile(os.path.join(d, "test.po"), releaseMode=True, modifyLinguas=False))
206 self.assertFalse(os.path.exists(os.path.join(d, "test.po")))
207
208 with open(os.path.join(d, "LINGUAS"), "r") as linguas:
209 self.assertEqual(linguas.read().strip(), "test other")
210
211 @unittest.mock.patch("translation_canary.translated._tests", [_picky_test])
212 class TestTestSourceTree(unittest.TestCase):
213 def setUp(self):
214 self.tmpobj = tempfile.TemporaryDirectory()
215 self.tmpdir = self.tmpobj.name
216
217 def tearDown(self):
218 self.tmpobj.cleanup()
219
220 def test_success(self):
221 open(os.path.join(self.tmpdir, "aa.po"), "w").close()
222 open(os.path.join(self.tmpdir, "ab.po"), "w").close()
223 self.assertTrue(testSourceTree(self.tmpdir))
224
225 def test_some_failure(self):
226 open(os.path.join(self.tmpdir, "aa.po"), "w").close()
227 open(os.path.join(self.tmpdir, "pa.po"), "w").close()
228 self.assertFalse(testSourceTree(self.tmpdir))
229
230 def test_all_failure(self):
231 open(os.path.join(self.tmpdir, "pa.po"), "w").close()
232 open(os.path.join(self.tmpdir, "pb.po"), "w").close()
233 self.assertFalse(testSourceTree(self.tmpdir))
234
235 def test_release_mode_success(self):
236 open(os.path.join(self.tmpdir, "aa.po"), "w").close()
237 open(os.path.join(self.tmpdir, "ab.po"), "w").close()
238 self.assertTrue(testSourceTree(self.tmpdir, releaseMode=True))
239
240 def test_release_mode_failure(self):
241 open(os.path.join(self.tmpdir, "aa.po"), "w").close()
242 open(os.path.join(self.tmpdir, "pa.po"), "w").close()
243 self.assertTrue(testSourceTree(self.tmpdir, releaseMode=True))
244 self.assertTrue(os.path.exists(os.path.join(self.tmpdir, "aa.po")))
245 self.assertFalse(os.path.exists(os.path.join(self.tmpdir, "pa.po")))
246
247 def test_release_mode_failure_with_lingua(self):
248 open(os.path.join(self.tmpdir, "aa.po"), "w").close()
249 open(os.path.join(self.tmpdir, "pa.po"), "w").close()
250 with open(os.path.join(self.tmpdir, "LINGUAS"), "w") as l:
251 l.write("aa pa\n")
252
253 self.assertTrue(testSourceTree(self.tmpdir, releaseMode=True))
254 self.assertTrue(os.path.exists(os.path.join(self.tmpdir, "aa.po")))
255 self.assertFalse(os.path.exists(os.path.join(self.tmpdir, "pa.po")))
256
257 with open(os.path.join(self.tmpdir, "LINGUAS")) as l:
258 self.assertEqual(l.read().strip(), "aa")
259
260 def test_release_mode_failure_with_lingua_no_modify(self):
261 open(os.path.join(self.tmpdir, "aa.po"), "w").close()
262 open(os.path.join(self.tmpdir, "pa.po"), "w").close()
263
264 with open(os.path.join(self.tmpdir, "LINGUAS"), "w") as l:
265 l.write("aa pa\n")
266
267 self.assertTrue(testSourceTree(self.tmpdir, releaseMode=True, modifyLinguas=False))
268 self.assertTrue(os.path.exists(os.path.join(self.tmpdir, "aa.po")))
269 self.assertFalse(os.path.exists(os.path.join(self.tmpdir, "pa.po")))
270
271 with open(os.path.join(self.tmpdir, "LINGUAS")) as l:
272 self.assertEqual(l.read().strip(), "aa pa")