2026-10-23 22:23:11 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""Choose affected test projects for a PR; test everything on main and tags."""
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
import subprocess
|
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
TESTS = sorted(path for path in (ROOT / "tests").glob("*/*.csproj") if path.stem != "Heimdall.TestCommon")
|
2026-11-09 17:06:36 +01:00
|
|
|
SOURCES = sorted((ROOT / "src").glob("*/*.csproj"))
|
|
|
|
|
ALL_PROJECTS = [*SOURCES, *(ROOT / "tests").glob("*/*.csproj")]
|
2026-10-23 22:23:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def references(project: Path) -> set[Path]:
|
|
|
|
|
tree = ET.parse(project)
|
|
|
|
|
return {
|
|
|
|
|
(project.parent / item.attrib["Include"].replace("\\", "/")).resolve()
|
|
|
|
|
for item in tree.iter("ProjectReference")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-11-09 17:06:36 +01:00
|
|
|
def referenced_projects(project: Path, graph: dict[Path, set[Path]]) -> set[Path]:
|
|
|
|
|
result: set[Path] = set()
|
|
|
|
|
pending = list(graph.get(project, set()))
|
|
|
|
|
while pending:
|
|
|
|
|
dependency = pending.pop()
|
|
|
|
|
if dependency in result:
|
|
|
|
|
continue
|
|
|
|
|
result.add(dependency)
|
|
|
|
|
pending.extend(graph.get(dependency, set()))
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_targets(affected: set[Path], suites: list[str], graph: dict[Path, set[Path]],
|
|
|
|
|
mode: str) -> list[str]:
|
|
|
|
|
if mode == "all":
|
|
|
|
|
return ["Heimdall.slnx"]
|
|
|
|
|
|
|
|
|
|
suite_projects = [project for project in TESTS if project.stem in suites]
|
|
|
|
|
covered_sources = set().union(*(referenced_projects(project, graph) for project in suite_projects)) if suite_projects else set()
|
|
|
|
|
targets = [*suite_projects, *(project for project in SOURCES
|
|
|
|
|
if project.resolve() in affected and project.resolve() not in covered_sources)]
|
|
|
|
|
return sorted({project.resolve().relative_to(ROOT).as_posix() for project in targets})
|
|
|
|
|
|
|
|
|
|
|
2026-10-23 22:23:11 +02:00
|
|
|
def changed_files() -> list[str] | None:
|
|
|
|
|
if os.environ.get("GITEA_EVENT_NAME") != "pull_request":
|
|
|
|
|
return None
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
["git", "diff", "--name-only", "origin/main...HEAD"],
|
|
|
|
|
cwd=ROOT, text=True, capture_output=True, check=False,
|
|
|
|
|
)
|
|
|
|
|
return result.stdout.splitlines() if result.returncode == 0 else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def plan(changed: list[str] | None) -> dict[str, object]:
|
|
|
|
|
if changed is None:
|
2026-11-09 17:06:36 +01:00
|
|
|
suites = [p.stem for p in TESTS]
|
|
|
|
|
return {"mode": "all", "changed": [], "suites": suites,
|
|
|
|
|
"build_projects": build_targets(set(), suites, {}, "all")}
|
2026-10-23 22:23:11 +02:00
|
|
|
|
2026-11-09 17:06:36 +01:00
|
|
|
all_paths = {p.resolve() for p in ALL_PROJECTS}
|
2026-10-23 22:23:11 +02:00
|
|
|
graph = {project: references(project) for project in all_paths}
|
|
|
|
|
changed_projects: set[Path] = set()
|
|
|
|
|
broad = False
|
|
|
|
|
for name in changed:
|
|
|
|
|
parts = Path(name).parts
|
|
|
|
|
if parts and parts[0] in {"src", "tests"} and len(parts) >= 3:
|
|
|
|
|
if name.endswith(".csproj"):
|
|
|
|
|
broad = True
|
|
|
|
|
matches = list((ROOT / parts[0] / parts[1]).glob("*.csproj"))
|
|
|
|
|
changed_projects.update(p.resolve() for p in matches)
|
|
|
|
|
elif parts and parts[0] in {"src", "tests"}:
|
|
|
|
|
broad = True
|
|
|
|
|
elif name.startswith(("scripts/ci/", "docker/", "models/")) or name in {
|
|
|
|
|
"Heimdall.slnx", "global.json", "Directory.Build.props",
|
|
|
|
|
"Directory.Build.targets", "GitVersion.yml", "GitVersion.yaml",
|
|
|
|
|
}:
|
|
|
|
|
broad = True
|
|
|
|
|
|
|
|
|
|
if not broad and changed and not changed_projects and all(
|
2026-11-17 10:48:38 +01:00
|
|
|
name.startswith(("docs/", "skills/"))
|
2026-11-06 14:11:28 +01:00
|
|
|
or name in {"readme.md", "AGENTS.md", ".gitea/PULL_REQUEST_TEMPLATE.md"}
|
2026-10-23 22:23:11 +02:00
|
|
|
for name in changed
|
|
|
|
|
):
|
2026-11-09 17:06:36 +01:00
|
|
|
return {"mode": "affected", "changed": changed, "suites": [], "build_projects": []}
|
2026-10-23 22:23:11 +02:00
|
|
|
|
|
|
|
|
if broad or (changed and not changed_projects):
|
2026-11-09 17:06:36 +01:00
|
|
|
suites = [p.stem for p in TESTS]
|
|
|
|
|
return {"mode": "all", "changed": changed, "suites": suites,
|
|
|
|
|
"build_projects": build_targets(set(), suites, graph, "all")}
|
2026-10-23 22:23:11 +02:00
|
|
|
|
|
|
|
|
affected = set(changed_projects)
|
|
|
|
|
while True:
|
|
|
|
|
additions = {project for project, refs in graph.items() if refs & affected}
|
|
|
|
|
if additions <= affected:
|
|
|
|
|
break
|
|
|
|
|
affected |= additions
|
|
|
|
|
|
|
|
|
|
suites = [p.stem for p in TESTS if p.resolve() in affected]
|
2026-11-09 17:06:36 +01:00
|
|
|
projects = build_targets(affected, suites, graph, "affected")
|
|
|
|
|
return {"mode": "affected", "changed": changed, "suites": suites,
|
|
|
|
|
"build_projects": projects}
|
2026-10-23 22:23:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
result = plan(changed_files())
|
|
|
|
|
output = ROOT / "artifacts" / "tests" / "plan.json"
|
|
|
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
output.write_text(json.dumps(result, indent=2) + "\n")
|
|
|
|
|
print(f"Test plan: {result['mode']} — {', '.join(result['suites']) or 'no .NET suite'}")
|
2026-11-09 17:06:36 +01:00
|
|
|
print(f"Build targets: {', '.join(result['build_projects']) or 'none'}")
|