testsAndMisc/python_pkg/phone_lease/tests/test_optional_credit.py
Krzysztof kuhy Rudnicki aae7dc57f6
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (mypy, mypy) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (pylint, pylint) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (rest, ) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (shellcheck, shellcheck) (push) Waiting to run
Python tests / test (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / lib-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
phone_lease, android_ui: credit is optional
phone_deploy.sh runs python_pkg.phone_lease with whatever python3 is
first on PATH. e9ba82ed made it import kuhy_credit at module load, so
from a venv without kuhy-credit every phone deploy died with
ModuleNotFoundError. Both phone CLIs now take the epilog from
help_epilog(), which returns None when the package is missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 15:13:14 +01:00

54 lines
1.7 KiB
Python

"""Phone tooling must run when kuhy_credit is not importable.
phone_deploy.sh and the android_ui driver start these CLIs with whatever
``python3`` is first on PATH (often a project venv without kuhy-credit).
The credit is decoration; a missing package must never block a phone lease.
"""
from __future__ import annotations
import subprocess
import sys
from kuhy_credit import credit_line
import pytest
from python_pkg._optional_credit import help_epilog
# Blocks kuhy_credit (sys.modules[name] = None makes import raise
# ImportError), then runs the module as ``python -m <module> --help`` would.
_RUN_WITHOUT_CREDIT = (
"import runpy, sys; sys.modules['kuhy_credit'] = None; "
"sys.argv = [sys.argv[1], '--help']; "
"runpy.run_module(sys.argv[0], run_name='__main__', alter_sys=True)"
)
PHONE_CLIS = ("python_pkg.phone_lease", "python_pkg.android_ui")
@pytest.mark.parametrize("module", PHONE_CLIS)
def test_help_works_without_kuhy_credit(module: str) -> None:
"""``--help`` exits 0 even when kuhy_credit cannot be imported."""
result = subprocess.run(
[sys.executable, "-c", _RUN_WITHOUT_CREDIT, module],
capture_output=True,
text=True,
timeout=30,
check=False,
)
assert result.returncode == 0, result.stderr
assert "usage" in result.stdout.lower()
def test_help_epilog_none_without_kuhy_credit(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""help_epilog() returns None when kuhy_credit cannot be imported."""
monkeypatch.setitem(sys.modules, "kuhy_credit", None)
assert help_epilog() is None
def test_help_epilog_is_credit_line() -> None:
"""help_epilog() returns the shared credit line when it is installed."""
assert help_epilog() == credit_line()