Skip to content
Myr

testing/CliRunner: Fix regression related to EOF introduced in 262bdf0

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

Commit 262bdf0 ensured to raise an EOFError exception on end of input to simulate tty behavior and avoid blocking prompt during tests when no more input is available. However the introduced implementation has a side effect when testing a click command having a File type option or argument and when it is set to stdin: the command ends up with error due to the Abort exception being raised when the stdin EOFError exception is caught. To prevent this undesirable side effect, prefer to raise the EOFError exceptions directly from the prompts functions inside the CliRunner class instead of doing it in the method overriding the iterator protcol for the _NamedTextIOWrapper class. Restore previous implementation of a test broken by changes of 262bdf0. Fixes #2939.

authored by Antoine Lambert · committed by Antoine Lambert · parent b63239d · Browse files · 3 files+15 added−11 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+15 added−11 removed

  • tests:+2 added−2 removed
  • other:+13 added−9 removed

3 files+15 added−11 removed

modified:src/click/testing.py+8 added-9 removed
114 unmodified lines
115 @property116 def mode(self) -> str:117 return self._mode118119 def __next__(self) -> str: # type: ignore120 try:121 line = super().__next__()122 except StopIteration as e:123 raise EOFError() from e124 return line119126120def make_input_stream(121 input: str | bytes | t.IO[t.Any] | None, charset: str122) -> t.BinaryIO:123 # Is already an input stream.
216 unmodified lines
340341 @_pause_echo(echo_input) # type: ignore342 def visible_input(prompt: str | None = None) -> str:343 sys.stdout.write(prompt or "")344 try:351 val = next(text_input).rstrip("\r\n")345 val = next(text_input).rstrip("\r\n")346 except StopIteration as e:347 raise EOFError() from e348 sys.stdout.write(f"{val}\n")349 sys.stdout.flush()350 return val351352 @_pause_echo(echo_input) # type: ignore353 def hidden_input(prompt: str | None = None) -> str:354 sys.stdout.write(f"{prompt or ''}\n")355 sys.stdout.flush()356 try:360 return next(text_input).rstrip("\r\n")357 return next(text_input).rstrip("\r\n")358 except StopIteration as e:359 raise EOFError() from e360361 @_pause_echo(echo_input) # type: ignore362 def _getchar(echo: bool) -> str:363 char = sys.stdin.read(1)
201 unmodified lines
modified:tests/test_chain.py+2 added-2 removed
161 unmodified lines
162163 return processor164165 result = runner.invoke(cli, args, input=input)166 # last two lines are '' and 'Aborted!'167 assert result.output.splitlines()[:-2] == expect166 assert not result.exception167 assert result.output.splitlines() == expect168169170def test_args_and_chain(runner):171 @click.group(chain=True)
74 unmodified lines
modified:CHANGES.rst+5 added
1.. currentmodule:: click23Version 8.2.24-------------56- Fix regression related to EOF handling in CliRunner. :issue:`2939`78Version 8.2.19-------------1011Released 2025-05-20
1241 unmodified lines