|
1 | 1 | """Cmd2 unit/functional testing""" |
2 | 2 |
|
3 | 3 | import contextlib |
| 4 | +import contextvars |
4 | 5 | import io |
5 | 6 | import os |
6 | 7 | import signal |
|
17 | 18 | from unittest import mock |
18 | 19 |
|
19 | 20 | import pytest |
20 | | -from prompt_toolkit.application import create_app_session, get_app |
| 21 | +from prompt_toolkit.application import Application, create_app_session, get_app |
21 | 22 | from prompt_toolkit.auto_suggest import AutoSuggestFromHistory |
22 | 23 | from prompt_toolkit.completion import DummyCompleter |
| 24 | +from prompt_toolkit.data_structures import Size |
23 | 25 | from prompt_toolkit.input import DummyInput, create_pipe_input |
24 | 26 | from prompt_toolkit.output import DummyOutput |
| 27 | +from prompt_toolkit.output.vt100 import Vt100_Output |
25 | 28 | from prompt_toolkit.shortcuts import CompleteStyle, PromptSession |
26 | 29 | from rich.style import Style |
27 | 30 | from rich.text import Text |
@@ -4965,6 +4968,80 @@ def get_rprompt(self): |
4965 | 4968 | assert True not in drawn_while_done, "the right prompt was drawn into the committed final frame" |
4966 | 4969 |
|
4967 | 4970 |
|
| 4971 | +def test_set_window_title_is_flushed_during_a_command() -> None: |
| 4972 | + stream = io.StringIO() |
| 4973 | + output = Vt100_Output(stream, lambda: Size(rows=24, columns=80), enable_cpr=False) |
| 4974 | + observed = [] |
| 4975 | + |
| 4976 | + class TitleApp(cmd2.Cmd): |
| 4977 | + def do_work(self, _statement): |
| 4978 | + self.set_window_title("Working…") |
| 4979 | + observed.append(stream.getvalue()) |
| 4980 | + |
| 4981 | + with create_app_session(input=DummyInput(), output=output): |
| 4982 | + app = TitleApp(allow_cli_args=False) |
| 4983 | + app.onecmd_plus_hooks("work") |
| 4984 | + |
| 4985 | + assert observed == ["\x1b]2;Working…\x07"] |
| 4986 | + |
| 4987 | + |
| 4988 | +@pytest.mark.parametrize("from_worker", [False, True]) |
| 4989 | +def test_set_window_title_while_a_prompt_is_running(from_worker) -> None: |
| 4990 | + stream = io.StringIO() |
| 4991 | + title_threads = [] |
| 4992 | + observed = [] |
| 4993 | + workers = [] |
| 4994 | + loop_thread = threading.get_ident() |
| 4995 | + |
| 4996 | + class TitleOutput(Vt100_Output): |
| 4997 | + def set_title(self, title): |
| 4998 | + title_threads.append(threading.get_ident()) |
| 4999 | + super().set_title(title) |
| 5000 | + |
| 5001 | + output = TitleOutput(stream, lambda: Size(rows=24, columns=80), enable_cpr=False) |
| 5002 | + with create_pipe_input() as pipe_input, create_app_session(input=pipe_input, output=output): |
| 5003 | + app = Application(input=pipe_input, output=output) |
| 5004 | + sent = False |
| 5005 | + |
| 5006 | + def check_title(): |
| 5007 | + # Observe before exiting, because the final render also flushes output. |
| 5008 | + observed.append("\x1b]2;Working…\x07" in stream.getvalue()) |
| 5009 | + app.exit() |
| 5010 | + |
| 5011 | + def update_title(): |
| 5012 | + cmd2.Cmd.set_window_title("Working…") |
| 5013 | + app.loop.call_soon_threadsafe(check_title) |
| 5014 | + |
| 5015 | + def after_render(_sender): |
| 5016 | + nonlocal sent |
| 5017 | + if sent: |
| 5018 | + return |
| 5019 | + sent = True |
| 5020 | + # Start after the first frame, so its flush cannot hide a buffered title. |
| 5021 | + if from_worker: |
| 5022 | + context = contextvars.copy_context() |
| 5023 | + worker = threading.Thread(target=context.run, args=(update_title,), daemon=True) |
| 5024 | + workers.append(worker) |
| 5025 | + worker.start() |
| 5026 | + else: |
| 5027 | + update_title() |
| 5028 | + |
| 5029 | + app.after_render += after_render |
| 5030 | + app.run(pre_run=lambda: app.loop.call_later(5, app.exit), set_exception_handler=False) |
| 5031 | + |
| 5032 | + for worker in workers: |
| 5033 | + worker.join(timeout=5) |
| 5034 | + assert not worker.is_alive() |
| 5035 | + |
| 5036 | + assert observed == [True] |
| 5037 | + assert title_threads == [loop_thread] |
| 5038 | + |
| 5039 | + |
| 5040 | +def test_set_window_title_without_a_terminal() -> None: |
| 5041 | + with create_app_session(input=DummyInput(), output=DummyOutput()): |
| 5042 | + cmd2.Cmd.set_window_title("Working…") |
| 5043 | + |
| 5044 | + |
4968 | 5045 | def test_get_bottom_toolbar(base_app: cmd2.Cmd) -> None: |
4969 | 5046 | assert base_app.get_bottom_toolbar() is None |
4970 | 5047 |
|
|
0 commit comments