Skip to content
Open
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
45 changes: 45 additions & 0 deletions scripts/test_inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
"""Create a JSON inventory of TileKernels test files by operator area."""

from __future__ import annotations

import argparse
import json
from collections import defaultdict
from pathlib import Path


def build_inventory(root: Path) -> dict[str, object]:
groups: dict[str, list[str]] = defaultdict(list)
tests_dir = root / "tests"
if not tests_dir.is_dir():
return {"total": 0, "groups": {}}

for path in sorted(tests_dir.rglob("test_*.py")):
rel = path.relative_to(root).as_posix()
parts = path.relative_to(tests_dir).parts
group = parts[0] if len(parts) > 1 else "root"
groups[group].append(rel)
return {
"total": sum(len(items) for items in groups.values()),
"groups": {name: {"count": len(items), "files": items} for name, items in sorted(groups.items())},
}


def main() -> int:
parser = argparse.ArgumentParser(description="List TileKernels tests by area.")
parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parents[1])
parser.add_argument("--output", type=Path)
args = parser.parse_args()

text = json.dumps(build_inventory(args.root.resolve()), indent=2, sort_keys=True)
if args.output:
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(text + "\n", encoding="utf-8")
else:
print(text)
return 0


if __name__ == "__main__":
raise SystemExit(main())
21 changes: 21 additions & 0 deletions tests/test_test_inventory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pathlib import Path

from scripts.test_inventory import build_inventory


def test_build_inventory_groups_nested_tests(tmp_path: Path):
test_dir = tmp_path / "tests" / "moe"
test_dir.mkdir(parents=True)
(test_dir / "test_gate.py").write_text("", encoding="utf-8")

inventory = build_inventory(tmp_path)

assert inventory["total"] == 1
assert inventory["groups"]["moe"]["files"] == ["tests/moe/test_gate.py"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

建议增加一个测试用例,用于验证当 tests 目录不存在时,build_inventory 能够安全返回空的总览结构而不抛出异常。

Suggested change
assert inventory["groups"]["moe"]["files"] == ["tests/moe/test_gate.py"]
assert inventory["groups"]["moe"]["files"] == ["tests/moe/test_gate.py"]
def test_build_inventory_no_tests_dir(tmp_path: Path):
inventory = build_inventory(tmp_path)
assert inventory["total"] == 0
assert inventory["groups"] == {}



def test_build_inventory_handles_missing_tests_dir(tmp_path: Path):
inventory = build_inventory(tmp_path)

assert inventory["total"] == 0
assert inventory["groups"] == {}