def feed_cli_with_input(_type, message, text, **kwargs): """ Create a Prompt, feed it with the given user input and return the CLI object. This returns a (result, Application) tuple. """ inp = create_pipe_input() try: inp.send_text(text) prompter = prompt_by_name(_type) application = prompter(message, input=inp, output=DummyOutput(), **kwargs) result = application.unsafe_ask() return result, application finally: inp.close()
def test_validation(self, mocked_validate): def _hello(): filepath_prompt._session.app.exit(result="hello") mocked_validate.side_effect = _hello self.inp.send_text("hello\n") filepath_prompt = FilePathPrompt( message="fooboo", style=InquirerPyStyle({"qmark": ""}), default=".vim", validate=PathValidator(), input=self.inp, output=DummyOutput(), ) result = filepath_prompt.execute() mocked_validate.assert_called_once() self.assertEqual(result, "hello") self.assertEqual(filepath_prompt.status["answered"], False) self.assertEqual(filepath_prompt.status["result"], None)
def feed_cli_with_input(_type, message, texts, sleep_time=1, **kwargs): """ Create a Prompt, feed it with the given user input and return the CLI object. You an provide multiple texts, the feeder will async sleep for `sleep_time` This returns a (result, Application) tuple. """ if not isinstance(texts, list): texts = [texts] inp = create_pipe_input() try: prompter = prompt_by_name(_type) application = prompter(message, input=inp, output=DummyOutput(), **kwargs) if is_prompt_toolkit_3(): loop = asyncio.new_event_loop() future_result = loop.create_task(application.unsafe_ask_async()) for i, text in enumerate(texts): # noinspection PyUnresolvedReferences inp.send_text(text) if i != len(texts) - 1: loop.run_until_complete(asyncio.sleep(sleep_time)) result = loop.run_until_complete(future_result) else: for text in texts: inp.send_text(text) result = application.unsafe_ask() return result, application finally: inp.close()
def _feed_cli_with_input(text, editing_mode=EditingMode.EMACS, clipboard=None, history=None, multiline=False, check_line_ending=True, pre_run_callback=None): """ Create a CommandLineInterface, feed it with the given user input and return the CLI object. This returns a (result, CLI) tuple. """ # If the given text doesn't end with a newline, the interface won't finish. if check_line_ending: assert text.endswith('\n') loop = PosixEventLoop() try: inp = PipeInput() inp.send_text(text) cli = CommandLineInterface(application=Application( buffer=Buffer(accept_action=AcceptAction.RETURN_DOCUMENT, history=history, is_multiline=multiline), editing_mode=editing_mode, clipboard=clipboard or InMemoryClipboard(), key_bindings_registry=KeyBindingManager.for_prompt().registry, ), eventloop=loop, input=inp, output=DummyOutput()) if pre_run_callback: pre_run_callback(cli) result = cli.run() return result, cli finally: loop.close() inp.close()
def test_prompt_result(self): self.inp.send_text("what\n") secret_prompt = SecretPrompt( message="hello", style=InquirerPyStyle({"answer": ""}), default="yes", qmark="~", vi_mode=False, input=self.inp, output=DummyOutput(), ) result = secret_prompt.execute() self.assertEqual(result, "yeswhat") self.assertEqual( secret_prompt.status, { "answered": True, "result": "yeswhat", "skipped": False }, )
def test_blank_line_fix(): def get_prompt_tokens(): return [("class:question", "What is your favourite letter?")] ic = InquirerControl(["a", "b", "c"]) inp = create_pipe_input() try: inp.send_text("") layout = common.create_inquirer_layout(ic, get_prompt_tokens, input=inp, output=DummyOutput()) # usually this would be 2000000000000000000000000000000 # but `common._fix_unecessary_blank_lines` makes sure # the main window is not as greedy (avoiding blank lines) assert (layout.container.preferred_height( 100, 200).max == 1000000000000000000000000000001) finally: inp.close()
def _feed_cli_with_input( text, editing_mode=EditingMode.EMACS, clipboard=None, history=None, multiline=False, check_line_ending=True, key_bindings=None, ): """ Create a Prompt, feed it with the given user input and return the CLI object. This returns a (result, Application) tuple. """ # If the given text doesn't end with a newline, the interface won't finish. if check_line_ending: assert text.endswith("\r") inp = create_pipe_input() try: inp.send_text(text) session = PromptSession( input=inp, output=DummyOutput(), editing_mode=editing_mode, history=history, multiline=multiline, clipboard=clipboard, key_bindings=key_bindings, ) result = session.prompt() return session.default_buffer.document, session.app finally: inp.close()
def test_print_with_style(monkeypatch): mock = Mock(return_value=None) monkeypatch.setattr(DummyOutput, "write", mock.write) monkeypatch.setattr(DummyOutput, "set_attributes", mock.set_attributes) print_formatted_text("Hello World", style="bold italic fg:darkred", output=DummyOutput()) assert len(mock.method_calls) == 4 assert mock.method_calls[0][0] == "set_attributes" assert mock.method_calls[0][1][0] == Attrs( color="8b0000", bgcolor="", bold=True, underline=False, italic=True, blink=False, reverse=False, hidden=False, ) assert mock.method_calls[1][0] == "write" assert mock.method_calls[1][1][0] == "Hello World"
def _feed_cli_with_input(text, editing_mode=EditingMode.EMACS): """ Create a CommandLineInterface, feed it with the given user input and return the CLI object. This returns a (result, CLI) tuple. """ # If the given text doesn't end with a newline, the interface won't finish. assert text.endswith('\n') loop = PosixEventLoop() try: inp = PipeInput() inp.send_text(text) cli = CommandLineInterface( application=Application(editing_mode=editing_mode), eventloop=loop, input=inp, output=DummyOutput()) result = cli.run() return result, cli finally: loop.close() inp.close()
def __init__(self): super(DummyApplication, self).__init__(output=DummyOutput(), input=DummyInput())
def editor(): return Editor(output=DummyOutput(), input=DummyInput())
def __init__(self) -> None: super().__init__(output=DummyOutput(), input=DummyInput())
def application_init_fake(self, *k, **kw): kw.pop("input", None) kw.pop("output", None) application_init(self, input=inp, output=DummyOutput(), *k, **kw)
def prompt_session_init_fake(self, *k, **kw): prompt_session_init(self, input=inp, output=DummyOutput(), *k, **kw)
def init(self): self.input = PipeInput() output = DummyOutput() # self.shell = Shell(input=self.input, output=output) self.processor = InputProcessor