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
Binary file added __pycache__/validate_pocs.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
26 changes: 26 additions & 0 deletions tests/test_validate_pocs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest
from pathlib import Path

import validate_pocs


class ValidatePocsTest(unittest.TestCase):
def setUp(self) -> None:
self.repo_root = Path(__file__).resolve().parents[1]

def test_discovery_finds_expected_targets(self) -> None:
python_targets = validate_pocs.discover_python_targets(self.repo_root)
package_targets = validate_pocs.discover_package_targets(self.repo_root)

self.assertTrue(any(path.name == "run_demo.py" for path in python_targets))
self.assertTrue(any(path.name == "package.json" for path in package_targets))
self.assertGreater(len(python_targets), 20)
self.assertGreater(len(package_targets), 0)
Comment thread
5hy7xz92nd-oss marked this conversation as resolved.

def test_validation_succeeds(self) -> None:
result = validate_pocs.validate_repo(self.repo_root)
self.assertEqual(result["errors"], [])


if __name__ == "__main__":
unittest.main()
84 changes: 84 additions & 0 deletions validate_pocs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env python3
"""Validate the repository's PoC entrypoints and manifests."""

from __future__ import annotations

import argparse
import json
import py_compile
import sys
from pathlib import Path
from typing import List, Dict, Tuple
Comment thread
5hy7xz92nd-oss marked this conversation as resolved.


EXCLUDED_DIRS = {".git", "__pycache__"}


def discover_python_targets(root: Path) -> List[Path]:
return sorted(
path
for path in root.rglob("*.py")
if not any(part in EXCLUDED_DIRS for part in path.parts)
)


def discover_package_targets(root: Path) -> List[Path]:
return sorted(
path
for path in root.rglob("package.json")
if not any(part in EXCLUDED_DIRS for part in path.parts)
)


def validate_repo(root: Path) -> Dict[str, object]:
python_targets = discover_python_targets(root)
package_targets = discover_package_targets(root)
errors: List[str] = []

for path in python_targets:
try:
py_compile.compile(str(path), doraise=True)
except py_compile.PyCompileError as exc: # pragma: no cover - exercised in tests when broken code exists
errors.append(f"{path.relative_to(root)}: {exc}")

for path in package_targets:
try:
data = json.loads(path.read_text(encoding="utf-8"))
except json.JSONDecodeError as exc:
errors.append(f"{path.relative_to(root)}: invalid JSON ({exc})")
continue

scripts = data.get("scripts")
if isinstance(scripts, dict) and "poc" in scripts:
if not isinstance(scripts["poc"], str) or not scripts["poc"].strip():
errors.append(f"{path.relative_to(root)}: scripts.poc must be a non-empty string")

return {
"root": str(root),
"python_files": len(python_targets),
"package_json_files": len(package_targets),
"errors": errors,
}


def main(argv: List[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parent)
args = parser.parse_args(argv)

result = validate_repo(args.root.resolve())
for error in result["errors"]:
print(error, file=sys.stderr)

print(
f"validated {result['python_files']} Python files and {result['package_json_files']} package manifests"
)
if result["errors"]:
print("validation failed", file=sys.stderr)
return 1
print("validation succeeded")
return 0


if __name__ == "__main__":
raise SystemExit(main())