diff --git a/server/code/app/coverage.py b/server/code/app/coverage.py index 8e4ad1cbc73..9b013867af2 100644 --- a/server/code/app/coverage.py +++ b/server/code/app/coverage.py @@ -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, diff --git a/server/code/app/symbol/test_links.py b/server/code/app/symbol/test_links.py index 9d81cd0cab6..1f438b2bfc7 100644 --- a/server/code/app/symbol/test_links.py +++ b/server/code/app/symbol/test_links.py @@ -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. @@ -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. @@ -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: diff --git a/server/code/store/index/coverage.py b/server/code/store/index/coverage.py index 108d20e9d06..252ab9b0f08 100644 --- a/server/code/store/index/coverage.py +++ b/server/code/store/index/coverage.py @@ -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 @@ -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 @@ -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] diff --git a/tests/unit/test_code_conventions.py b/tests/unit/test_code_conventions.py index bed038e6dac..f7a8550ed38 100644 --- a/tests/unit/test_code_conventions.py +++ b/tests/unit/test_code_conventions.py @@ -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 {} diff --git a/tests/unit/test_code_coverage_claim.py b/tests/unit/test_code_coverage_claim.py index 49e03c12216..2a99ce9650d 100644 --- a/tests/unit/test_code_coverage_claim.py +++ b/tests/unit/test_code_coverage_claim.py @@ -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") @@ -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 ------------------------------------------------------------- @@ -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 {} diff --git a/tests/unit/test_code_coverage_freshness.py b/tests/unit/test_code_coverage_freshness.py index 6241fcb4813..d32ce01f35a 100644 --- a/tests/unit/test_code_coverage_freshness.py +++ b/tests/unit/test_code_coverage_freshness.py @@ -29,7 +29,7 @@ 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, @@ -37,6 +37,7 @@ async def coverage_rows_for(self, org, ns, paths): "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") @@ -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)) @@ -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)) diff --git a/tests/unit/test_code_coverage_grep_rounds.py b/tests/unit/test_code_coverage_grep_rounds.py index 7e552996e77..25e06c7cb4a 100644 --- a/tests/unit/test_code_coverage_grep_rounds.py +++ b/tests/unit/test_code_coverage_grep_rounds.py @@ -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 {} diff --git a/tests/unit/test_code_coverage_tiers.py b/tests/unit/test_code_coverage_tiers.py index e2526fdc03e..d0ef12a178b 100644 --- a/tests/unit/test_code_coverage_tiers.py +++ b/tests/unit/test_code_coverage_tiers.py @@ -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 {} diff --git a/tests/unit/test_code_coverage_unit_tests.py b/tests/unit/test_code_coverage_unit_tests.py index 97dc633b320..45404994d48 100644 --- a/tests/unit/test_code_coverage_unit_tests.py +++ b/tests/unit/test_code_coverage_unit_tests.py @@ -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 {} diff --git a/tests/unit/test_code_test_gate.py b/tests/unit/test_code_test_gate.py index 7923e3ffd0e..41eccd6d607 100644 --- a/tests/unit/test_code_test_gate.py +++ b/tests/unit/test_code_test_gate.py @@ -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): @@ -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 @@ -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 = [ @@ -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