| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
""" |
| 22 |
Framework for running tests against translatable strings. |
| 23 |
|
| 24 |
Tests are loaded from modules in this directory. A test is any callable object |
| 25 |
within the module with a name that starts with 'test_'. |
| 26 |
|
| 27 |
Each test is called with a POEntry object as an argument. A test passes if it |
| 28 |
returns without raising an exception. |
| 29 |
""" |
| 30 |
|
| 31 |
try: |
| 32 |
import polib |
| 33 |
except ImportError: |
| 34 |
print("You need to install the python-polib package to read translations") |
| 35 |
raise |
| 36 |
|
| 37 |
|
| 38 |
import pkgutil |
| 39 |
import importlib |
| 40 |
|
| 41 |
_tests = [] |
| 42 |
for finder, mod_name, _ispkg in pkgutil.iter_modules(__path__): |
| 43 |
|
| 44 |
if mod_name == "__main__": |
| 45 |
continue |
| 46 |
|
| 47 |
|
| 48 |
full_name = "{}.{}".format(__name__, mod_name) |
| 49 |
module = importlib.import_module(full_name) |
| 50 |
|
| 51 |
|
| 52 |
for attrname, attr in module.__dict__.items(): |
| 53 |
if attrname.startswith('test_') and callable(attr): |
| 54 |
_tests.append(attr) |
| 55 |
|
| 56 |
def testString(poentry): |
| 57 |
"""Run all tests against the given translatable string. |
| 58 |
|
| 59 |
:param polib.POEntry poentry: The PO file entry to test |
| 60 |
:returns: whether the tests succeeded or not |
| 61 |
:rtype: bool |
| 62 |
""" |
| 63 |
success = True |
| 64 |
for test in _tests: |
| 65 |
try: |
| 66 |
test(poentry) |
| 67 |
except Exception as e: |
| 68 |
success = False |
| 69 |
print("%s failed on %s: %s" % (test.__name__, poentry.msgid, str(e))) |
| 70 |
|
| 71 |
return success |
| 72 |
|
| 73 |
def testPOT(potfile): |
| 74 |
"""Run all tests against all entries in a POT file. |
| 75 |
|
| 76 |
:param str potfile: The name of a .pot file to test |
| 77 |
:return: whether the checks succeeded or not |
| 78 |
:rtype: bool |
| 79 |
""" |
| 80 |
success = True |
| 81 |
|
| 82 |
parsed_pot = polib.pofile(potfile) |
| 83 |
|
| 84 |
for entry in parsed_pot: |
| 85 |
if not testString(entry): |
| 86 |
success = False |
| 87 |
|
| 88 |
return success |