Skip to content
Myr

fix output when using echo_stdin

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

echo calls to read1 pause echo when using hidden input prompt and getchar don't buffer reads to avoid echoing early

authored by Seb Aebischer · committed by David Lord · parent 0dc6d74 · Browse files · 3 files+102 added−4 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+102 added−4 removed

  • tests:+75 added−0 removed
  • other:+27 added−4 removed

3 files+102 added−4 removed

modified:src/click/testing.py+25 added-4 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)27 return rv2829 def read(self, n=-1):30 return self._echo(self._input.read(n))3132 def read1(self, n=-1):33 return self._echo(self._input.read1(n))3435 def readline(self, n=-1):36 return self._echo(self._input.readline(n))3738 def readlines(self):
169 unmodified lines
208 bytes_output = io.BytesIO()209210 if self.echo_stdin:211 input = EchoingStdin(input, bytes_output)212 echo_input = input213214 sys.stdin = input = _NamedTextIOWrapper(215 input, encoding=self.charset, name="<stdin>", mode="r"216 )217 if self.echo_stdin:218 # Force unbuffered reads, otherwise the underlying EchoingStdin219 # stream will echo a big chunk of input on the first read.220 input._CHUNK_SIZE = 1221222 sys.stdout = _NamedTextIOWrapper(223 bytes_output, encoding=self.charset, name="<stdout>", mode="w"224 )225
12 unmodified lines
238239 def visible_input(prompt=None):240 sys.stdout.write(prompt or "")241 val = input.readline().rstrip("\r\n")242 if not self.echo_stdin:231 sys.stdout.write(f"{val}\n")243 sys.stdout.write(f"{val}\n")244 sys.stdout.flush()245 return val246247 def hidden_input(prompt=None):248 sys.stdout.write(f"{prompt or ''}\n")249 sys.stdout.flush()250 if self.echo_stdin:251 echo_input.paused = True238 return input.readline().rstrip("\r\n")252 val = input.readline().rstrip("\r\n")253 if self.echo_stdin:254 echo_input.paused = False255 return val256257 def _getchar(echo):258 if not echo and self.echo_stdin:259 echo_input.paused = True260 char = sys.stdin.read(1)242 if echo:261 if echo and not self.echo_stdin:262 sys.stdout.write(char)263 sys.stdout.flush()264 elif not echo and self.echo_stdin:265 echo_input.paused = False266 return char267268 default_color = color269
170 unmodified lines
modified:tests/test_testing.py+75 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="wau wau\n")56 assert not result.exception57 assert result.output == "Foo: wau wau\nfoo=wau wau\n"5859 @click.command()60 @click.option("--foo", prompt=True)61 def test_prompt(foo):62 click.echo(f"foo={foo}")6364 runner = CliRunner(echo_stdin=True)65 result = runner.invoke(test_prompt, input="wau wau\n")66 assert not result.exception67 assert result.output == "Foo: wau wau\nfoo=wau wau\n"6869 @click.command()70 @click.option("--foo", prompt=True, hide_input=True)71 def test_hidden_prompt(foo):72 click.echo(f"foo={foo}")7374 runner = CliRunner(echo_stdin=True)75 result = runner.invoke(test_hidden_prompt, input="wau wau\n")76 assert not result.exception77 assert result.output == "Foo: \nfoo=wau wau\n"7879 @click.command()80 @click.option("--foo", prompt=True)81 @click.option("--bar", prompt=True)82 def test_multiple_prompts(foo, bar):83 click.echo(f"foo={foo}, bar={bar}")8485 runner = CliRunner(echo_stdin=True)86 result = runner.invoke(test_multiple_prompts, input="one\ntwo\n")87 assert not result.exception88 assert result.output == "Foo: one\nBar: two\nfoo=one, bar=two\n"899091def test_runner_with_stream():92 @click.command()93 def test():94 i = click.get_binary_stream("stdin")
47 unmodified lines
142 result = runner.invoke(continue_it, input="y")143 assert not result.exception144 assert result.output == "y\n"145146 runner = CliRunner(echo_stdin=True)147 result = runner.invoke(continue_it, input="y")148 assert not result.exception149 assert result.output == "y\n"150151 @click.command()152 def getchar_echo():153 click.echo(click.getchar(echo=True))154155 runner = CliRunner()156 result = runner.invoke(getchar_echo, input="y")157 assert not result.exception158 assert result.output == "yy\n"159160 runner = CliRunner(echo_stdin=True)161 result = runner.invoke(getchar_echo, input="y")162 assert not result.exception163 assert result.output == "yy\n"164165166def test_catch_exceptions():167 class CustomError(Exception):168 pass169
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