test_indexcache_doxygen_cache.py

tests/test_indexcache_doxygen_cache.py

The `doxygen_cache` retrieval contract, asserted directly against `IndexCache` (#399).

test_a_cached_output_is_served_under_the_key_that_produced_it

None test_indexcache_doxygen_cache.test_a_cached_output_is_served_under_the_key_that_produced_it(Path tmp_path)

THE SUCCESS PATH. Nothing overwrote the file, so the skip must be taken — this is the whole value of the cache (#363: a cold doxygen run costs tens of seconds) and it is the half that a too-eager invalidation silently destroys.

tests/test_indexcache_doxygen_cache.py:56

Show on the map →

Called by none

Calls _write_output

Checks

  • assert cache.doxygen_get(KEY_A) == output, ("an untouched output was not served back to its own key — the cache now costs a " "full doxygen run on every build and no test of the failure path would notice" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measured
Source · 15 lines
def test_a_cached_output_is_served_under_the_key_that_produced_it(tmp_path: Path) -> None:
    """THE SUCCESS PATH. Nothing overwrote the file, so the skip must be taken — this is the
    whole value of the cache (#363: a cold doxygen run costs tens of seconds) and it is the
    half that a too-eager invalidation silently destroys.
    """
    output = _write_output(tmp_path / "out" / "doxygen_sqlite3.db", "WIDE build output")
    cache = IndexCache(tmp_path / "c.idxcache", tmp_path)
    cache.doxygen_put(KEY_A, output)

    assert cache.doxygen_get(KEY_A) == output, (
        "an untouched output was not served back to its own key — the cache now costs a "
        "full doxygen run on every build and no test of the failure path would notice"
    )

Activity · 4 steps
lines 61–61output = _write_output(tmp_path / "out" / "doxygen_sqlite3.db", "WIDE build output")lines 62–62cache = IndexCache(tmp_path / "c.idxcache", tmp_path)lines 63–63cache.doxygen_put(KEY_A, output)lines 65–68assert cache.doxygen_get(KEY_A) == output, ( "an untouched output was not served back to its own key — the cache now costs a " "full doxygen run on every build and no test of the failure path would notice" )

test_a_key_is_not_served_another_key_s_overwrite_of_the_same_path

None test_indexcache_doxygen_cache.test_a_key_is_not_served_another_key_s_overwrite_of_the_same_path(Path tmp_path)

tests/test_indexcache_doxygen_cache.py:71

Show on the map →

Called by none

Calls _write_output

Checks

  • assert cache.doxygen_get(KEY_A) == shared
  • assert cache.doxygen_get(KEY_A) is None, ("key A was served the output key B wrote over the shared path — the cache reported a " "hit for a configuration whose output no longer exists anywhere (#399)" )
  • assert cache.doxygen_get(KEY_B) == shared, ("the key that actually produced the current file must still hit; invalidating both " "would trade a wrong answer for a permanently cold cache" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measured
Source · 32 lines
def test_a_key_is_not_served_another_key_s_overwrite_of_the_same_path(tmp_path: Path) -> None:
    """#399 REPRODUCED AT UNIT SCALE, and the exact sequence measured on a real build: key A
    runs and records the shared path; key B runs, OVERWRITES that path, and records it too;
    key A is asked again.

    The old hit condition was "a row for this key exists AND the path exists", both of which
    are still true — so key A was served key B's output. On a real target this was seen in
    both directions, which is what makes it aliasing rather than staleness: a withdrawn
    `predefined` inherited the WIDER earlier output, a withdrawn `index_scope` the NARROWER
    one. Neither is "the newer output wins"; it is "whichever output was written last wins,
    whatever its key said".
    """
    shared = tmp_path / "out" / "doxygen_sqlite3.db"
    _write_output(shared, "NARROW build output")
    cache = IndexCache(tmp_path / "c.idxcache", tmp_path)
    cache.doxygen_put(KEY_A, shared)
    assert cache.doxygen_get(KEY_A) == shared

    ## Key B is the next build: same path, different content, its own row.
    _write_output(shared, "WIDE build output — a different configuration of the same repo")
    cache.doxygen_put(KEY_B, shared)

    assert cache.doxygen_get(KEY_A) is None, (
        "key A was served the output key B wrote over the shared path — the cache reported a "
        "hit for a configuration whose output no longer exists anywhere (#399)"
    )
    assert cache.doxygen_get(KEY_B) == shared, (
        "the key that actually produced the current file must still hit; invalidating both "
        "would trade a wrong answer for a permanently cold cache"
    )

Activity · 9 steps
lines 83–83shared = tmp_path / "out" / "doxygen_sqlite3.db"lines 84–84_write_output(shared, "NARROW build output")lines 85–85cache = IndexCache(tmp_path / "c.idxcache", tmp_path)lines 86–86cache.doxygen_put(KEY_A, shared)lines 87–87assert cache.doxygen_get(KEY_A) == sharedlines 90–90_write_output(shared, "WIDE build output — a different configuration of the same repo")lines 91–91cache.doxygen_put(KEY_B, shared)lines 93–96assert cache.doxygen_get(KEY_A) is None, ( "key A was served the output key B wrote over the shared path — the cache reported a " "hit for a configuration whose output no longer exists anywhere (#399)" )lines 97–100assert cache.doxygen_get(KEY_B) == shared, ( "the key that actually produced the current file must still hit; invalidating both " "would trade a wrong answer for a permanently cold cache" )

test_a_sidecar_predating_output_sha_is_rebuilt_rather_than_crashing

None test_indexcache_doxygen_cache.test_a_sidecar_predating_output_sha_is_rebuilt_rather_than_crashing(Path tmp_path)

THE UPGRADE PATH, which no real build produces and which therefore had no coverage.

tests/test_indexcache_doxygen_cache.py:119

Show on the map →

Called by none

Calls _write_output

Checks

  • assert "output_sha" in columns, "the old table shape survived, so every put will raise"
  • assert cache.doxygen_get(KEY_A) == output, ("an upgraded sidecar cannot store and serve an entry, so the rebuild left the table " "unusable rather than merely empty" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measured
Source · 34 lines
def test_a_sidecar_predating_output_sha_is_rebuilt_rather_than_crashing(tmp_path: Path) -> None:
    """THE UPGRADE PATH, which no real build produces and which therefore had no coverage.

    `CREATE TABLE IF NOT EXISTS` does nothing to a table that already exists, so a sidecar
    written before `output_sha` keeps its three-column shape and the first `doxygen_put`
    against it raises `no column named output_sha`. The build-version wipe does not help —
    it deletes ROWS, not columns. A crash here would land on every existing cache on disk,
    in the one component whose whole job is to be discardable.
    """
    cache_path = tmp_path / "old.idxcache"
    conn = sqlite3.connect(str(cache_path))
    conn.executescript(
        "CREATE TABLE doxygen_cache ("
        "  tree_sha TEXT PRIMARY KEY, db_path TEXT NOT NULL, created_at INTEGER NOT NULL);"
    )
    conn.execute(
        "INSERT INTO doxygen_cache(tree_sha, db_path, created_at) VALUES (?, ?, 0)",
        (KEY_A, str(tmp_path / "stale.db")),
    )
    conn.commit()
    conn.close()

    cache = IndexCache(cache_path, tmp_path)

    columns = {row[1] for row in cache.conn.execute("PRAGMA table_info(doxygen_cache)")}
    assert "output_sha" in columns, "the old table shape survived, so every put will raise"
    output = _write_output(tmp_path / "out" / "doxygen_sqlite3.db", "post-upgrade output")
    cache.doxygen_put(KEY_A, output)
    assert cache.doxygen_get(KEY_A) == output, (
        "an upgraded sidecar cannot store and serve an entry, so the rebuild left the table "
        "unusable rather than merely empty"
    )

Activity · 12 steps
lines 128–128cache_path = tmp_path / "old.idxcache"lines 129–129conn = sqlite3.connect(str(cache_path))lines 130–133conn.executescript( "CREATE TABLE doxygen_cache (" " tree_sha TEXT PRIMARY KEY, db_path TEXT NOT NULL, created_at INTEGER NOT NULL);" )lines 134–137conn.execute( "INSERT INTO doxygen_cache(tree_sha, db_path, created_at) VALUES (?, ?, 0)", (KEY_A, str(tmp_path / "stale.db")), )lines 138–138conn.commit()lines 139–139conn.close()lines 141–141cache = IndexCache(cache_path, tmp_path)lines 143–143columns = {row[1] for row in cache.conn.execute("PRAGMA table_info(doxygen_cache)")}lines 144–144assert "output_sha" in columns, "the old table shape survived, so every put will raise"lines 145–145output = _write_output(tmp_path / "out" / "doxygen_sqlite3.db", "post-upgrade output")lines 146–146cache.doxygen_put(KEY_A, output)lines 147–150assert cache.doxygen_get(KEY_A) == output, ( "an upgraded sidecar cannot store and serve an entry, so the rebuild left the table " "unusable rather than merely empty" )

test_an_output_that_cannot_be_hashed_is_not_cached_at_all

None test_indexcache_doxygen_cache.test_an_output_that_cannot_be_hashed_is_not_cached_at_all(Path tmp_path)

THE EMPTY-STRING WILDCARD, refused. `treescan.hash_file` returns "" for a file it cannot read, so recording that digest would make a later unreadable read compare `"" == ""` and HIT — a false hit manufactured out of two failures. The row is therefore not written, and the next build re-runs doxygen instead of trusting a file nobody hashed.

tests/test_indexcache_doxygen_cache.py:103

Show on the map →

Called by none

Calls none

Checks

  • assert rows == 0, "an unhashable output was recorded, so its digest is a wildcard"
  • assert cache.doxygen_get(KEY_A) is None

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measured
Source · 16 lines
def test_an_output_that_cannot_be_hashed_is_not_cached_at_all(tmp_path: Path) -> None:
    """THE EMPTY-STRING WILDCARD, refused. `treescan.hash_file` returns "" for a file it
    cannot read, so recording that digest would make a later unreadable read compare
    `"" == ""` and HIT — a false hit manufactured out of two failures. The row is therefore
    not written, and the next build re-runs doxygen instead of trusting a file nobody hashed.
    """
    cache = IndexCache(tmp_path / "c.idxcache", tmp_path)
    missing = tmp_path / "out" / "never_written.db"

    cache.doxygen_put(KEY_A, missing)

    rows = cache.conn.execute("SELECT COUNT(*) FROM doxygen_cache").fetchone()[0]
    assert rows == 0, "an unhashable output was recorded, so its digest is a wildcard"
    assert cache.doxygen_get(KEY_A) is None

Activity · 6 steps
lines 109–109cache = IndexCache(tmp_path / "c.idxcache", tmp_path)lines 110–110missing = tmp_path / "out" / "never_written.db"lines 112–112cache.doxygen_put(KEY_A, missing)lines 114–114rows = cache.conn.execute("SELECT COUNT(*) FROM doxygen_cache").fetchone()[0]lines 115–115assert rows == 0, "an unhashable output was recorded, so its digest is a wildcard"lines 116–116assert cache.doxygen_get(KEY_A) is None

test_doxygen_any_refuses_another_configurations_output

None test_indexcache_doxygen_cache.test_doxygen_any_refuses_another_configurations_output(Path tmp_path)

doxygen_any must refuse output produced under a different configuration.

tests/test_indexcache_doxygen_cache.py:196

Show on the map →

Called by none

Calls none

Checks

  • assert narrow != wide, "two different Doxyfiles must not share a configuration hash"
  • assert cache.doxygen_any(narrow) == output, ("output built under THIS configuration must be available to splice into, or the " "incremental path can never engage at all" )
  • assert cache.doxygen_any(wide) is None, ("output built under a DIFFERENT configuration was served for splicing — this is " "#399's aliasing, and it silently produces an index carrying the old scope's rows" )
  • assert cache.doxygen_any(narrow) is None, ("a row with no recorded configuration was assumed compatible; an unknown " "configuration must refuse rather than be guessed at" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measured
Source · 57 lines
def test_doxygen_any_refuses_another_configurations_output(tmp_path: Path) -> None:
    """THE REGRESSION THIS CLOSES SHIPPED ONCE AND WAS CAUGHT BY SOMEBODY ELSE'S TEST.

    `doxygen_any` serves a previous output for the incremental splice to splice INTO. Its
    first version returned the newest digest-verified row regardless of which key wrote it,
    which is exactly the aliasing `doxygen_get` was hardened against in #399: every key in
    `doxygen_cache` names the SAME path, so "verified" only proves the file still holds what
    that row recorded — not that the row belongs to this build.

    The consequence was concrete. Withdrawing an `index_scope` statement widened the tree, so
    the newly-visible files showed up as ADDED and the splice engaged — against output built
    under the NARROW scope. The file inventory came back and its functions did not.
    `test_a_stated_index_scope_narrows_the_index_and_a_withdrawal_widens_it` failed at commit
    time; nothing in this module's own tests noticed.

    So the property is pinned directly: same configuration serves, different configuration
    refuses, and a legacy row with no recorded configuration refuses rather than being
    assumed compatible.

    @brief A different configuration's output is never served for splicing.
    @return None.
    @version 1
    """
    output = tmp_path / "doxygen.db"
    output.write_bytes(b"pretend this is a doxygen database")
    cache = IndexCache(tmp_path / "sidecar.db", tmp_path)

    narrow = cache.config_sha("INPUT = src\n", [])
    wide = cache.config_sha("INPUT = .\n", [])
    assert narrow != wide, "two different Doxyfiles must not share a configuration hash"

    cache.doxygen_put("tree-1", output, narrow)

    assert cache.doxygen_any(narrow) == output, (
        "output built under THIS configuration must be available to splice into, or the "
        "incremental path can never engage at all"
    )
    assert cache.doxygen_any(wide) is None, (
        "output built under a DIFFERENT configuration was served for splicing — this is "
        "#399's aliasing, and it silently produces an index carrying the old scope's rows"
    )

    ## A row written before `config_sha` existed carries the empty string. Treating that as
    ## "matches anything" would restore the bug through the upgrade path, and treating it as
    ## "matches nothing" costs exactly one full rebuild.
    cache.conn.execute("UPDATE doxygen_cache SET config_sha = '' WHERE tree_sha = 'tree-1'")
    assert cache.doxygen_any(narrow) is None, (
        "a row with no recorded configuration was assumed compatible; an unknown "
        "configuration must refuse rather than be guessed at"
    )
    cache.commit()
    cache.close()


# ─── B4: the parser's own version is part of the configuration ────────────────

Activity · 13 steps
lines 219–219output = tmp_path / "doxygen.db"lines 220–220output.write_bytes(b"pretend this is a doxygen database")lines 221–221cache = IndexCache(tmp_path / "sidecar.db", tmp_path)lines 223–223narrow = cache.config_sha("INPUT = src\n", [])lines 224–224wide = cache.config_sha("INPUT = .\n", [])lines 225–225assert narrow != wide, "two different Doxyfiles must not share a configuration hash"lines 227–227cache.doxygen_put("tree-1", output, narrow)lines 229–232assert cache.doxygen_any(narrow) == output, ( "output built under THIS configuration must be available to splice into, or the " "incremental path can never engage at all" )lines 233–236assert cache.doxygen_any(wide) is None, ( "output built under a DIFFERENT configuration was served for splicing — this is " "#399's aliasing, and it silently produces an index carrying the old scope's rows" )lines 241–241cache.conn.execute("UPDATE doxygen_cache SET config_sha = '' WHERE tree_sha = 'tree-1'")lines 242–245assert cache.doxygen_any(narrow) is None, ( "a row with no recorded configuration was assumed compatible; an unknown " "configuration must refuse rather than be guessed at" )lines 246–246cache.commit()lines 247–247cache.close()

test_splice_generation_counts_and_resets

None test_indexcache_doxygen_cache.test_splice_generation_counts_and_resets(Path tmp_path)

The splice generation counter must advance, reset, and survive reopening.

tests/test_indexcache_doxygen_cache.py:158

Show on the map →

Called by none

Calls none

Checks

  • assert cache.splice_generation() == 0, "a fresh sidecar must start at generation zero"
  • assert cache.splice_generation() == expected
  • assert reopened.splice_generation() == 3, ("the counter did not survive reopening, so every build would start from zero and no " "generation limit could ever be reached — the bound would be decorative" )
  • assert reopened.splice_generation() == 0, "a full build must clear accumulated drift"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-INDEX-002measured
Source · 33 lines
def test_splice_generation_counts_and_resets(tmp_path: Path) -> None:
    """RUN THE GUARD AGAINST THE CASE IT WAS BUILT FOR. The counter exists to bound a
    MEASURED error: a spliced database carried one call edge a full rebuild does not emit,
    out of 5168, and nothing in a splice bounds that across many refreshes. A counter that
    silently failed to advance would restore the unbounded behaviour while reading as
    protection, so all three properties are pinned — it advances, a full build clears it, and
    it PERSISTS, because the sidecar is reopened on every build and an in-memory-only counter
    would reset itself to zero on each one and never reach any limit.

    @brief The generation counter advances, resets and persists.
    @return None.
    @version 1
    """
    cache_path = tmp_path / "sidecar.db"
    cache = IndexCache(cache_path, tmp_path)
    assert cache.splice_generation() == 0, "a fresh sidecar must start at generation zero"
    for expected in (1, 2, 3):
        cache.record_splice()
        assert cache.splice_generation() == expected
    cache.commit()
    cache.close()

    reopened = IndexCache(cache_path, tmp_path)
    assert reopened.splice_generation() == 3, (
        "the counter did not survive reopening, so every build would start from zero and no "
        "generation limit could ever be reached — the bound would be decorative"
    )
    reopened.record_splice(reset=True)
    assert reopened.splice_generation() == 0, "a full build must clear accumulated drift"
    reopened.commit()
    reopened.close()

Activity · 14 steps
lines 171–171cache_path = tmp_path / "sidecar.db"lines 172–172cache = IndexCache(cache_path, tmp_path)lines 173–173assert cache.splice_generation() == 0, "a fresh sidecar must start at generation zero"lines 174–174◇ for expected in (1, 2, 3)lines 175–175cache.record_splice()lines 176–176assert cache.splice_generation() == expectedlines 177–177cache.commit()lines 178–178cache.close()lines 180–180reopened = IndexCache(cache_path, tmp_path)lines 181–184assert reopened.splice_generation() == 3, ( "the counter did not survive reopening, so every build would start from zero and no " "generation limit could ever be reached — the bound would be decorative" )lines 185–185reopened.record_splice(reset=True)lines 186–186assert reopened.splice_generation() == 0, "a full build must clear accumulated drift"lines 187–187reopened.commit()lines 188–188reopened.close()