Carry the root across a reload; the tests tell it from the cwd
This commit is contained in:
+6
-6
@@ -352,7 +352,7 @@ def run_cycle(root, task, premises, name, session_id):
|
|||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def reload_if_edited(task, cycle_number, failures):
|
def reload_if_edited(root, task, cycle_number, failures):
|
||||||
try:
|
try:
|
||||||
source = open(__file__, encoding="utf-8").read()
|
source = open(__file__, encoding="utf-8").read()
|
||||||
except OSError as failure:
|
except OSError as failure:
|
||||||
@@ -364,21 +364,21 @@ def reload_if_edited(task, cycle_number, failures):
|
|||||||
if trial.returncode != 0:
|
if trial.returncode != 0:
|
||||||
print(f"keeping the running script: {trial.stderr.strip() or f'exit {trial.returncode}'}", file=sys.stderr)
|
print(f"keeping the running script: {trial.stderr.strip() or f'exit {trial.returncode}'}", file=sys.stderr)
|
||||||
return
|
return
|
||||||
os.execv(sys.executable, [sys.executable, __file__, RELOAD_FLAG, str(cycle_number), str(failures), task])
|
os.execv(sys.executable, [sys.executable, __file__, RELOAD_FLAG, str(cycle_number), str(failures), root, task])
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
argv = sys.argv[1:]
|
argv = sys.argv[1:]
|
||||||
cycle_number, failures = 0, 0
|
cycle_number, failures, root = 0, 0, ""
|
||||||
if argv[:1] == [RELOAD_FLAG]:
|
if argv[:1] == [RELOAD_FLAG]:
|
||||||
cycle_number, failures, argv = int(argv[1]), int(argv[2]), argv[3:]
|
cycle_number, failures, root, argv = int(argv[1]), int(argv[2]), argv[3], argv[4:]
|
||||||
task = " ".join(argv)
|
task = " ".join(argv)
|
||||||
if task in ("", "-h", "--help"):
|
if task in ("", "-h", "--help"):
|
||||||
print(__doc__.strip())
|
print(__doc__.strip())
|
||||||
sys.exit(0 if task else 1)
|
sys.exit(0 if task else 1)
|
||||||
root = checkout_root()
|
root = root or checkout_root()
|
||||||
while True:
|
while True:
|
||||||
reload_if_edited(task, cycle_number, failures)
|
reload_if_edited(root, task, cycle_number, failures)
|
||||||
cycle_number += 1
|
cycle_number += 1
|
||||||
premises = read_premises(root)
|
premises = read_premises(root)
|
||||||
name = session_name(root)
|
name = session_name(root)
|
||||||
|
|||||||
+11
-11
@@ -331,38 +331,38 @@ class RunCycle(unittest.TestCase):
|
|||||||
|
|
||||||
|
|
||||||
class Reload(unittest.TestCase):
|
class Reload(unittest.TestCase):
|
||||||
def test_runs_the_edited_file_before_trusting_it_and_hands_it_the_counters_on_argv(self):
|
def test_runs_the_edited_file_before_trusting_it_and_hands_it_the_root_and_counters_on_argv(self):
|
||||||
with tempfile.TemporaryDirectory() as root:
|
with tempfile.TemporaryDirectory() as root:
|
||||||
script = os.path.join(root, "loop-claude")
|
script = os.path.join(root, "loop-claude")
|
||||||
open(script, "w").write(loop.RELOAD_BASELINE.replace("import glob\n", "import glob\nimport no_such_module\n"))
|
open(script, "w").write(loop.RELOAD_BASELINE.replace("import glob\n", "import glob\nimport no_such_module\n"))
|
||||||
with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv:
|
with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv:
|
||||||
loop.reload_if_edited("do it", cycle_number=1, failures=0)
|
loop.reload_if_edited("/r", "do it", cycle_number=1, failures=0)
|
||||||
execv.assert_not_called()
|
execv.assert_not_called()
|
||||||
open(script, "w").write(loop.RELOAD_BASELINE.replace("RELOAD_FLAG = ", "RELOAD_FLAG = \"x\" or "))
|
open(script, "w").write(loop.RELOAD_BASELINE.replace("RELOAD_FLAG = ", "RELOAD_FLAG = \"x\" or "))
|
||||||
environment = dict(os.environ)
|
environment = dict(os.environ)
|
||||||
with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv:
|
with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv:
|
||||||
loop.reload_if_edited("do it", cycle_number=1, failures=2)
|
loop.reload_if_edited("/r", "do it", cycle_number=1, failures=2)
|
||||||
execv.assert_called_once_with(sys.executable, [sys.executable, script, loop.RELOAD_FLAG, "1", "2", "do it"])
|
execv.assert_called_once_with(sys.executable, [sys.executable, script, loop.RELOAD_FLAG, "1", "2", "/r", "do it"])
|
||||||
self.assertEqual(dict(os.environ), environment)
|
self.assertEqual(dict(os.environ), environment)
|
||||||
|
|
||||||
|
|
||||||
class Main(unittest.TestCase):
|
class Main(unittest.TestCase):
|
||||||
def test_resumes_the_counters_a_reload_handed_over(self):
|
def test_resumes_the_root_and_counters_a_reload_handed_over(self):
|
||||||
patches = [unittest.mock.patch.object(loop, name) for name in ("checkout_root", "read_premises", "reload_if_edited", "run_cycle")]
|
patches = [unittest.mock.patch.object(loop, name) for name in ("checkout_root", "read_premises", "reload_if_edited", "run_cycle")]
|
||||||
with contextlib.ExitStack() as stack:
|
with contextlib.ExitStack() as stack:
|
||||||
mocks = [stack.enter_context(patch) for patch in patches]
|
mocks = [stack.enter_context(patch) for patch in patches]
|
||||||
stack.enter_context(unittest.mock.patch.object(sys, "argv", ["loop-claude", loop.RELOAD_FLAG, "3", "2", "do it"]))
|
stack.enter_context(unittest.mock.patch.object(sys, "argv", ["loop-claude", loop.RELOAD_FLAG, "3", "2", "/r", "do it"]))
|
||||||
stack.enter_context(unittest.mock.patch.object(sys, "stdout", io.StringIO()))
|
stack.enter_context(unittest.mock.patch.object(sys, "stdout", io.StringIO()))
|
||||||
mocks[0].return_value = os.getcwd()
|
|
||||||
mocks[3].side_effect = SystemExit(0)
|
mocks[3].side_effect = SystemExit(0)
|
||||||
with self.assertRaises(SystemExit):
|
with self.assertRaises(SystemExit):
|
||||||
loop.main()
|
loop.main()
|
||||||
mocks[1].assert_called_once_with(os.getcwd())
|
mocks[0].assert_not_called()
|
||||||
mocks[2].assert_called_once_with("do it", 3, 2)
|
mocks[1].assert_called_once_with("/r")
|
||||||
self.assertEqual(mocks[3].call_args.args[0], os.getcwd())
|
mocks[2].assert_called_once_with("/r", "do it", 3, 2)
|
||||||
|
self.assertEqual(mocks[3].call_args.args[0], "/r")
|
||||||
self.assertIn("=== cycle 4 ===", sys.stdout.getvalue())
|
self.assertIn("=== cycle 4 ===", sys.stdout.getvalue())
|
||||||
mocks[3].side_effect = ["claude exited 3"] * 3
|
mocks[3].side_effect = ["claude exited 3"] * 3
|
||||||
stack.enter_context(unittest.mock.patch.object(sys, "argv", ["loop-claude", loop.RELOAD_FLAG, "3", "0", "do it"]))
|
stack.enter_context(unittest.mock.patch.object(sys, "argv", ["loop-claude", loop.RELOAD_FLAG, "3", "0", "/r", "do it"]))
|
||||||
stack.enter_context(unittest.mock.patch.object(sys, "stderr", io.StringIO()))
|
stack.enter_context(unittest.mock.patch.object(sys, "stderr", io.StringIO()))
|
||||||
sleep = stack.enter_context(unittest.mock.patch("time.sleep"))
|
sleep = stack.enter_context(unittest.mock.patch("time.sleep"))
|
||||||
with self.assertRaises(SystemExit) as stopped:
|
with self.assertRaises(SystemExit) as stopped:
|
||||||
|
|||||||
Reference in New Issue
Block a user