| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 33 |
def pofile(poobj): |
| 34 |
f = tempfile.NamedTemporaryFile(suffix='.po') |
| 35 |
poobj.save(f.name) |
| 36 |
return f |
| 37 |
|
| 38 |
|
| 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 |
|
| 47 |
|
| 48 |
|
| 49 |
def test_ok(self): |
| 50 |
|
| 51 |
with pofile_from_entry(msgid="test string", msgstr="estay ingstray") as p: |
| 52 |
test_markup(p.name) |
| 53 |
|
| 54 |
|
| 55 |
with pofile_from_entry(msgid="<b>bold</b> string", msgstr="<b>oldbay</b> ingstray") as p: |
| 56 |
test_markup(p.name) |
| 57 |
|
| 58 |
|
| 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 |
|
| 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 |
|
| 88 |
warnings.simplefilter("error") |
| 89 |
|
| 90 |
warnings.simplefilter("default", DeprecationWarning) |
| 91 |
|
| 92 |
def tearDown(self): |
| 93 |
warnings.resetwarnings() |
| 94 |
|
| 95 |
def test_ok(self): |
| 96 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 141 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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") |