Skip to content

livery.extensions.pytest._guard

livery.extensions.pytest._guard

Refuse direct pytest runs outside the workshop's runner or configured editor.

Registered through livery-extensions-pytest's pytest11 entry point, so every pytest this venv starts carries it. A run passes the guard when --as-livery is on the command line (passed by the test and examples steps and by the VS Code settings fm sync renders) or when LIVERY_PYTEST is set in the environment (inherited by xdist workers and nested test sessions once an outer run is admitted, and set by isolated release verification runs). Without either, pytest refuses before collection with the commands that wrap it; --help still prints usage.

Functions:

Name Description
pytest_addoption

Add --as-livery, admitting a run started by the workshop or editor.

pytest_configure

Refuse a bare pytest run; export the variable once admitted.

Attributes:

Name Type Description
FLAG
VARIABLE

FLAG module-attribute

FLAG = '--as-livery'

VARIABLE module-attribute

VARIABLE = 'LIVERY_PYTEST'

pytest_addoption

pytest_addoption(parser: Parser) -> None

Add --as-livery, admitting a run started by the workshop or editor.

Source code in packages/extensions/pytest/src/livery/extensions/pytest/_guard.py
def pytest_addoption(parser: pytest.Parser) -> None:
    """Add ``--as-livery``, admitting a run started by the workshop or editor."""
    parser.addoption(
        FLAG,
        action="store_true",
        default=False,
        help="run pytest under the workshop's guard; fm test and VS Code pass it",
    )

pytest_configure

pytest_configure(config: Config) -> None

Refuse a bare pytest run; export the variable once admitted.

Source code in packages/extensions/pytest/src/livery/extensions/pytest/_guard.py
def pytest_configure(config: pytest.Config) -> None:
    """Refuse a bare pytest run; export the variable once admitted."""
    if config.getoption("help", default=False):
        return
    if config.getoption(FLAG, default=False) or os.environ.get(VARIABLE):
        os.environ[VARIABLE] = "1"
        return
    raise pytest.UsageError(
        "pytest must run through `fm test` or `fm check`, never directly. "
        f"Pass `{FLAG}` only from an editor integration configured by `fm sync` "
        f"(or set {VARIABLE}=1 in a harness that wraps pytest)."
    )