Skip to content

livery.extensions.pytest._slice

livery.extensions.pytest._slice

One shard's share of the test files: listed, partitioned, recorded.

A sharded check leg runs the test step on every shard, each over a share of the test files. share lists the test files of every suite the run reaches (test_files), reads the durations and the head of the leg's platform (livery.workshop.read_durations), packs the files into the shard's slices with livery.workshop.partition, the first slice starting at the head, takes this shard's slice, and leaves what it partitioned in the shard's record (livery.workshop.leave_shard) for the gate job's proof. The test step runs twice on a leg whose coverage entry tops up the suites no record can supply, so a second run on the same shard adds to the record instead of replacing it (SETS_FILE).

Functions:

Name Description
test_files

The test files of suite this run runs, root-relative with forward slashes.

share

This shard's share of suites' test files, per suite path, and its record.

Attributes:

Name Type Description
SETS_FILE

SETS_FILE module-attribute

SETS_FILE = '.workshop/pytest-shard.json'

test_files

test_files(ctx: PhaseContext, suite: Package) -> tuple[str, ...]

The test files of suite this run runs, root-relative with forward slashes.

The files the run selects for the suite (ctx.tests) when it names any; else every file pytest collects from the suite's test directory, test_*.py and *_test.py under it, the workspace's own tests directory included. Sorted. A suite without a test directory has none.

Source code in packages/extensions/pytest/src/livery/extensions/pytest/_slice.py
def test_files(ctx: PhaseContext, suite: Package) -> tuple[str, ...]:
    """The test files of *suite* this run runs, root-relative with forward slashes.

    The files the run selects for the suite (``ctx.tests``) when it
    names any; else every file pytest collects from the suite's test
    directory, ``test_*.py`` and ``*_test.py`` under it, the workspace's
    own ``tests`` directory included. Sorted. A suite without a test
    directory has none.
    """
    named = ctx.tests.get(suite.path)
    if named:
        return tuple(sorted(named))
    return _collected(suite)

share

share(ctx: PhaseContext, suites: tuple[Package, ...], *, step: str) -> dict[str, tuple[str, ...]]

This shard's share of suites' test files, per suite path, and its record.

Every test file the workspace holds, each package's and the workspace's own, is packed into the shard's count of slices by the durations of the leg's platform, read from the rows stamped before the checkout's committer time (livery.workshop.checkout_time), the first slice starting at the rows' head, the test seconds the steps only the first shard runs hold back, and of suites' test files (test_files) the ones in this shard's slice are returned, a suite's files sorted; a suite with no file in the slice maps to an empty tuple. The packing is over the whole workspace, never over the suites of one run, so a file has one shard however many runs of the step reach it on a leg: the gate's, and the coverage top-up's, which runs other suites and the workspace's own tests again. The slice, the digest of the set, the suites with a file in the set and the rows read are left in the shard's record. A suite with no test file is not named among the suites: it runs whole on the first shard, with every step that does not read the shard, and the coverage leg expects its parts there. Each line printed names step.

With no durations row before the bound, or a store that cannot be read, the reason prints, the first shard runs every file and every other shard runs none, and the record names no row, so the gate job's proof sees the leg ran unsharded. Outside CI no leg names a durations series, so every file weighs the same and the files are shared out by name.

Source code in packages/extensions/pytest/src/livery/extensions/pytest/_slice.py
def share(
    ctx: PhaseContext, suites: tuple[Package, ...], *, step: str
) -> dict[str, tuple[str, ...]]:
    """This shard's share of *suites*' test files, per suite path, and its record.

    Every test file the workspace holds, each package's and the
    workspace's own, is packed into the shard's count of slices by the
    durations of the leg's platform, read from the rows stamped before
    the checkout's committer time ([livery.workshop.checkout_time][]),
    the first slice starting at the rows' head, the test seconds the
    steps only the first shard runs hold back, and of *suites*' test
    files (`test_files`) the ones in this shard's
    slice are returned, a suite's files sorted; a suite with no file in
    the slice maps to an empty tuple. The packing is over the whole
    workspace, never over the suites of one run, so a file has one
    shard however many runs of the step reach it on a leg: the gate's,
    and the coverage top-up's, which runs other suites and the
    workspace's own tests again. The slice, the digest of the set, the
    suites with a file in the set and the rows read are left in the
    shard's record. A suite with no test file is not named among the
    suites: it runs whole on the first shard, with every step that does
    not read the shard, and the coverage leg expects its parts there.
    Each line printed names *step*.

    With no durations row before the bound, or a store that cannot be
    read, the reason prints, the first shard runs every file and every
    other shard runs none, and the record names no row, so the gate
    job's proof sees the leg ran unsharded. Outside CI no leg names a
    durations series, so every file weighs the same and the files are
    shared out by name.
    """
    listed = {suite.path: test_files(ctx, suite) for suite in suites}
    every = tuple(name for names in listed.values() for name in names)
    named = tuple(path for path, names in listed.items() if names)
    before = checkout_time(ctx.root)
    run = ci_run()
    leg = run.leg if run is not None else ""
    durations: dict[str, float] = {}
    head = 0.0
    rows: tuple[str, ...] = ()
    if not leg:
        print(
            f"  {step}: no leg names a durations series here; every test file"
            " weighs the same"
        )
    else:
        durations, head, rows, why = read_durations(ctx.root, leg, before=before)
        if why:
            print(
                f"  {step}: the durations of {platform_leg(leg)} could not be read"
                f" ({why}); the first shard runs every test file"
            )
        elif not rows:
            print(
                f"  {step}: no durations row of {platform_leg(leg)} before"
                f" {before.isoformat(timespec='seconds')}; the first shard runs"
                " every test file"
            )
    if leg and not rows:
        mine = every if ctx.shard.first else ()
    else:
        slices = partition(
            _workspace_files(ctx, every), durations, ctx.shard.count, head=head
        )
        ours = set(every)
        mine = tuple(name for name in slices[ctx.shard.index - 1] if name in ours)
    _record(
        ctx.root,
        shard=ctx.shard,
        every=every,
        mine=mine,
        suites=named,
        rows=rows,
        before=before,
        attempt=run.attempt if run is not None else "",
    )
    print(
        f"  {step}: shard {ctx.shard} runs {len(mine) or 'none'} of {len(every)}"
        " test file(s)"
    )
    held = set(mine)
    return {
        path: tuple(name for name in names if name in held)
        for path, names in listed.items()
    }