Skip to content

livery.workshop._pytest_common

livery.workshop._pytest_common

Pure-stdlib helpers shared by the workshop's pytest11 plugins.

Every pytest session in an environment with livery-workshop installed loads its pytest11 entry points before any test runs. Keeping these constants and path helpers in a leaf module with no workshop imports keeps the workshop's module graph out of every test session's startup and out of every test file's recorded reads.

Functions:

Name Description
is_group

Whether directory under packages/ groups packages rather than being one.

package_directories

List every directory under packages/ that should hold a package.

package_of

Name the workspace package a test belongs to, from its pytest node id.

Attributes:

Name Type Description
PACKAGES_DIR
LEG_VARIABLE
POINT_VARIABLE
SPEED_FILE_VARIABLE
PACKAGE_TREES

PACKAGES_DIR module-attribute

PACKAGES_DIR = 'packages'

LEG_VARIABLE module-attribute

LEG_VARIABLE = 'WORKSHOP_LEG'

POINT_VARIABLE module-attribute

POINT_VARIABLE = 'WORKSHOP_POINT'

SPEED_FILE_VARIABLE module-attribute

SPEED_FILE_VARIABLE = 'WORKSHOP_SPEED_FILE'

PACKAGE_TREES module-attribute

PACKAGE_TREES = frozenset({'tests', 'src', 'docs'})

is_group

is_group(directory: Path) -> bool

Whether directory under packages/ groups packages rather than being one.

A group has no workshop.toml of its own and at least one subdirectory that has one. Groups do not nest.

Source code in packages/workshop/src/livery/workshop/_pytest_common.py
def is_group(directory: Path) -> bool:
    """Whether *directory* under ``packages/`` groups packages rather than being one.

    A group has no ``workshop.toml`` of its own and at least one
    subdirectory that has one. Groups do not nest.
    """
    if (directory / "workshop.toml").is_file():
        return False
    return any((child / "workshop.toml").is_file() for child in directory.iterdir())

package_directories

package_directories(root: Path) -> tuple[Path, ...]

List every directory under packages/ that should hold a package.

A package lives at packages/<name>/ or, inside a group directory, at packages/<group>/<name>/. A group is a directory with no workshop.toml of its own and at least one subdirectory that has one. The directories are listed whether or not they carry a contract, and nothing in them is judged; livery.workshop.discover_packages reads and judges the contracts. Reach for this where a glob would assume one level.

Parameters:

Name Type Description Default
root Path

The workspace root.

required

Returns:

Type Description
Path

The directories, sorted by path; empty when root has no

...

packages/ directory.

Source code in packages/workshop/src/livery/workshop/_pytest_common.py
def package_directories(root: Path) -> tuple[Path, ...]:
    """List every directory under ``packages/`` that should hold a package.

    A package lives at ``packages/<name>/`` or, inside a group
    directory, at ``packages/<group>/<name>/``. A group is a directory
    with no ``workshop.toml`` of its own and at least one subdirectory
    that has one. The directories are listed whether or not they carry
    a contract, and nothing in them is judged;
    [livery.workshop.discover_packages][] reads and judges the
    contracts. Reach for this where a glob would assume one level.

    Args:
        root: The workspace root.

    Returns:
        The directories, sorted by path; empty when *root* has no
        ``packages/`` directory.
    """
    packages_dir = root / PACKAGES_DIR
    if not packages_dir.is_dir():
        return ()
    found: list[Path] = []
    for directory in (p for p in packages_dir.iterdir() if p.is_dir()):
        if is_group(directory):
            found.extend(p for p in directory.iterdir() if p.is_dir())
        else:
            found.append(directory)
    return tuple(sorted(found, key=lambda path: path.as_posix()))

package_of

package_of(nodeid: str) -> str

Name the workspace package a test belongs to, from its pytest node id.

The package is the path before the first directory a package keeps its tests or sources in, tests, src or docs: packages/forge/tests/test_x.py::test_y belongs to packages/forge. A package in a group directory is one level deeper, and its own name may be one of those three: in packages/extensions/docs/tests/test_x.py::test_y the tests beneath docs says docs is the package, so the answer is packages/extensions/docs. Nothing is read from the disk.

Parameters:

Name Type Description Default
nodeid str

A test's node id, relative to the workspace root.

required

Returns:

Type Description
str

The package's path relative to the workspace root, or "" for

str

a node outside packages/, such as a workspace test under

str

tests/.

Source code in packages/workshop/src/livery/workshop/_pytest_common.py
def package_of(nodeid: str) -> str:
    """Name the workspace package a test belongs to, from its pytest node id.

    The package is the path before the first directory a package keeps
    its tests or sources in, ``tests``, ``src`` or ``docs``:
    ``packages/forge/tests/test_x.py::test_y`` belongs to
    ``packages/forge``. A package in a group directory is one level
    deeper, and its own name may be one of those three: in
    ``packages/extensions/docs/tests/test_x.py::test_y`` the ``tests``
    beneath ``docs`` says ``docs`` is the package, so the answer is
    ``packages/extensions/docs``. Nothing is read from the disk.

    Args:
        nodeid: A test's node id, relative to the workspace root.

    Returns:
        The package's path relative to the workspace root, or ``""`` for
        a node outside ``packages/``, such as a workspace test under
        ``tests/``.
    """
    parts = nodeid.split("::", 1)[0].split("/")
    if len(parts) < 3 or parts[0] != "packages":
        return ""
    for index in range(2, len(parts) - 1):
        if parts[index] not in PACKAGE_TREES:
            continue
        if index == 2 and parts[3] in PACKAGE_TREES and len(parts) > 4:
            continue
        return "/".join(parts[:index])
    return "/".join(parts[:2])