test_nested_tree_cache.py

tests/test_nested_tree_cache.py

gh#24 — the nested-tree walk ran repeatedly inside one build.

test_nesting_the_cache_is_a_no_op_and_the_outermost_owns_it

None test_nested_tree_cache.test_nesting_the_cache_is_a_no_op_and_the_outermost_owns_it(Path tmp_path)

`build_index` opens one, and a caller that already opened its own must not have the inner exit clear it out from under the rest of the build. The outermost owns the lifetime; an inner activation is inert.

tests/test_nested_tree_cache.py:92

Checks

  • assert scope._NESTED_CACHE is not None, "the inner exit cleared the outer cache"
  • assert scope._NESTED_CACHE is None, "the outermost exit must clear it"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_nesting_the_cache_is_a_no_op_and_the_outermost_owns_it → nested_repo_roots
Source · 11 lines
def test_nesting_the_cache_is_a_no_op_and_the_outermost_owns_it(tmp_path: Path) -> None:
    """`build_index` opens one, and a caller that already opened its own must not have the
    inner exit clear it out from under the rest of the build. The outermost owns the
    lifetime; an inner activation is inert."""
    root = _nested(tmp_path)
    with scope.nested_tree_cache():
        scope.nested_repo_roots(root)
        with scope.nested_tree_cache():
            scope.nested_repo_roots(root)
        assert scope._NESTED_CACHE is not None, "the inner exit cleared the outer cache"
    assert scope._NESTED_CACHE is None, "the outermost exit must clear it"

1 more lines, not shown

Activity · 7 steps
lines 96–96root = _nested(tmp_path)lines 97–97with scope.nested_tree_cache()lines 98–98scope.nested_repo_roots(root)lines 99–99with scope.nested_tree_cache()lines 100–100scope.nested_repo_roots(root)lines 101–101assert scope._NESTED_CACHE is not None, "the inner exit cleared the outer cache"lines 102–102assert scope._NESTED_CACHE is None, "the outermost exit must clear it"

test_outside_the_cache_every_call_walks

None test_nested_tree_cache.test_outside_the_cache_every_call_walks(Path tmp_path, monkeypatch)

THE STALENESS GUARD, asserted rather than assumed. The cache must not outlive the build that opened it: `nested_repo_roots` answers filesystem state, and a submodule added while a long-lived server runs would otherwise serve a stale SCOPE — which decides what is indexed at all, so the cost of being wrong is not a slow build but a wrong one.

tests/test_nested_tree_cache.py:58

Show on the map →

Called by none

Calls _nested, nested_repo_roots

Checks

  • assert len(calls) == 2, "outside a build the walk must not be memoised"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_outside_the_cache_every_call_walks → nested_repo_roots
Source · 17 lines
def test_outside_the_cache_every_call_walks(tmp_path: Path, monkeypatch) -> None:
    """THE STALENESS GUARD, asserted rather than assumed. The cache must not outlive the
    build that opened it: `nested_repo_roots` answers filesystem state, and a submodule
    added while a long-lived server runs would otherwise serve a stale SCOPE — which
    decides what is indexed at all, so the cost of being wrong is not a slow build but a
    wrong one.
    """
    root = _nested(tmp_path)
    calls: list[Path] = []
    real = scope._nested_repos_under
    monkeypatch.setattr(scope, "_nested_repos_under", lambda r: calls.append(r) or real(r))

    scope.nested_repo_roots(root)
    scope.nested_repo_roots(root)
    assert len(calls) == 2, "outside a build the walk must not be memoised"

Activity · 7 steps
lines 65–65root = _nested(tmp_path)lines 66–66calls: list[Path] = []lines 67–67real = scope._nested_repos_underlines 68–68monkeypatch.setattr(scope, "_nested_repos_under", lambda r: calls.append(r) or real(r))lines 70–70scope.nested_repo_roots(root)lines 71–71scope.nested_repo_roots(root)lines 72–72assert len(calls) == 2, "outside a build the walk must not be memoised"

