Raise an exception on end of input in CliRunner
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by kyu243 · committed by David Lord · parent cfa6f4a · Browse files · 5 files·+27 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+27 added−6 removed
- tests:+17 added−4 removed
- other:+10 added−2 removed
Provenance
- Commit
80efdf6- Tree
5fe50d6- Author
kyu243 <kieranyangyu@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
cfa6f4a
5 files changed+27 added−6 removed
modified:src/click/testing.py+9 added-2 removed
114 unmodified lines115 @property116 def mode(self) -> str:117 return self._mode118 119+ def __next__(self) -> str: # type: ignore120+ try:121+ line = super().__next__()122+ except StopIteration as e:123+ raise EOFError() from e124+ return line125 126+127 def make_input_stream(128 input: str | bytes | t.IO[t.Any] | None, charset: str129 ) -> t.BinaryIO:130 # Is already an input stream.216 unmodified lines347 348 @_pause_echo(echo_input) # type: ignore349 def visible_input(prompt: str | None = None) -> str:350 sys.stdout.write(prompt or "")344- val = text_input.readline().rstrip("\r\n")351+ val = next(text_input).rstrip("\r\n")352 sys.stdout.write(f"{val}\n")353 sys.stdout.flush()354 return val355 356 @_pause_echo(echo_input) # type: ignore357 def hidden_input(prompt: str | None = None) -> str:358 sys.stdout.write(f"{prompt or ''}\n")359 sys.stdout.flush()353- return text_input.readline().rstrip("\r\n")360+ return next(text_input).rstrip("\r\n")361 362 @_pause_echo(echo_input) # type: ignore363 def _getchar(echo: bool) -> str:364 char = sys.stdin.read(1)201 unmodified linesmodified:tests/test_chain.py+2 added-2 removed
161 unmodified lines162 163 return processor164 165 result = runner.invoke(cli, args, input=input)166- assert not result.exception167- assert result.output.splitlines() == expect166+ # last two lines are '' and 'Aborted!'167+ assert result.output.splitlines()[:-2] == expect168 169 170 def test_args_and_chain(runner):171 @click.group(chain=True)74 unmodified linesmodified:tests/test_termui.py+2 added-2 removed
240 unmodified lines241 @click.option("-f", default=__file__, prompt="file", type=click.File(**file_kwargs))242 def cli(f):243 click.echo(f.name)244 245- result = runner.invoke(cli)245+ result = runner.invoke(cli, input="\n")246 assert result.output == f"file [{__file__}]: \n{__file__}\n"247 248 249 def test_secho(runner):200 unmodified lines450 ("prompt", "input", "default", "expect"),451 [452 (True, "password\npassword", None, "password"),453 ("Confirm Password", "password\npassword\n", None, "password"),454- (True, "", "", ""),454+ (True, "\n\n", "", ""),455 (False, None, None, None),456 ],457 )458 def test_confirmation_prompt(runner, prompt, input, default, expect):29 unmodified linesmodified:tests/test_utils.py+13 added
177 unmodified lines178 out, err = capsys.readouterr()179 assert out == "Password:\ninterrupted\n"180 181 182+def test_prompts_eof(runner):183+ """If too few lines of input are given, prompt should exit, not hang."""184+185+ @click.command186+ def echo():187+ for _ in range(3):188+ click.echo(click.prompt("", type=int))189+190+ # only provide two lines of input for three prompts191+ result = runner.invoke(echo, input="1\n2\n")192+ assert result.exit_code == 1193+194+195 def _test_gen_func():196 yield "a"197 yield "b"198 yield "c"473 unmodified linesmodified:CHANGES.rst+1 added
5 unmodified lines6 - Fix flag value handling for flag options with a provided type. :issue:`2894`7 :issue:`2897` :pr:`2930`8 - Fix shell completion for nested groups. :issue:`2906` :pr:`2907`9 - Flush ``sys.stderr`` at the end of ``CliRunner.invoke``. :issue:`2682`10+- Fix EOF handling for stdin input in CliRunner. :issue:`2787`11 12 Version 8.2.013 -------------14 1231 unmodified lines