test: capture worktree messages through the _cprint route

This commit is contained in:
Teknium
2026-08-19 22:55:44 -07:00
parent 7b655fdf8d
commit 33e64fd83c

View File

@@ -28,8 +28,24 @@ class _Stub(CLICommandsMixin):
def _run(stub, command):
"""Capture handler output.
Worktree messages route through ``cli._cprint`` (the ANSI-aware
prompt_toolkit renderer) since the garbled-escape fix; its output binds
to the prompt_toolkit session output object, not ``sys.stdout``, so
``redirect_stdout`` alone cannot capture it. Mirror ``_cprint`` into the
same buffer to keep asserting the user-visible text.
"""
import re
from unittest.mock import patch as _patch
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
ansi = re.compile(r"\x1b\[[0-9;]*m")
def _fake_cprint(text):
buf.write(ansi.sub("", str(text)) + "\n")
with contextlib.redirect_stdout(buf), _patch.object(cli_mod, "_cprint", _fake_cprint):
stub._handle_worktree_command(command)
return buf.getvalue()