Skill Tracer
Skills › BMAD Method › bmad-toolsmith › test-template.py

test-template.py

38 lines · skills/bmad-toolsmith/shapes/script-utility/assets/test-template.py at bd70c64

1import json
2import subprocess
3import sys
4import tempfile
5import unittest
6from pathlib import Path
7
8SCRIPT = Path(__file__).resolve().parents[1] / "{script}.py"
9
11def run(*args: str) -> subprocess.CompletedProcess[str]:
12 """Run the script as a subprocess, the way the agent does."""
13 return subprocess.run([sys.executable, str(SCRIPT), *args], capture_output=True, text=True, check=False)
16class ScriptTest(unittest.TestCase):
17 def setUp(self) -> None:
18 tmp = tempfile.TemporaryDirectory()
19 self.addCleanup(tmp.cleanup)
20 self.root = Path(tmp.name)
22 def test_reports_the_result(self) -> None:
23 fixture = self.root / "input.txt"
24 fixture.write_text("{fixture contents}\n", encoding="utf-8")
25 result = run(str(fixture))
26 self.assertEqual(result.returncode, 0, result.stderr)
27 data = json.loads(result.stdout)
28 self.assertEqual(data["path"], str(fixture))
30 def test_fails_with_one_line(self) -> None:
31 result = run(str(self.root / "missing.txt"))
32 self.assertEqual(result.returncode, 1)
33 self.assertEqual(result.stderr.count("\n"), 1, result.stderr)
34 self.assertIn("not found", result.stderr)
37if __name__ == "__main__":
38 unittest.main()