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
Provenance
- Commit
30e7f76- Tree
5868e36- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
0dc6d74- Parent 2
f6f8976
3 files changed+105 added−3 removed
modified:src/click/testing.py+31 added-3 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+28 return rv29 30 def read(self, n=-1):31 return self._echo(self._input.read(n))32 33+ def read1(self, n=-1):34+ return self._echo(self._input.read1(n))35+36 def readline(self, n=-1):37 return self._echo(self._input.readline(n))38 39 def readlines(self):5 unmodified lines45 def __repr__(self):46 return repr(self._input)47 48 49+@contextlib.contextmanager50+def _pause_echo(stream):51+ if stream is None:52+ yield53+ else:54+ stream._paused = True55+ yield56+ stream._paused = False57+58+59 class _NamedTextIOWrapper(io.TextIOWrapper):60 def __init__(self, buffer, name=None, mode=None, **kwargs):61 super().__init__(buffer, **kwargs)62 self._name = name143 unmodified lines206 .. versionchanged:: 4.0207 Added the ``color`` parameter.208 """209 input = make_input_stream(input, self.charset)210+ echo_input = None211 212 old_stdin = sys.stdin213 old_stdout = sys.stdout214 old_stderr = sys.stderr4 unmodified lines219 220 bytes_output = io.BytesIO()221 222 if self.echo_stdin:206- input = EchoingStdin(input, bytes_output)223+ input = echo_input = EchoingStdin(input, bytes_output)224 225 sys.stdin = input = _NamedTextIOWrapper(226 input, encoding=self.charset, name="<stdin>", mode="r"227 )228+229+ if self.echo_stdin:230+ # Force unbuffered reads, otherwise TextIOWrapper reads a231+ # large chunk which is echoed early.232+ input._CHUNK_SIZE = 1233+234 sys.stdout = _NamedTextIOWrapper(235 bytes_output, encoding=self.charset, name="<stdout>", mode="w"236 )237 9 unmodified lines247 mode="w",248 errors="backslashreplace",249 )250 251+ @_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 val258 259+ @_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")264 265+ @_pause_echo(echo_input)266 def _getchar(echo):267 char = sys.stdin.read(1)268+269 if echo:270 sys.stdout.write(char)271+244- sys.stdout.flush()272+ sys.stdout.flush()273 return char274 275 default_color = color276 170 unmodified linesmodified:tests/test_testing.py+72 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="bar bar\n")56+ assert not result.exception57+ assert result.output == "Foo: bar bar\nfoo=bar bar\n"58+59+ @click.command()60+ @click.option("--foo", prompt=True)61+ def test_prompt(foo):62+ click.echo(f"foo={foo}")63+64+ result = runner.invoke(test_prompt, input="bar bar\n")65+ assert not result.exception66+ assert result.output == "Foo: bar bar\nfoo=bar bar\n"67+68+ @click.command()69+ @click.option("--foo", prompt=True, hide_input=True)70+ def test_hidden_prompt(foo):71+ click.echo(f"foo={foo}")72+73+ result = runner.invoke(test_hidden_prompt, input="bar bar\n")74+ assert not result.exception75+ assert result.output == "Foo: \nfoo=bar bar\n"76+77+ @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}")82+83+ 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"86+87+88 def test_runner_with_stream():89 @click.command()90 def test():91 i = click.get_binary_stream("stdin")47 unmodified lines139 result = runner.invoke(continue_it, input="y")140 assert not result.exception141 assert result.output == "y\n"142 143+ runner = CliRunner(echo_stdin=True)144+ result = runner.invoke(continue_it, input="y")145+ assert not result.exception146+ assert result.output == "y\n"147 148+ @click.command()149+ def getchar_echo():150+ click.echo(click.getchar(echo=True))151+152+ runner = CliRunner()153+ result = runner.invoke(getchar_echo, input="y")154+ assert not result.exception155+ assert result.output == "yy\n"156+157+ runner = CliRunner(echo_stdin=True)158+ result = runner.invoke(getchar_echo, input="y")159+ assert not result.exception160+ assert result.output == "yy\n"161+162+163 def test_catch_exceptions():164 class CustomError(Exception):165 pass166 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