Blame tests/locale_utils.py

Packit b040ce
Packit b040ce
import subprocess
Packit b040ce
Packit b040ce
"""Helper functions, decorators,... for working with locales"""
Packit b040ce
Packit b040ce
def get_avail_locales():
Packit b040ce
    return {loc.decode(errors="replace").strip() for loc in subprocess.check_output(["locale", "-a"]).split()}
Packit b040ce
Packit b040ce
def requires_locales(locales):
Packit b040ce
    """A decorator factory to skip tests that require unavailable locales
Packit b040ce
Packit b040ce
    :param set locales: set of required locales
Packit b040ce
Packit b040ce
    **Requires the test to have the set of available locales defined as its
Packit b040ce
    ``avail_locales`` attribute.**
Packit b040ce
Packit b040ce
    """
Packit b040ce
Packit b040ce
    canon_locales = {loc.replace("UTF-8", "utf8") for loc in locales}
Packit b040ce
    def decorator(test_method):
Packit b040ce
        def decorated(test, *args):
Packit b040ce
            missing = canon_locales - set(test.avail_locales)
Packit b040ce
            if missing:
Packit b040ce
                test.skipTest("requires missing locales: %s" % missing)
Packit b040ce
            else:
Packit b040ce
                return test_method(test, *args)
Packit b040ce
Packit b040ce
        return decorated
Packit b040ce
Packit b040ce
    return decorator