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
Provenance
- Commit
0d01686- Tree
9661fa2- Author
Seb Aebischer <8686939+saebischer@users.noreply.github.com>- Committer
David Lord <davidism@gmail.com>- Parent
0dc6d74
3 files changed+102 added−4 removed
modified:src/click/testing.py+25 added-4 removed
14 unmodified lines15 class EchoingStdin:16 def __init__(self, input, output):17 self._input = input18 self._output = output19+ self.paused = False20 21 def __getattr__(self, x):22 return getattr(self._input, x)23 24 def _echo(self, rv):25+ if not self.paused:24- self._output.write(rv)26+ self._output.write(rv)27 return rv28 29 def read(self, n=-1):30 return self._echo(self._input.read(n))31 32+ def read1(self, n=-1):33+ return self._echo(self._input.read1(n))34+35 def readline(self, n=-1):36 return self._echo(self._input.readline(n))37 38 def readlines(self):169 unmodified lines208 bytes_output = io.BytesIO()209 210 if self.echo_stdin:211 input = EchoingStdin(input, bytes_output)212+ echo_input = input213 214 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 = 1221+222 sys.stdout = _NamedTextIOWrapper(223 bytes_output, encoding=self.charset, name="<stdout>", mode="w"224 )225 12 unmodified lines238 239 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 val246 247 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 val256 257 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 char267 268 default_color = color269 170 unmodified linesmodified:tests/test_testing.py+75 added
24 unmodified lines25 result = runner.invoke(test, input="Hello World!\n")26 assert not result.exception27 assert result.output == "Hello World!\n"28 29+30+def 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()41+42 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"46 47 48+def test_echo_stdin_prompts():49+ @click.command()50+ def test_python_input():51+ foo = input("Foo: ")52+ click.echo(f"foo={foo}")53+54+ 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"58+59+ @click.command()60+ @click.option("--foo", prompt=True)61+ def test_prompt(foo):62+ click.echo(f"foo={foo}")63+64+ 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"68+69+ @click.command()70+ @click.option("--foo", prompt=True, hide_input=True)71+ def test_hidden_prompt(foo):72+ click.echo(f"foo={foo}")73+74+ 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"78+79+ @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}")84+85+ 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"89+90+91 def test_runner_with_stream():92 @click.command()93 def test():94 i = click.get_binary_stream("stdin")47 unmodified lines142 result = runner.invoke(continue_it, input="y")143 assert not result.exception144 assert result.output == "y\n"145 146+ runner = CliRunner(echo_stdin=True)147+ result = runner.invoke(continue_it, input="y")148+ assert not result.exception149+ assert result.output == "y\n"150 151+ @click.command()152+ def getchar_echo():153+ click.echo(click.getchar(echo=True))154+155+ runner = CliRunner()156+ result = runner.invoke(getchar_echo, input="y")157+ assert not result.exception158+ assert result.output == "yy\n"159+160+ runner = CliRunner(echo_stdin=True)161+ result = runner.invoke(getchar_echo, input="y")162+ assert not result.exception163+ assert result.output == "yy\n"164+165+166 def test_catch_exceptions():167 class CustomError(Exception):168 pass169 254 unmodified linesmodified:CHANGES.rst+2 added
206 unmodified lines207 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`213 214 215 Version 7.1.2216 -------------635 unmodified lines