Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
@@ -10,7 +10,6 @@ import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
import unittest.mock
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
@@ -24,7 +23,7 @@ STAMP = f"<{NOW}>"
|
||||
|
||||
def clocked():
|
||||
"""The system's effects on a clock stopped at NOW, which renders a time as <seconds>."""
|
||||
return dataclasses.replace(system_effects(), local_time=lambda seconds: f"<{seconds}>", now=lambda: NOW)
|
||||
return dataclasses.replace(system_effects(pathlib.Path.home(), os.environ), local_time=lambda seconds: f"<{seconds}>", now=lambda: NOW)
|
||||
|
||||
|
||||
def ticks(pid):
|
||||
@@ -78,13 +77,10 @@ class Said(unittest.TestCase):
|
||||
|
||||
class LocalTime(unittest.TestCase):
|
||||
def test_renders_seconds_in_the_machines_zone(self):
|
||||
# A POSIX zone two hours ahead of UTC, so a time in UTC would read 14:13:20. Cleanups run last in, first out: the zone is restored before time.tzset makes the clock read it again.
|
||||
self.addCleanup(time.tzset)
|
||||
zone = unittest.mock.patch.dict(os.environ, {"TZ": "XXX-2"})
|
||||
zone.start()
|
||||
self.addCleanup(zone.stop)
|
||||
time.tzset()
|
||||
self.assertEqual(system_effects().local_time(NOW), "2026-09-21 16:13:20")
|
||||
# A POSIX zone two hours ahead of UTC, so a time in UTC would read 14:13:20. A process reads its zone as it starts, so the clock runs in one of its own.
|
||||
script = f"import os\nimport pathlib\nimport sys\nsys.path.insert(0, {str(pathlib.Path(__file__).resolve().parents[1])!r})\nfrom loop_effects.effects import system_effects\nprint(system_effects(pathlib.Path.home(), os.environ).local_time({NOW}))"
|
||||
ran = subprocess.run([sys.executable, "-c", script], capture_output=True, check=True, env=os.environ | {"TZ": "XXX-2"}, text=True)
|
||||
self.assertEqual(ran.stdout, "2026-09-21 16:13:20\n")
|
||||
|
||||
|
||||
|
||||
@@ -98,7 +94,7 @@ class ClaudeOnTerminal(unittest.TestCase):
|
||||
# A Ctrl-C on the terminal reaches every process in its foreground. kill returns once the signal is pending in the test's process, before claude exits and claude_on_terminal returns.
|
||||
claude.write_text(f"#!{sys.executable}\nimport os, signal, sys\nos.kill(os.getppid(), signal.SIGINT)\nsys.exit(3)\n")
|
||||
claude.chmod(0o755)
|
||||
code = system_effects().claude_on_terminal([], folder, {"PATH": folder})
|
||||
code = system_effects(pathlib.Path.home(), os.environ).claude_on_terminal([], folder, {"PATH": folder})
|
||||
self.assertEqual((code, caught, signal.getsignal(signal.SIGINT)), (3, [], before))
|
||||
|
||||
|
||||
@@ -110,27 +106,28 @@ class TmuxEffect(unittest.TestCase):
|
||||
tmux = pathlib.Path(folder, "tmux")
|
||||
tmux.write_text(f"#!{sys.executable}\nimport sys\nprint(' '.join(sys.argv[1:]))\nsys.exit(4)\n")
|
||||
tmux.chmod(0o755)
|
||||
return unittest.mock.patch.dict(os.environ, {"PATH": folder, "TMUX": "", "TMUX_PANE": ""})
|
||||
return system_effects(pathlib.Path.home(), os.environ | {"PATH": folder, "TMUX": "", "TMUX_PANE": ""})
|
||||
|
||||
def test_runs_tmux_and_returns_what_it_printed(self):
|
||||
with tempfile.TemporaryDirectory() as folder, self.fake_tmux(folder):
|
||||
done = system_effects().tmux(["list-windows", "-t", "=loop-proj"])
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
done = self.fake_tmux(folder).tmux(["list-windows", "-t", "=loop-proj"])
|
||||
self.assertEqual((done.returncode, done.stdout), (4, "list-windows -t =loop-proj\n"))
|
||||
|
||||
def test_reads_output_that_is_not_utf_8_with_each_bad_byte_replaced(self):
|
||||
with tempfile.TemporaryDirectory() as folder, self.fake_tmux(folder):
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
effects = self.fake_tmux(folder)
|
||||
pathlib.Path(folder, "tmux").write_text(f"#!{sys.executable}\nimport sys\nsys.stdout.buffer.write(b'caf\\xe9')\n")
|
||||
self.assertEqual(system_effects().tmux(["list-windows"]).stdout, "caf\ufffd")
|
||||
self.assertEqual(effects.tmux(["list-windows"]).stdout, "caf\ufffd")
|
||||
|
||||
def test_reads_a_tmux_that_cannot_start_as_exit_127(self):
|
||||
with tempfile.TemporaryDirectory() as folder, unittest.mock.patch.dict(os.environ, {"PATH": folder}):
|
||||
done = system_effects().tmux(["-V"])
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
done = system_effects(pathlib.Path.home(), os.environ | {"PATH": folder}).tmux(["-V"])
|
||||
self.assertEqual((done.args, done.returncode, done.stdout), (["tmux", "-V"], 127, ""))
|
||||
self.assertIn("could not start", done.stderr)
|
||||
|
||||
def test_runs_tmux_on_the_terminal_and_returns_its_exit_code(self):
|
||||
with tempfile.TemporaryDirectory() as folder, self.fake_tmux(folder), contextlib.redirect_stdout(io.StringIO()):
|
||||
self.assertEqual(system_effects().tmux_on_terminal(["attach-session", "-t", "=loop-proj"]), 4)
|
||||
with tempfile.TemporaryDirectory() as folder, contextlib.redirect_stdout(io.StringIO()):
|
||||
self.assertEqual(self.fake_tmux(folder).tmux_on_terminal(["attach-session", "-t", "=loop-proj"]), 4)
|
||||
|
||||
|
||||
class PythonEffect(unittest.TestCase):
|
||||
@@ -169,8 +166,8 @@ class ChildShells(unittest.TestCase):
|
||||
os.killpg(proc.pid, signal.SIGKILL)
|
||||
proc.wait()
|
||||
|
||||
def shell_of(self, proc):
|
||||
[found] = [each for each in child_shells.shells_of(os.getpid()) if each.pid == proc.pid]
|
||||
def shell_of(self, proc, slots=pathlib.Path("/no-slots")):
|
||||
[found] = [each for each in child_shells.shells_of(os.getpid(), slots, child_shells.children_of) if each.pid == proc.pid]
|
||||
return found
|
||||
|
||||
def test_reads_a_child_shells_command_and_whether_a_busy_tool_runs_below_it(self):
|
||||
@@ -189,12 +186,12 @@ class ChildShells(unittest.TestCase):
|
||||
|
||||
def test_a_shell_below_which_a_process_holds_a_heavy_slot_file_holds_a_slot_and_runs_no_busy_tool(self):
|
||||
# run-heavy replaces itself with its command, which keeps the slot's file open.
|
||||
with tempfile.TemporaryDirectory() as state, unittest.mock.patch.dict(os.environ, {"XDG_STATE_HOME": state}):
|
||||
with tempfile.TemporaryDirectory() as state:
|
||||
slot = pathlib.Path(state, "run-heavy", "slot-0")
|
||||
slot.parent.mkdir()
|
||||
holding = self.started(f"exec 9>{slot}; sleep 60; true")
|
||||
plain = self.started("sleep 60; true")
|
||||
self.assertEqual([(self.shell_of(each).holds_slot, self.shell_of(each).runs_busy_tool) for each in (holding, plain)], [(True, False), (False, False)])
|
||||
self.assertEqual([(self.shell_of(each, slot.parent).holds_slot, self.shell_of(each, slot.parent).runs_busy_tool) for each in (holding, plain)], [(True, False), (False, False)])
|
||||
|
||||
def test_a_shell_written_output_is_the_modified_time_of_its_output_file_and_else_its_start(self):
|
||||
with tempfile.NamedTemporaryFile() as output:
|
||||
@@ -264,8 +261,8 @@ class ChildShells(unittest.TestCase):
|
||||
self.assertFalse(child_shells.has_slot(2**30, pathlib.Path("/")))
|
||||
shell = self.started("sleep 60; true")
|
||||
real = child_shells.children_of
|
||||
with unittest.mock.patch.object(child_shells, "children_of", side_effect=lambda pid: [2**30, shell.pid] if pid == os.getpid() else real(pid)):
|
||||
self.assertEqual([each.pid for each in child_shells.shells_of(os.getpid())], [shell.pid])
|
||||
children = lambda pid: [2**30, shell.pid] if pid == os.getpid() else real(pid)
|
||||
self.assertEqual([each.pid for each in child_shells.shells_of(os.getpid(), pathlib.Path("/no-slots"), children)], [shell.pid])
|
||||
|
||||
def test_command_of_takes_the_text_of_the_last_eval_wrapper_and_else_the_whole_line(self):
|
||||
wrapped = ["/bin/bash", "-c", "source /s && eval 'echo a && eval b < /dev/null' < /dev/null && pwd -P >| /t"]
|
||||
@@ -330,7 +327,7 @@ class Timer(unittest.TestCase):
|
||||
self.assertTrue(ran.wait(10))
|
||||
|
||||
def test_the_systems_timer_runs_on_a_daemon_thread_so_a_loop_that_exits_while_it_waits_does_not_wait_for_it(self):
|
||||
self.assertIs(system_effects().timer, start_timer)
|
||||
self.assertIs(system_effects(pathlib.Path.home(), os.environ).timer, start_timer)
|
||||
before = set(threading.enumerate())
|
||||
stop = start_timer(60, lambda: None)
|
||||
self.addCleanup(stop)
|
||||
|
||||
Reference in New Issue
Block a user