test_the_cache_sees_a_tree_added_between_builds

None test_nested_tree_cache.test_the_cache_sees_a_tree_added_between_builds(Path tmp_path)

The behavioural form of the guard above: two builds, a nested tree appearing between them, and the second build must see it. A process-lifetime cache passes the call-count test and fails this one.

tests/test_nested_tree_cache.py:75

Checks

  • assert {p.name for p in before} == {"dep"}
  • assert {p.name for p in after} == {"dep", "extra"}, ("the second build did not see a tree added after the first — the cache outlived its build" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_the_cache_sees_a_tree_added_between_builds → nested_repo_roots
Source · 17 lines
def test_the_cache_sees_a_tree_added_between_builds(tmp_path: Path) -> None:
    """The behavioural form of the guard above: two builds, a nested tree appearing
    between them, and the second build must see it. A process-lifetime cache passes the
    call-count test and fails this one."""
    root = _nested(tmp_path)
    with scope.nested_tree_cache():
        before = scope.nested_repo_roots(root)
    (root / "vendor" / "extra" / ".git").mkdir(parents=True)
    with scope.nested_tree_cache():
        after = scope.nested_repo_roots(root)

    assert {p.name for p in before} == {"dep"}
    assert {p.name for p in after} == {"dep", "extra"}, (
        "the second build did not see a tree added after the first — the cache outlived its build"
    )

Activity · 8 steps
lines 79–79root = _nested(tmp_path)lines 80–80with scope.nested_tree_cache()lines 81–81before = scope.nested_repo_roots(root)lines 82–82(root / "vendor" / "extra" / ".git").mkdir(parents=True)lines 83–83with scope.nested_tree_cache()lines 84–84after = scope.nested_repo_roots(root)lines 86–86assert {p.name for p in before} == {"dep"}lines 87–89assert {p.name for p in after} == {"dep", "extra"}, ( "the second build did not see a tree added after the first — the cache outlived its build" )

test_the_walk_runs_once_per_root_inside_the_cache

None test_nested_tree_cache.test_the_walk_runs_once_per_root_inside_the_cache(Path tmp_path, monkeypatch)

Four callers, one walk. The cache is keyed by RESOLVED root, so two callers naming the same tree differently still share the answer.

tests/test_nested_tree_cache.py:41

Checks

  • assert len(calls) == 1, f"the walk ran {len(calls)} times inside one build"
  • assert [p.name for p in first] == ["dep"]

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_the_walk_runs_once_per_root_inside_the_cache → nested_repo_roots
Source · 17 lines
def test_the_walk_runs_once_per_root_inside_the_cache(tmp_path: Path, monkeypatch) -> None:
    """Four callers, one walk. The cache is keyed by RESOLVED root, so two callers naming
    the same tree differently still share the answer."""
    root = _nested(tmp_path)
    calls: list[Path] = []
    real = scope._nested_repos_under
    monkeypatch.setattr(scope, "_nested_repos_under", lambda r: calls.append(r) or real(r))

    with scope.nested_tree_cache():
        first = scope.nested_repo_roots(root)
        scope.nested_repo_roots(root)
        scope.nested_repo_roots(Path(str(root) + "/."))

    assert len(calls) == 1, f"the walk ran {len(calls)} times inside one build"
    assert [p.name for p in first] == ["dep"]

Activity · 10 steps
lines 44–44root = _nested(tmp_path)lines 45–45calls: list[Path] = []lines 46–46real = scope._nested_repos_underlines 47–47monkeypatch.setattr(scope, "_nested_repos_under", lambda r: calls.append(r) or real(r))lines 49–49with scope.nested_tree_cache()lines 50–50first = scope.nested_repo_roots(root)lines 51–51scope.nested_repo_roots(root)lines 52–52scope.nested_repo_roots(Path(str(root) + "/."))lines 54–54assert len(calls) == 1, f"the walk ran {len(calls)} times inside one build"lines 55–55assert [p.name for p in first] == ["dep"]