Skip to content

Commit

Permalink
fix: fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Oct 5, 2024
1 parent 6b95012 commit 3b18bbe
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 26 deletions.
15 changes: 9 additions & 6 deletions plumbum/cli/i18n.py
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -23,33 +26,33 @@ def get_translation_for(

else:
import gettext
import os

# If not installed with setuptools, this might not be available
try:
import pkg_resources
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
)
69 changes: 49 additions & 20 deletions tests/test_translate.py
Original file line number Diff line number Diff line change
@@ -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")
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

0 comments on commit 3b18bbe

Please sign in to comment.