From 6b950126984ffea1cb83ce320d954034341ac67b Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Wed, 24 Jan 2018 11:10:48 +0100 Subject: [PATCH 1/3] Adding translate test --- tests/test_translate.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/test_translate.py diff --git a/tests/test_translate.py b/tests/test_translate.py new file mode 100644 index 000000000..cd71643fb --- /dev/null +++ b/tests/test_translate.py @@ -0,0 +1,28 @@ +# Setting French as system language +import os +os.environ['LC_ALL'] = 'fr_FR.utf-8' + +import pytest +import sys + +from plumbum import cli + +class Simple(cli.Application): + foo = cli.SwitchAttr("--foo") + + def main(self): + pass + +class TestFRCLI: + def test_nolang_switches(self): + _, rc = Simple.run(["foo", "-h"], exit = False) + assert rc == 0 + _, rc = Simple.run(["foo", "--version"], exit = False) + assert rc == 0 + + def test_help_lang(self, capsys): + _, rc = Simple.run(["foo", "-h"], exit = False) + assert rc == 0 + stdout, stderr = capsys.readouterr() + assert "Utilisation" in stdout + assert "Imprime ce message d'aide et sort" in stdout From b513be04138d2f0b767905f36a79c8c52d8813f0 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sat, 5 Oct 2024 00:19:23 -0400 Subject: [PATCH 2/3] fix: fix tests Signed-off-by: Henry Schreiner --- .github/workflows/ci.yml | 4 +++ plumbum/cli/i18n.py | 15 +++++---- tests/test_translate.py | 69 ++++++++++++++++++++++++++++------------ 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fff293f20..e91a0e690 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,6 +59,10 @@ jobs: python-version: ${{ matrix.python-version }} allow-prereleases: true + - name: Add locale for locale test + if: runner.os == 'Linux' + run: sudo locale-gen fr_FR.UTF-8 + - name: Setup uv (cached) uses: hynek/setup-cached-uv@v2 diff --git a/plumbum/cli/i18n.py b/plumbum/cli/i18n.py index 1921fe5ba..57294f5f0 100644 --- a/plumbum/cli/i18n.py +++ b/plumbum/cli/i18n.py @@ -1,6 +1,9 @@ from __future__ import annotations import locale +import os + +DIR = os.path.abspath(os.path.dirname(__file__)) # High performance method for English (no translation needed) loc = locale.getlocale()[0] @@ -23,7 +26,6 @@ def get_translation_for( else: import gettext - import os # If not installed with setuptools, this might not be available try: @@ -31,25 +33,26 @@ def get_translation_for( except ImportError: pkg_resources = None # type: ignore[assignment] - local_dir = os.path.basename(__file__) - def get_translation_for(package_name: str) -> gettext.NullTranslations: # type: ignore[misc] """Find and return gettext translation for package (Try to find folder manually if setuptools does not exist) """ + assert loc is not None if "." in package_name: package_name = ".".join(package_name.split(".")[:-1]) localedir = None if pkg_resources is None: - mydir = os.path.join(local_dir, "i18n") + mydir = os.path.join(DIR, "i18n") else: mydir = pkg_resources.resource_filename(package_name, "i18n") for localedir in mydir, None: - localefile = gettext.find(package_name, localedir) + localefile = gettext.find(package_name, localedir, languages=[loc]) if localefile: break - return gettext.translation(package_name, localedir=localedir, fallback=True) + return gettext.translation( + package_name, localedir=localedir, languages=[loc], fallback=True + ) diff --git a/tests/test_translate.py b/tests/test_translate.py index cd71643fb..18541b719 100644 --- a/tests/test_translate.py +++ b/tests/test_translate.py @@ -1,28 +1,57 @@ # Setting French as system language -import os -os.environ['LC_ALL'] = 'fr_FR.utf-8' +from __future__ import annotations + +import importlib +import locale import pytest -import sys -from plumbum import cli +import plumbum.cli +import plumbum.cli.i18n + + +def reload_cli(): + importlib.reload(plumbum.cli.i18n) + importlib.reload(plumbum.cli.switches) + importlib.reload(plumbum.cli.application) + importlib.reload(plumbum.cli) + + +@pytest.fixture() +def french(): + try: + locale.setlocale(locale.LC_ALL, "fr_FR.utf-8") + reload_cli() + yield + finally: + locale.setlocale(locale.LC_ALL, "") + reload_cli() + + +@pytest.mark.usefixtures("french") +def test_nolang_switches(): + class Simple(plumbum.cli.Application): + foo = plumbum.cli.SwitchAttr("--foo") + + def main(self): + pass + + _, rc = Simple.run(["foo", "-h"], exit=False) + assert rc == 0 + _, rc = Simple.run(["foo", "--version"], exit=False) + assert rc == 0 -class Simple(cli.Application): - foo = cli.SwitchAttr("--foo") - def main(self): - pass +@pytest.mark.usefixtures("french") +def test_help_lang(capsys): + class Simple(plumbum.cli.Application): + foo = plumbum.cli.SwitchAttr("--foo") -class TestFRCLI: - def test_nolang_switches(self): - _, rc = Simple.run(["foo", "-h"], exit = False) - assert rc == 0 - _, rc = Simple.run(["foo", "--version"], exit = False) - assert rc == 0 + def main(self): + pass - def test_help_lang(self, capsys): - _, rc = Simple.run(["foo", "-h"], exit = False) - assert rc == 0 - stdout, stderr = capsys.readouterr() - assert "Utilisation" in stdout - assert "Imprime ce message d'aide et sort" in stdout + _, rc = Simple.run(["foo", "-h"], exit=False) + assert rc == 0 + stdout, stderr = capsys.readouterr() + assert "Utilisation" in stdout + assert "Imprime ce message d'aide et sort" in stdout From f7f15be02d067ff421817ae194a287e8e12d5d9e Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sat, 5 Oct 2024 00:45:01 -0400 Subject: [PATCH 3/3] tests: skip if locale not available Signed-off-by: Henry Schreiner --- .github/workflows/ci.yml | 2 +- tests/test_translate.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e91a0e690..ce1c043e0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -60,7 +60,7 @@ jobs: allow-prereleases: true - name: Add locale for locale test - if: runner.os == 'Linux' + if: runner.os == 'Linux' && matrix.python-version != '3.10' run: sudo locale-gen fr_FR.UTF-8 - name: Setup uv (cached) diff --git a/tests/test_translate.py b/tests/test_translate.py index 18541b719..305cba3ab 100644 --- a/tests/test_translate.py +++ b/tests/test_translate.py @@ -23,6 +23,10 @@ def french(): locale.setlocale(locale.LC_ALL, "fr_FR.utf-8") reload_cli() yield + except locale.Error: + pytest.skip( + "No fr_FR locale found, run 'sudo locale-gen fr_FR.UTF-8' to run this test" + ) finally: locale.setlocale(locale.LC_ALL, "") reload_cli()