test: capture worktree messages through the _cprint route
This commit is contained in:
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user