Skip to content

livery.extensions.pytest._coverage

livery.extensions.pytest._coverage

Reduce coverage.py's measurements to the line format the coverage extension reads.

While coverage is listed, the coverage extension asks its coverage.measurers query which extension reduces each measurer's data, and this extension answers for arcs, coverage.py's arcs, in its [for.coverage] table (measurers). Arcs turns the data a check leg's metered run left into one part per suite the leg ran, each suite's arcs within its closure as its own tests and the code outside every test reached them; and on a machine, the data its last run left into one part of the whole run. A part is in the line format, version 3: per file, coverage.py's own analysis of it under the workspace's .coveragerc. It holds the statements and the ones that ran, the excluded lines, the statements written over several lines, each branch of a line with more than one exit with the sentence that says why it would not run, the functions and classes, and the tests that reached each line wherever the data names a test: on a check leg a suite's own tests and the code outside every test, on a machine every context its run recorded. coverage.py reads no columns, so a part holds no ranges. A branch the source waives with # pragma: no branch counts as taken, as coverage.py counts it, and is tagged waived.

coverage.py's parser reads each file once, however many suites name it, and says each branch's sentence once. Before a part is handed over, each file's numbers are checked against coverage.py's own analysis of the same data, and a file whose numbers differ refuses the reduction, naming the file.

Classes:

Name Description
Arcs

coverage.py's arcs, reduced to parts in the line format.

Functions:

Name Description
measurers

Answer coverage.measurers: this extension reduces coverage.py's arcs.

Attributes:

Name Type Description
MEASURER
FORMAT
DATA_FILE
CONFIG_FILE
METER
WAIVED
LEFT_UNIT

MEASURER module-attribute

MEASURER = 'arcs'

FORMAT module-attribute

FORMAT = 3

DATA_FILE module-attribute

DATA_FILE = '.coverage'

CONFIG_FILE module-attribute

CONFIG_FILE = '.coveragerc'

METER module-attribute

METER = 'COVERAGE_PROCESS_START'

WAIVED module-attribute

WAIVED = 'waived'

LEFT_UNIT module-attribute

LEFT_UNIT = '.'

Arcs

coverage.py's arcs, reduced to parts in the line format.

Methods:

Name Description
leg

One part per unit, in the units' order, from the data a check leg left.

left

The part of the data coverage.py left at root; none when it left none.

leg

leg(root: Path, units: Sequence[Mapping[str, Any]]) -> list[dict[str, Any]]

One part per unit, in the units' order, from the data a check leg left.

Each unit is {"unit": <its path>, "tests": <the prefix its own tests' contexts start with>, "within": [<the path prefixes of its closure>]}; a unit without within takes every file. The leg's data files combine in memory, and a unit's part holds the arcs of the files within its closure that its own tests reached, each test's context its node id, and that the code outside every test reached, import time's: those belong to every suite whose closure holds their file. A file the data names with no arc is in no unit's part. Where the leg recorded any test's context, each part names the contexts that reached each of its lines, the empty one for the code outside every test, numbered in sorted order; the record renumbers them when it stores the part.

Raises:

Type Description
ValueError

when the leg left no data, naming the variable that arms the meter; when a unit names no path; or when a file's numbers differ from coverage.py's, naming it.

Source code in packages/extensions/pytest/src/livery/extensions/pytest/_coverage.py
def leg(
    self, root: Path, units: Sequence[Mapping[str, Any]]
) -> list[dict[str, Any]]:
    """One part per unit, in the units' order, from the data a check leg left.

    Each unit is ``{"unit": <its path>, "tests": <the prefix its
    own tests' contexts start with>, "within": [<the path prefixes
    of its closure>]}``; a unit without ``within`` takes every
    file. The leg's data files combine in memory, and a unit's part
    holds the arcs of the files within its closure that its own
    tests reached, each test's context its node id, and that the
    code outside every test reached, import time's: those belong to
    every suite whose closure holds their file. A file the data
    names with no arc is in no unit's part. Where the leg recorded
    any test's context, each part names the contexts that reached
    each of its lines, the empty one for the code outside every
    test, numbered in sorted order; the record renumbers them when
    it stores the part.

    Raises:
        ValueError: when the leg left no data, naming the variable
            that arms the meter; when a unit names no path; or when a
            file's numbers differ from coverage.py's, naming it.
    """
    from coverage import CoverageData

    found = sorted(root.glob(f"{DATA_FILE}.*"))
    if not found:
        raise ValueError(
            f"the leg left no coverage.py data ({DATA_FILE}.*) at {root}:"
            f" inside CI the test runner arms {METER} in pytest's environment,"
            " so the tests and every process they start are metered; a leg"
            " that ran its suites and left no data ran no metered pytest"
        )
    combined = CoverageData(no_disk=True)
    reducer = _Reducer(root)
    parts: list[dict[str, Any]] = []
    try:
        for path in found:
            piece = CoverageData(basename=str(path))
            try:
                piece.read()
                combined.update(piece)
            finally:
                piece.close(force=True)
        named = bool(set(combined.measured_contexts()) - {""})
        for index, unit in enumerate(units):
            name, files, reached = _unit_arcs(
                combined, root, unit, index, contexts=named
            )
            data = CoverageData(no_disk=True)
            try:
                data.add_arcs(files)
                parts.append(
                    reducer.part(
                        name,
                        data,
                        {each: each for each in files},
                        reached if named else None,
                    )
                )
            finally:
                data.close(force=True)
    finally:
        combined.close(force=True)
    return parts

left

left(root: Path) -> list[dict[str, Any]]

The part of the data coverage.py left at root; none when it left none.

A file the data names outside root is no file of the workspace and is left out; one inside it that the checkout no longer has is in the part with an empty digest and nothing else, since its source cannot be read.

Raises:

Type Description
ValueError

when a file's numbers differ from coverage.py's, naming it.

Source code in packages/extensions/pytest/src/livery/extensions/pytest/_coverage.py
def left(self, root: Path) -> list[dict[str, Any]]:
    """The part of the data coverage.py left at *root*; none when it left none.

    A file the data names outside *root* is no file of the
    workspace and is left out; one inside it that the checkout no
    longer has is in the part with an empty digest and nothing
    else, since its source cannot be read.

    Raises:
        ValueError: when a file's numbers differ from coverage.py's,
            naming it.
    """
    from coverage import CoverageData

    if not (root / DATA_FILE).is_file():
        return []
    data = CoverageData(basename=str(root / DATA_FILE))
    try:
        data.read()
        names: dict[str, str] = {}
        for measured in data.measured_files():
            relative = _relative(measured, root)
            if relative is not None:
                names[relative] = measured
        reached = (
            {
                relative: data.contexts_by_lineno(measured)
                for relative, measured in names.items()
            }
            if set(data.measured_contexts()) - {""}
            else None
        )
        return [_Reducer(root).part(LEFT_UNIT, data, names, reached)]
    finally:
        data.close(force=True)

measurers

measurers(root: Path) -> dict[str, Arcs]

Answer coverage.measurers: this extension reduces coverage.py's arcs.

Source code in packages/extensions/pytest/src/livery/extensions/pytest/_coverage.py
def measurers(root: Path) -> dict[str, Arcs]:
    """Answer ``coverage.measurers``: this extension reduces coverage.py's arcs."""
    del root
    return {MEASURER: Arcs()}