|
| 1 | +import asyncio |
| 2 | +from unittest.mock import patch |
| 3 | + |
1 | 4 | import pytest |
2 | 5 |
|
3 | 6 | from questionary.prompt import PromptParameterException |
4 | 7 | from questionary.prompt import prompt |
| 8 | +from questionary.prompt import prompt_async |
5 | 9 | from tests.utils import patched_prompt |
6 | 10 |
|
7 | 11 |
|
@@ -76,3 +80,53 @@ def test_print_with_name(): |
76 | 80 | questions = [{"name": "hello", "type": "print", "message": "Hello World"}] |
77 | 81 | result = patched_prompt(questions, "") |
78 | 82 | assert result == {"hello": None} |
| 83 | + |
| 84 | + |
| 85 | +@patch("builtins.print") |
| 86 | +@patch("questionary.prompt.unsafe_prompt", side_effect=KeyboardInterrupt) |
| 87 | +def test_no_keyboard_interrupt_message_in_prompt( |
| 88 | + mock_unsafe_prompt, mock_print |
| 89 | +) -> None: |
| 90 | + """ |
| 91 | + Test no message printed when `kbi_msg` is None in `prompt()`. |
| 92 | +
|
| 93 | + Args: |
| 94 | + mock_unsafe_prompt: A mock of the internal `unsafe_prompt()` call. |
| 95 | + raises a KeyboardInterrupt. |
| 96 | +
|
| 97 | + mock_print: A mock of Python's builtin `print()` function. |
| 98 | + """ |
| 99 | + # Act |
| 100 | + result = prompt(questions={}, kbi_msg=None) |
| 101 | + |
| 102 | + # Verify internal functions were properly called |
| 103 | + mock_unsafe_prompt.assert_called_once() # Raises KeyboardInterrupt |
| 104 | + mock_print.assert_not_called() |
| 105 | + |
| 106 | + # Verify result |
| 107 | + assert result == {} |
| 108 | + |
| 109 | + |
| 110 | +@patch("builtins.print") |
| 111 | +@patch("questionary.prompt.unsafe_prompt_async", side_effect=KeyboardInterrupt) |
| 112 | +def test_no_keyboard_interrupt_message_in_prompt_async( |
| 113 | + mock_unsafe_prompt_async, mock_print |
| 114 | +) -> None: |
| 115 | + """ |
| 116 | + Test no message printed when `kbi_msg` is None in `prompt_async()`. |
| 117 | +
|
| 118 | + Args: |
| 119 | + mock_unsafe_prompt_async: A mock of the internal `unsafe_prompt_async()` call. |
| 120 | + raises a KeyboardInterrupt. |
| 121 | +
|
| 122 | + mock_print: A mock of Python's builtin `print()` function. |
| 123 | + """ |
| 124 | + # Act |
| 125 | + result = asyncio.run(prompt_async(questions={}, kbi_msg=None)) |
| 126 | + |
| 127 | + # Verify internal functions were properly called |
| 128 | + mock_unsafe_prompt_async.assert_called_once() # Raises KeyboardInterrupt |
| 129 | + mock_print.assert_not_called() |
| 130 | + |
| 131 | + # Verify result |
| 132 | + assert result == {} |
0 commit comments