Skip to content

Commit 236a672

Browse files
committed
Fix test_pre_prompt_running_loop
Correctly initialize a new PromptSession with pipe_input in the test to avoid AttributeError since the 'input' property is read-only.
1 parent d3b3db3 commit 236a672

1 file changed

Lines changed: 41 additions & 5 deletions

File tree

tests/test_cmd2.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3422,11 +3422,6 @@ def run_alert():
34223422
mock_app.invalidate.assert_called_once()
34233423

34243424

3425-
def test_async_alert_main_thread_error(base_app):
3426-
with pytest.raises(RuntimeError, match="main thread"):
3427-
base_app.async_alert("fail")
3428-
3429-
34303425
def test_async_alert_not_at_prompt(base_app):
34313426
import threading
34323427

@@ -3574,3 +3569,44 @@ def test_read_input_dynamic_prompt_with_history(base_app, monkeypatch):
35743569
result = prompt_arg()
35753570
assert isinstance(result, ANSI)
35763571
assert result.value == ANSI(base_app.prompt).value
3572+
3573+
3574+
def test_pre_prompt_running_loop(base_app):
3575+
# Test that pre_prompt runs with a running event loop.
3576+
import asyncio
3577+
3578+
from prompt_toolkit.input import create_pipe_input
3579+
from prompt_toolkit.output import DummyOutput
3580+
from prompt_toolkit.shortcuts import PromptSession
3581+
3582+
# Setup pipe input to feed data to prompt_toolkit
3583+
with create_pipe_input() as pipe_input:
3584+
# Create a new session with our pipe input because the input property is read-only
3585+
base_app.session = PromptSession(
3586+
input=pipe_input,
3587+
output=DummyOutput(),
3588+
history=base_app.session.history,
3589+
completer=base_app.session.completer,
3590+
)
3591+
3592+
loop_check = {'running': False}
3593+
3594+
def my_pre_prompt():
3595+
try:
3596+
asyncio.get_running_loop()
3597+
loop_check['running'] = True
3598+
except RuntimeError:
3599+
loop_check['running'] = False
3600+
3601+
base_app.pre_prompt = my_pre_prompt
3602+
3603+
# Feed input to exit prompt immediately
3604+
pipe_input.send_text("foo\n")
3605+
3606+
# Enable raw input and mock isatty to ensure self.session.prompt is used
3607+
base_app.use_rawinput = True
3608+
with mock.patch('sys.stdin.isatty', return_value=True):
3609+
# patch_stdout is used in this branch. It should work with DummyOutput/PipeInput.
3610+
base_app.read_input("prompt> ")
3611+
3612+
assert loop_check['running']

0 commit comments

Comments
 (0)