Skip to content
Merged
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
15 changes: 14 additions & 1 deletion simple/stamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,20 @@ def main():
p = argparse.ArgumentParser()
p.add_argument("run"); p.add_argument("--check", action="store_true")
a = p.parse_args()
chain = [json.loads(l) for l in (Path(a.run) / "chain.jsonl").read_text(encoding="utf-8").splitlines()]
chain_file = Path(a.run) / "chain.jsonl"
if not chain_file.exists():
sys.exit(f"{chain_file} not found")
chain = []
with chain_file.open("r", encoding="utf-8") as f:
for n, line in enumerate(f, 1):
if not line.strip():
continue
try:
chain.append(json.loads(line))
except json.JSONDecodeError as err:
sys.exit(f"{chain_file}: line {n} is not valid JSON ({err.msg}); the file is cut off or damaged")
if not chain:
sys.exit(f"{chain_file} has no chain entries")
if a.check:
sys.exit(0 if check(a.run, chain) else 1)
for e in chain:
Expand Down
52 changes: 52 additions & 0 deletions tests/simple/test_stamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Unit tests for simple/stamp.py."""
import json
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest

ROOT = Path(__file__).resolve().parents[2]
PYTHON = sys.executable


class StampMainTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.run = Path(self.tmp.name)

def tearDown(self):
self.tmp.cleanup()

def _run_stamp(self, args):
cmd = [PYTHON, str(ROOT / "simple" / "stamp.py"), str(self.run)] + args
res = subprocess.run(cmd, capture_output=True, text=True)
return res.returncode, res.stdout, res.stderr

def test_missing_chain_file_fails(self):
code, out, err = self._run_stamp(["--check"])
self.assertNotEqual(code, 0)
self.assertIn("not found", err)

def test_empty_chain_fails_closed(self):
(self.run / "chain.jsonl").write_text("")
code, out, err = self._run_stamp(["--check"])
self.assertNotEqual(code, 0)
self.assertIn("has no chain entries", err)

def test_whitespace_only_chain_fails_closed(self):
(self.run / "chain.jsonl").write_text(" \n\n \n")
code, out, err = self._run_stamp(["--check"])
self.assertNotEqual(code, 0)
self.assertIn("has no chain entries", err)

def test_malformed_json_fails_with_line_number(self):
valid = json.dumps({"index": 0, "kind": "initial"})
(self.run / "chain.jsonl").write_text(f"{valid}\n{{bad json\n")
code, out, err = self._run_stamp(["--check"])
self.assertNotEqual(code, 0)
self.assertIn("line 2 is not valid JSON", err)


if __name__ == "__main__":
unittest.main()
Loading