Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/code/app/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,10 @@ async def claim_tests(
exact_terms = {t for t, tier in terms.items() if tier == cc.EXACT_SCOPED}
ordered_terms = list(terms.items())
# The terms' test-tree greps are one statement per claim.
map_rows, greps, cov_rows = await asyncio.gather(
map_rows, greps, (cov_rows, _) = await asyncio.gather(
repo.test_map_detail(org, ns, src_paths, limit=cap) if src_paths else _empty_list(),
_grep_tests_many(repo, org, ns, [(t, cap) for t, _ in ordered_terms]),
repo.coverage_rows_for(org, ns, src_paths) if src_paths else _empty_list(),
repo.coverage_rows_for(org, ns, src_paths),
)
current, historical = await partition_coverage_rows(
repo,
Expand Down
6 changes: 1 addition & 5 deletions server/code/app/symbol/test_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
)
from server.code.store.index import CodeRepo
from server.code.store.index import prefer_scip_edges as _prefer_scip_edges
from server.code.store.index.coverage import COVERAGE_ROW_PATHS
from server.settings import settings

# A Python class call ends at the class symbol: a test that builds the object runs these.
Expand All @@ -38,8 +37,6 @@
# itself, a fixture on the way, the class of a touched constructor, the function a
# touched nested function is defined in (a call of it need not run the nested one).
CHAIN_ENDS = ("", "fixture", "constructor", "enclosing")
# Coverage rows read for the executed definitions' files.
_COVERAGE_ROWS = 2000
# Edges read per hop of the walk.
READ_LIMIT = 300
# Test-map rows read for the source paths of a definition.
Expand Down Expand Up @@ -225,8 +222,7 @@ async def _coverage(
executed.setdefault(str(r["path"]), []).append((str(r["symbol_id"]), run["body"]))
if not executed:
return measured, [], False
rows = await repo.coverage_rows_for(org, ns, list(executed), limit=_COVERAGE_ROWS)
capped = len(executed) > COVERAGE_ROW_PATHS or len(rows) >= _COVERAGE_ROWS
rows, capped = await repo.coverage_rows_for(org, ns, list(executed))
current, _ = await partition_coverage_rows(repo, org, ns, rows)
tests: dict[str, dict[str, Any]] = {}
for row in current:
Expand Down
17 changes: 10 additions & 7 deletions server/code/store/index/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from server.code.store.index.base import CodeRepoBase
from server.code.store.index.sql import _batched

# `coverage_rows_for` reads this many paths; a caller with more knows its read was cut.
# `coverage_rows_for` reads coverage rows for at most this many paths.
COVERAGE_ROW_PATHS = 50


Expand Down Expand Up @@ -43,13 +43,15 @@ async def live_coverage_files(self, org: str, ns: str) -> set[str]:

async def coverage_rows_for(
self, org: str, ns: str, paths: list[str], *, limit: int = 2000
) -> list[dict[str, Any]]:
"""The `coverage` test-map rows of the first `COVERAGE_ROW_PATHS` paths, most specific
first (the order total): the claim filters them by the subject's span,
so it reads them all rather than the flat strongest page."""
) -> tuple[list[dict[str, Any]], bool]:
"""Return the most specific coverage rows first and whether a read bound was reached.

The result can be incomplete when there are more than `COVERAGE_ROW_PATHS` paths
or when the query returns `limit` rows.
"""
if not paths:
return []
return await self._ch.query_all(
return [], False
rows = await self._ch.query_all(
"""
SELECT src_path, test_path, confidence, evidence
FROM loom.code_test_map FINAL
Expand All @@ -61,6 +63,7 @@ async def coverage_rows_for(
""",
{"org": org, "ns": ns, "paths": paths[:COVERAGE_ROW_PATHS], "limit": limit},
)
return rows, len(paths) > COVERAGE_ROW_PATHS or len(rows) >= limit

async def coverage_files_for(
self, org: str, ns: str, paths: list[str]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_code_conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ async def grep_files(
return [], 0

async def coverage_rows_for(self, org, ns, paths, *, limit=2000):
return []
return [], False

async def coverage_files_for(self, org, ns, paths):
return {}
Expand Down
31 changes: 30 additions & 1 deletion tests/unit/test_code_coverage_claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import server.code.app.coverage as h
from server.code.api.models_testing import CodeCoverageClaimInput, CodeTestHomeInput
from server.code.domain.index import coverage_claim as cc
from server.code.store.index import CodeRepo
from server.code.store.index.coverage import COVERAGE_ROW_PATHS
from tests.unit.ch_fake import RecordingCH

# The scalar code repository these tests model (none is configured by default).
pytestmark = pytest.mark.usefixtures("scalar_code_repo")
Expand All @@ -18,6 +21,32 @@
ORG, NS = "clickhouse", "code-clickhouse"


@pytest.mark.asyncio
@pytest.mark.parametrize(
("path_count", "row_count", "expected_capped"),
[(0, 0, False), (1, 1, False), (1, 2, True), (COVERAGE_ROW_PATHS + 1, 1, True)],
)
async def test_coverage_read_reports_its_path_and_row_bounds(
path_count: int, row_count: int, expected_capped: bool
) -> None:
paths = [f"src/file_{i}.py" for i in range(path_count)]
rows = [
{"src_path": "src/file_0.py", "test_path": f"tests/test_{i}.py"} for i in range(row_count)
]
ch = RecordingCH().on("method = 'coverage'", rows)

assert await CodeRepo(ch).coverage_rows_for(ORG, NS, paths, limit=2) == (rows, expected_capped)
if paths:
assert ch.calls[0].params == {
"org": ORG,
"ns": NS,
"paths": paths[:COVERAGE_ROW_PATHS],
"limit": 2,
}
else:
assert not ch.calls


# ---- pure helpers -------------------------------------------------------------


Expand Down Expand Up @@ -178,7 +207,7 @@ async def symbols_by_name(self, org, ns, name, *, limit=50):
)

async def coverage_rows_for(self, org, ns, paths, *, limit=2000):
return []
return [], False

async def coverage_files_for(self, org, ns, paths):
return {}
Expand Down
9 changes: 5 additions & 4 deletions tests/unit/test_code_coverage_freshness.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ async def coverage_files_for(self, org, ns, paths):

async def coverage_rows_for(self, org, ns, paths):
assert (org, ns) == (ORG, NS)
return [
rows = [
{
"src_path": SRC,
"test_path": TEST,
"confidence": 1.0,
"evidence": cr.pair_evidence(self.measured, [(10, 12)], 3),
}
]
return rows, False

async def get_state(self, org, ns, key):
assert (org, ns, key) == (ORG, NS, "git_head")
Expand Down Expand Up @@ -155,9 +156,9 @@ async def coverage_files_for(self, org, ns, paths):
async def test_pair_uses_its_own_commit_even_when_file_measurement_is_current(monkeypatch):
class OldPairRepo(CoverageRepo):
async def coverage_rows_for(self, org, ns, paths):
rows = await super().coverage_rows_for(org, ns, paths)
rows, capped = await super().coverage_rows_for(org, ns, paths)
rows[0]["evidence"] = cr.pair_evidence("different", [(10, 12)], 3)
return rows
return rows, capped

repo = OldPairRepo(indexed="current", measured="current")
out = await run_claim(monkeypatch, repo, (10, 12))
Expand Down Expand Up @@ -256,7 +257,7 @@ async def test_symbol_file_association_without_span_identity_stays_historical():
async def test_measured_execution_without_a_specific_test_is_not_uncovered(monkeypatch):
class SharedLinesRepo(CoverageRepo):
async def coverage_rows_for(self, org, ns, paths):
return []
return [], False

repo = SharedLinesRepo(indexed="current", measured="current")
out = await run_claim(monkeypatch, repo, (10, 12))
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_code_coverage_grep_rounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ async def grep_files_many(self, org, ns, patterns, *, path_prefixes, limit):
)

async def coverage_rows_for(self, org, ns, paths, *, limit=2000):
return []
return [], False

async def coverage_files_for(self, org, ns, paths):
return {}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_code_coverage_tiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ async def symbols_by_name(self, org, ns, name, *, limit=50):
return []

async def coverage_rows_for(self, org, ns, paths, *, limit=2000):
return []
return [], False

async def coverage_files_for(self, org, ns, paths):
return {}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_code_coverage_unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ async def test_files_calling(self, org, ns, definitions, *, limit):
return []

async def coverage_rows_for(self, org, ns, paths, *, limit=2000):
return []
return [], False

async def coverage_files_for(self, org, ns, paths):
return {}
Expand Down
14 changes: 6 additions & 8 deletions tests/unit/test_code_test_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ def __init__(self) -> None:
self.neighbor_definitions: dict[str, str] = {}
self.coverage_files: dict[str, dict[str, Any]] = {}
self.coverage_rows: list[dict[str, Any]] = []
self.coverage_limit = 2000
self.kinds: tuple[str, ...] = ()

async def functions_for_paths(self, org, ns, paths, *, kinds=(), with_revision=False, **_kw):
Expand Down Expand Up @@ -442,8 +443,9 @@ async def test_map_for(self, org, ns, paths, *, limit=30):
async def coverage_files_for(self, org, ns, paths):
return {p: f for p, f in self.coverage_files.items() if p in paths}

async def coverage_rows_for(self, org, ns, paths, *, limit=2000):
return [r for r in self.coverage_rows if r["src_path"] in paths][:limit]
async def coverage_rows_for(self, org, ns, paths):
rows = [r for r in self.coverage_rows if r["src_path"] in paths][: self.coverage_limit]
return rows, len(rows) >= self.coverage_limit

async def get_state(self, org, ns, key):
return "c" * 40
Expand Down Expand Up @@ -757,11 +759,7 @@ async def test_definitions_that_share_a_name_are_touched_and_measured_apart(inte
assert [u["start_line"] for u in out.untested] == [17]


async def test_a_cut_coverage_read_is_a_cause_and_a_test_only_change_has_none(
intel, monkeypatch: pytest.MonkeyPatch
) -> None:
import server.code.app.symbol.test_links as links

async def test_a_cut_coverage_read_is_a_cause_and_a_test_only_change_has_none(intel) -> None:
repo = _py_repo()
repo.coverage_files = {"pkg/w.py": {"commit_sha": "c" * 40, "lines": "[[1, 99]]", "tests": 2}}
repo.coverage_rows = [
Expand All @@ -777,7 +775,7 @@ async def test_a_cut_coverage_read_is_a_cause_and_a_test_only_change_has_none(
assert out.epistemic == "measured" and out.causes == []
assert [t["why"] for t in out.tests].count("coverage") == 3
# the read returned as many rows as it asks for: more may exist
monkeypatch.setattr(links, "_COVERAGE_ROWS", 2)
repo.coverage_limit = 2
out = await _gate(intel, repo, diff=_one_line("pkg/w.py", 46))
assert out.causes == ["truncated"]
assert [t["why"] for t in out.tests].count("coverage") == 2
Expand Down
Loading