Skip to content
Myr

Merge pull request #1820 from saebischer/fix-clirunner-echo_stdin

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix issues with CliRunner's echo_stdin

authored by David Lord · committed by GitHub · parents 0dc6d74, f6f8976 · Browse files · 3 files+105 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+105 added−3 removed

  • tests:+72 added−0 removed
  • other:+33 added−3 removed

3 files+105 added−3 removed

modified:src/click/testing.py+31 added-3 removed
14 unmodified lines
15class EchoingStdin:16 def __init__(self, input, output):17 self._input = input18 self._output = output19 self._paused = False2021 def __getattr__(self, x):22 return getattr(self._input, x)2324 def _echo(self, rv):25 if not self._paused:24 self._output.write(rv)26 self._output.write(rv)2728 return rv2930 def read(self, n=-1):31 return self._echo(self._input.read(n))3233 def read1(self, n=-1):34 return self._echo(self._input.read1(n))3536 def readline(self, n=-1):37 return self._echo(self._input.readline(n))3839 def readlines(self):
5 unmodified lines
45 def __repr__(self):46 return repr(self._input)474849@contextlib.contextmanager50def _pause_echo(stream):51 if stream is None:52 yield53 else:54 stream._paused = True55 yield56 stream._paused = False575859class _NamedTextIOWrapper(io.TextIOWrapper):60 def __init__(self, buffer, name=None, mode=None, **kwargs):61 super().__init__(buffer, **kwargs)62 self._name = name
143 unmodified lines
206 .. versionchanged:: 4.0207 Added the ``color`` parameter.208 """209 input = make_input_stream(input, self.charset)210 echo_input = None211212 old_stdin = sys.stdin213 old_stdout = sys.stdout214 old_stderr = sys.stderr
4 unmodified lines
219220 bytes_output = io.BytesIO()221222 if self.echo_stdin:206 input = EchoingStdin(input, bytes_output)223 input = echo_input = EchoingStdin(input, bytes_output)224225 sys.stdin = input = _NamedTextIOWrapper(226 input, encoding=self.charset, name="<stdin>", mode="r"227 )228229 if self.echo_stdin:230 # Force unbuffered reads, otherwise TextIOWrapper reads a231 # large chunk which is echoed early.232 input._CHUNK_SIZE = 1233234 sys.stdout = _NamedTextIOWrapper(235 bytes_output, encoding=self.charset, name="<stdout>", mode="w"236 )237
9 unmodified lines
247 mode="w",248 errors="backslashreplace",249 )250251 @_pause_echo(echo_input)252 def visible_input(prompt=None):253 sys.stdout.write(prompt or "")254 val = input.readline().rstrip("\r\n")255 sys.stdout.write(f"{val}\n")256 sys.stdout.flush()257 return val258259 @_pause_echo(echo_input)260 def hidden_input(prompt=None):261 sys.stdout.write(f"{prompt or ''}\n")262 sys.stdout.flush()263 return input.readline().rstrip("\r\n")264265 @_pause_echo(echo_input)266 def _getchar(echo):267 char = sys.stdin.read(1)268269 if echo:270 sys.stdout.write(char)271244 sys.stdout.flush()272 sys.stdout.flush()273 return char274275 default_color = color276
170 unmodified lines
modified:tests/test_testing.py+72 added
24 unmodified lines
25 result = runner.invoke(test, input="Hello World!\n")26 assert not result.exception27 assert result.output == "Hello World!\n"282930def test_echo_stdin_stream():31 @click.command()32 def test():33 i = click.get_binary_stream("stdin")34 o = click.get_binary_stream("stdout")35 while 1:36 chunk = i.read(4096)37 if not chunk:38 break39 o.write(chunk)40 o.flush()4142 runner = CliRunner(echo_stdin=True)43 result = runner.invoke(test, input="Hello World!\n")44 assert not result.exception45 assert result.output == "Hello World!\nHello World!\n"464748def test_echo_stdin_prompts():49 @click.command()50 def test_python_input():51 foo = input("Foo: ")52 click.echo(f"foo={foo}")5354 runner = CliRunner(echo_stdin=True)55 result = runner.invoke(test_python_input, input="bar bar\n")56 assert not result.exception57 assert result.output == "Foo: bar bar\nfoo=bar bar\n"5859 @click.command()60 @click.option("--foo", prompt=True)61 def test_prompt(foo):62 click.echo(f"foo={foo}")6364 result = runner.invoke(test_prompt, input="bar bar\n")65 assert not result.exception66 assert result.output == "Foo: bar bar\nfoo=bar bar\n"6768 @click.command()69 @click.option("--foo", prompt=True, hide_input=True)70 def test_hidden_prompt(foo):71 click.echo(f"foo={foo}")7273 result = runner.invoke(test_hidden_prompt, input="bar bar\n")74 assert not result.exception75 assert result.output == "Foo: \nfoo=bar bar\n"7677 @click.command()78 @click.option("--foo", prompt=True)79 @click.option("--bar", prompt=True)80 def test_multiple_prompts(foo, bar):81 click.echo(f"foo={foo}, bar={bar}")8283 result = runner.invoke(test_multiple_prompts, input="one\ntwo\n")84 assert not result.exception85 assert result.output == "Foo: one\nBar: two\nfoo=one, bar=two\n"868788def test_runner_with_stream():89 @click.command()90 def test():91 i = click.get_binary_stream("stdin")
47 unmodified lines
139 result = runner.invoke(continue_it, input="y")140 assert not result.exception141 assert result.output == "y\n"142143 runner = CliRunner(echo_stdin=True)144 result = runner.invoke(continue_it, input="y")145 assert not result.exception146 assert result.output == "y\n"147148 @click.command()149 def getchar_echo():150 click.echo(click.getchar(echo=True))151152 runner = CliRunner()153 result = runner.invoke(getchar_echo, input="y")154 assert not result.exception155 assert result.output == "yy\n"156157 runner = CliRunner(echo_stdin=True)158 result = runner.invoke(getchar_echo, input="y")159 assert not result.exception160 assert result.output == "yy\n"161162163def test_catch_exceptions():164 class CustomError(Exception):165 pass166
254 unmodified lines
modified:CHANGES.rst+2 added
206 unmodified lines
207 Fixes an inconsistency when nesting ``Context.forward()`` calls.208 :issue:`1568`209- The ``MultiCommand.resultcallback`` decorator is renamed to210 ``result_callback``. The old name is deprecated. :issue:`1160`211- Fix issues with ``CliRunner`` output when using ``echo_stdin=True``.212 :issue:`1101`213214215Version 7.1.2216-------------
635 unmodified lines