Blame tests/locale_utils.py

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