fix(work): import hashlib and wire heal subparser into main CLI
This commit is contained in:
@@ -42,6 +42,7 @@ def _load(name, relpath):
|
||||
|
||||
exec_constrained = _load("exec_constrained_md", "bin/exec-constrained.py")
|
||||
agent_md = _load("agent_md_mdtest", "bin/agent_md.py")
|
||||
box_ctl = _load("box_ctl_mdtest", "bin/box-ctl.py")
|
||||
|
||||
|
||||
def _box_ctl(*args, stdin=None):
|
||||
@@ -50,6 +51,39 @@ def _box_ctl(*args, stdin=None):
|
||||
input=stdin, capture_output=True, text=True, timeout=180)
|
||||
|
||||
|
||||
class _InProcResult:
|
||||
"""Minimal CompletedProcess stand-in (returncode/stdout only)."""
|
||||
def __init__(self, returncode, stdout):
|
||||
self.returncode = returncode
|
||||
self.stdout = stdout
|
||||
|
||||
|
||||
def _box_ctl_inproc(*args, stdin=None):
|
||||
"""In-process _box_ctl for dry-run and validation-failure verbs.
|
||||
|
||||
Calls the real main(argv) -- identical argv parsing, dispatch, audit,
|
||||
stdin reads, and stdout JSON -- with stdio captured, amortizing the
|
||||
~80ms per-spawn interpreter + module-exec cost over one import.
|
||||
stdin, when given, is fed exactly as subprocess input= would be.
|
||||
"""
|
||||
import io
|
||||
from contextlib import redirect_stdout
|
||||
buf = io.StringIO()
|
||||
returncode = 0
|
||||
saved_stdin = sys.stdin
|
||||
if stdin is not None:
|
||||
sys.stdin = io.StringIO(stdin)
|
||||
try:
|
||||
with redirect_stdout(buf):
|
||||
try:
|
||||
box_ctl.main(["box-ctl.py", *args])
|
||||
except SystemExit as e:
|
||||
returncode = e.code if isinstance(e.code, int) else 1
|
||||
finally:
|
||||
sys.stdin = saved_stdin
|
||||
return _InProcResult(returncode, buf.getvalue())
|
||||
|
||||
|
||||
class ExecMdOpsTests(unittest.TestCase):
|
||||
def test_ops_registered_and_side_effecting(self):
|
||||
spec = exec_constrained.OPS
|
||||
@@ -320,8 +354,10 @@ class BoxCtlMdTests(unittest.TestCase):
|
||||
["md-audit", "../x"],
|
||||
["md", "read", "646", "../x"],
|
||||
]
|
||||
# In-process dispatch: same main(argv) path, fail() JSON, and
|
||||
# exit code as a subprocess call. Assertions below are unchanged.
|
||||
for args in cases:
|
||||
r = _box_ctl(*args)
|
||||
r = _box_ctl_inproc(*args)
|
||||
self.assertNotEqual(r.returncode, 0, args)
|
||||
self.assertEqual(json.loads(r.stdout)["code"], "BAD_NAME", args)
|
||||
|
||||
@@ -333,13 +369,17 @@ class BoxCtlMdTests(unittest.TestCase):
|
||||
["md", "amend", "NOPE.md", "content here"],
|
||||
["md", "append", "NOPE.md", "note here"],
|
||||
]
|
||||
# In-process dispatch: same main(argv) path, stdin reads, fail()
|
||||
# JSON, and exit code as a subprocess call. Assertions below are
|
||||
# unchanged. (Amend/append parse --stdin BEFORE validating the
|
||||
# path, so stdin is fed here exactly as the spawn did.)
|
||||
for args in cases:
|
||||
r = _box_ctl(*args)
|
||||
r = _box_ctl_inproc(*args)
|
||||
self.assertNotEqual(r.returncode, 0, args)
|
||||
self.assertEqual(json.loads(r.stdout)["code"], "BAD_NAME", args)
|
||||
r = _box_ctl("md-amend", "../x", "--stdin", stdin="hi")
|
||||
r = _box_ctl_inproc("md-amend", "../x", "--stdin", stdin="hi")
|
||||
self.assertEqual(json.loads(r.stdout)["code"], "BAD_NAME")
|
||||
r = _box_ctl("md-append", "../x", "--stdin", stdin="hi")
|
||||
r = _box_ctl_inproc("md-append", "../x", "--stdin", stdin="hi")
|
||||
self.assertEqual(json.loads(r.stdout)["code"], "BAD_NAME")
|
||||
|
||||
def test_amend_stdin_safety_rejection_writes_nothing(self):
|
||||
@@ -386,7 +426,10 @@ class BoxCtlMdTests(unittest.TestCase):
|
||||
(["md-write", "646", "SOUL.md"], False),
|
||||
]
|
||||
for args, valid in cases:
|
||||
r = _box_ctl("quality-validate", *args)
|
||||
# In-process dry-run: same main(argv) path and stdout JSON as a
|
||||
# subprocess call, without the per-case spawn cost. Assertions
|
||||
# below are unchanged.
|
||||
r = _box_ctl_inproc("quality-validate", *args)
|
||||
self.assertEqual(json.loads(r.stdout)["valid"], valid, args)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user