Carry the reload counters on argv, off the LOOP_CLAUDE_ names --help documents

This commit is contained in:
2026-09-25 17:35:24 +02:00
parent 2a2fff8fad
commit 11aad9b72c
4 changed files with 33 additions and 17 deletions
+23 -6
View File
@@ -8,6 +8,7 @@ import os
import pathlib
import stat
import subprocess
import sys
import tempfile
import unittest
import unittest.mock
@@ -309,18 +310,34 @@ class RunCycle(unittest.TestCase):
class Reload(unittest.TestCase):
def test_runs_the_edited_file_before_trusting_it(self):
def test_runs_the_edited_file_before_trusting_it_and_hands_it_the_counters_on_argv(self):
with tempfile.TemporaryDirectory() as root:
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"))
with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv:
loop.reload_if_edited(cycle_number=1, failures=0)
loop.reload_if_edited("do it", cycle_number=1, failures=0)
execv.assert_not_called()
open(script, "w").write(loop.RELOAD_BASELINE.replace("RELOAD_CYCLE = ", "RELOAD_CYCLE = \"x\" or "))
open(script, "w").write(loop.RELOAD_BASELINE.replace("RELOAD_FLAG = ", "RELOAD_FLAG = \"x\" or "))
environment = dict(os.environ)
with unittest.mock.patch.object(loop, "__file__", script), unittest.mock.patch("os.execv") as execv:
loop.reload_if_edited(cycle_number=1, failures=2)
execv.assert_called_once()
self.assertEqual((os.environ.pop(loop.RELOAD_CYCLE), os.environ.pop(loop.RELOAD_FAILURES)), ("1", "2"))
loop.reload_if_edited("do it", cycle_number=1, failures=2)
execv.assert_called_once_with(sys.executable, [sys.executable, script, loop.RELOAD_FLAG, "1", "2", "do it"])
self.assertEqual(dict(os.environ), environment)
class Main(unittest.TestCase):
def test_resumes_the_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")]
with contextlib.ExitStack() as stack:
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, "stdout", io.StringIO()))
mocks[0].return_value = os.getcwd()
mocks[3].side_effect = SystemExit(0)
with self.assertRaises(SystemExit):
loop.main()
mocks[2].assert_called_once_with("do it", 3, 2)
self.assertIn("=== cycle 4 ===", sys.stdout.getvalue())
if __name__ == "__main__":