diff --git a/scripts/shard_pr_tests.py b/scripts/shard_pr_tests.py index 871e1e83..3eb61b91 100644 --- a/scripts/shard_pr_tests.py +++ b/scripts/shard_pr_tests.py @@ -2,7 +2,8 @@ """Split selected files into two deterministic, duration-balanced Gloo shards. Every selected file is assigned once, even without a duration or Gloo marker. -Unlisted files containing ``pytest.mark.gloo`` use the table's default_seconds; +Unlisted files whose code (not strings or comments) references +``pytest.mark.gloo`` use the table's default_seconds; other unlisted files have zero weight. Marker detection affects balance only. Paths must be canonical relative files inside tests/ (symlink escapes rejected). Duplicate input paths are deduplicated. Empty input produces two empty files. @@ -11,6 +12,7 @@ Duplicate input paths are deduplicated. Empty input produces two empty files. from __future__ import annotations import argparse +import ast import json import math from pathlib import Path @@ -33,6 +35,19 @@ def load_durations(path: Path) -> tuple[float, dict[str, float]]: return default, files +def has_gloo_marker(source: str) -> bool: + """Return whether Python source uses the ``pytest.mark.gloo`` marker expression.""" + + try: + tree = ast.parse(source) + except SyntaxError: + return False + return any( + isinstance(node, ast.Attribute) and ast.unparse(node) == "pytest.mark.gloo" + for node in ast.walk(tree) + ) + + def shard_tests( selected: list[str], default_seconds: float, durations: dict[str, float], *, root: Path, @@ -46,9 +61,9 @@ def shard_tests( raise ValueError(f"selected path must be a file inside tests/: {name!r}") weight = durations.get(name) if weight is None: - weight = default_seconds if "pytest.mark.gloo" in ( - root / path - ).read_text(encoding="utf-8") else 0 + weight = default_seconds if has_gloo_marker( + (root / path).read_text(encoding="utf-8") + ) else 0 weighted.append((name, weight)) shards: tuple[list[str], list[str]] = ([], []) loads = [0.0, 0.0] diff --git a/tests/test_ci_policy.py b/tests/test_ci_policy.py index f3ad341a..5f7aa57d 100644 --- a/tests/test_ci_policy.py +++ b/tests/test_ci_policy.py @@ -240,16 +240,13 @@ def test_gloo_marker_is_registered_and_marks_real_cpu_worker_files(): def test_gloo_durations_cover_exactly_the_marked_test_files(): - # AST detects real marker expressions, excluding quoted fixtures/comments. - import ast + # The sharder's AST detection excludes quoted fixtures and comments. + from scripts.shard_pr_tests import has_gloo_marker marked = { path.relative_to(ROOT).as_posix() for path in (ROOT / "tests").rglob("test_*.py") - if any( - isinstance(node, ast.Attribute) and ast.unparse(node) == "pytest.mark.gloo" - for node in ast.walk(ast.parse(path.read_text(encoding="utf-8"))) - ) + if has_gloo_marker(path.read_text(encoding="utf-8")) } table = json.loads((ROOT / "ci/gloo-test-durations.json").read_text()) assert set(table["files"]) == marked diff --git a/tests/test_shard_pr_tests.py b/tests/test_shard_pr_tests.py index e66b5679..86b9c48f 100644 --- a/tests/test_shard_pr_tests.py +++ b/tests/test_shard_pr_tests.py @@ -51,6 +51,29 @@ def test_unlisted_marked_files_get_default_but_all_files_are_assigned_once(tmp_p assert sorted(shards[0] + shards[1]) == sorted(names) +def test_marker_mentioned_only_in_strings_or_comments_has_no_weight(tmp_path): + names = ["tests/test_policy.py", "tests/test_worker.py"] + _files(tmp_path, names, marked=[names[1]]) + (tmp_path / names[0]).write_text( + '# pytest.mark.gloo\nFIXTURE = "pytestmark = pytest.mark.gloo"\n', encoding="utf-8" + ) + assert not shard_pr_tests.has_gloo_marker((tmp_path / names[0]).read_text()) + assert shard_pr_tests.has_gloo_marker((tmp_path / names[1]).read_text()) + assert shard_pr_tests.shard_tests(names, 80, {}, root=tmp_path) == ( + [names[1]], [names[0]], + ) + + +def test_real_selection_keeps_policy_tests_out_of_gloo_balance(): + default, durations = shard_pr_tests.load_durations(ROOT / "ci/gloo-test-durations.json") + names = [*durations, "tests/test_ci_policy.py", "tests/test_shard_pr_tests.py"] + shards = shard_pr_tests.shard_tests(names, default, durations, root=ROOT) + assert shards[0][0] == "tests/test_distributed_gloo.py" + assert shards[1][:2] == [ + "tests/test_distributed_model_gloo.py", "tests/test_distributed_probe_gloo.py", + ] + + def test_equal_weights_use_path_order_and_equal_loads_choose_zero(tmp_path): names = [f"tests/test_{letter}.py" for letter in "dcba"] _files(tmp_path, names)