Skip to content

Content hashes and the pass cache

A test's pass is cached under its own content hash and the content hash of the code it tests; neither includes the other.

cache

Content hashes of tests and of the code they test, and the pass cache.

The code a test checks is identified by its content: code_digest is the module digest of module_hashes over the checked module's own declarations, which is invariant under renaming, reordering, and reformatting, and never includes a test (tests are not part of Module.statements, and the computations the runner adds to type a property's variables are left out). A test is identified by its own content: declaration_digest hashes a test declaration's structure, its source positions excluded, under the same digest (tagged_digest). Editing a test thus leaves the code's hash unchanged, and editing the code leaves every test's hash unchanged.

ResultCache remembers each pass under the pair of hashes and the test's settings: a test whose content, code, and settings are unchanged since it last passed is not run again. A failure is never cached.

RUNNER_PREFIX module-attribute

RUNNER_PREFIX = '__test_binders_'

The name prefix of the computations the runner adds to a checked module to type a property's variables.

CACHE_REVISION module-attribute

CACHE_REVISION = '1'

The revision of the runner's semantics; a cached pass from another is not trusted.

ResultCache

ResultCache(root: Path | None = None)

Passes remembered by content hash, one small file per pass.

PARAMETER DESCRIPTION
root

The directory; $XDG_CACHE_HOME/quivers/tests (or ~/.cache/quivers/tests) when None.

TYPE: Path | None DEFAULT: None

Source code in src/quivers/testing/cache.py
def __init__(self, root: Path | None = None) -> None:
    base = Path(os.environ.get("XDG_CACHE_HOME") or Path.home() / ".cache")
    self.root = root if root is not None else base / "quivers" / "tests"

passed

passed(test: str, module: str, settings: str) -> bool

Whether this test passed against this code with these settings.

PARAMETER DESCRIPTION
test

The test's hash.

TYPE: str

module

The code's hash.

TYPE: str

settings

The test's settings, serialized.

TYPE: str

RETURNS DESCRIPTION
bool

Whether a pass is recorded.

Source code in src/quivers/testing/cache.py
def passed(self, test: str, module: str, settings: str) -> bool:
    """Whether this test passed against this code with these settings.

    Parameters
    ----------
    test : str
        The test's hash.
    module : str
        The code's hash.
    settings : str
        The test's settings, serialized.

    Returns
    -------
    bool
        Whether a pass is recorded.
    """
    return self._path(test, module, settings).is_file()

record

record(test: str, module: str, settings: str) -> None

Remember a pass.

PARAMETER DESCRIPTION
test

The test's hash.

TYPE: str

module

The code's hash.

TYPE: str

settings

The test's settings, serialized.

TYPE: str

Source code in src/quivers/testing/cache.py
def record(self, test: str, module: str, settings: str) -> None:
    """Remember a pass.

    Parameters
    ----------
    test : str
        The test's hash.
    module : str
        The code's hash.
    settings : str
        The test's settings, serialized.
    """
    path = self._path(test, module, settings)
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(f"{test}\n{module}\n")

declaration_digest

declaration_digest(test: TestDecl) -> str

The content hash of one test declaration.

PARAMETER DESCRIPTION
test

The declaration.

TYPE: TestDecl

RETURNS DESCRIPTION
str

The digest of its kind, title, body, variables, options, subject and doc comment, its source positions excluded.

Source code in src/quivers/testing/cache.py
def declaration_digest(test: TestDecl) -> str:
    """The content hash of one test declaration.

    Parameters
    ----------
    test : TestDecl
        The declaration.

    Returns
    -------
    str
        The digest of its kind, title, body, variables, options, subject and
        doc comment, its source positions excluded.
    """
    return tagged_digest("test", _strip(json.loads(test.model_dump_json())))

code_digest

code_digest(module: QiecModule) -> str

The content hash of a checked module's code.

PARAMETER DESCRIPTION
module

The checked module, possibly with the runner's binder computations.

TYPE: QiecModule

RETURNS DESCRIPTION
str

The digest of the multiset of its declarations' kinds and content hashes, the runner's computations left out.

Source code in src/quivers/testing/cache.py
def code_digest(module: QiecModule) -> str:
    """The content hash of a checked module's code.

    Parameters
    ----------
    module : QiecModule
        The checked module, possibly with the runner's binder computations.

    Returns
    -------
    str
        The digest of the multiset of its declarations' kinds and content
        hashes, the runner's computations left out.
    """
    hashes = module_hashes(module)
    form: list[CoreJson] = [
        [identity.kind, identity.hash]
        for identity in sorted(
            hashes.declarations, key=lambda identity: (identity.kind, identity.hash)
        )
        if not identity.name.startswith(RUNNER_PREFIX)
    ]
    return tagged_digest("module", form)