Skip to content

Commit b53e250

Browse files
committed
testing/CliRunner: Fix regression related to EOF introduced in 262bdf0
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.
1 parent 2d610e3 commit b53e250

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
.. currentmodule:: click
22

3+
Version 8.2.2
4+
-------------
5+
6+
- Fix regression related to EOF handling in CliRunner. :issue:`2939`
7+
38
Version 8.2.1
49
-------------
510

src/click/testing.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,6 @@ def name(self) -> str:
116116
def mode(self) -> str:
117117
return self._mode
118118

119-
def __next__(self) -> str: # type: ignore
120-
try:
121-
line = super().__next__()
122-
except StopIteration as e:
123-
raise EOFError() from e
124-
return line
125-
126119

127120
def make_input_stream(
128121
input: str | bytes | t.IO[t.Any] | None, charset: str
@@ -348,7 +341,10 @@ def isolation(
348341
@_pause_echo(echo_input) # type: ignore
349342
def visible_input(prompt: str | None = None) -> str:
350343
sys.stdout.write(prompt or "")
351-
val = next(text_input).rstrip("\r\n")
344+
try:
345+
val = next(text_input).rstrip("\r\n")
346+
except StopIteration as e:
347+
raise EOFError()
352348
sys.stdout.write(f"{val}\n")
353349
sys.stdout.flush()
354350
return val
@@ -357,7 +353,10 @@ def visible_input(prompt: str | None = None) -> str:
357353
def hidden_input(prompt: str | None = None) -> str:
358354
sys.stdout.write(f"{prompt or ''}\n")
359355
sys.stdout.flush()
360-
return next(text_input).rstrip("\r\n")
356+
try:
357+
return next(text_input).rstrip("\r\n")
358+
except StopIteration as e:
359+
raise EOFError() from e
361360

362361
@_pause_echo(echo_input) # type: ignore
363362
def _getchar(echo: bool) -> str:

tests/test_chain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ def processor(iterator):
163163
return processor
164164

165165
result = runner.invoke(cli, args, input=input)
166-
# last two lines are '' and 'Aborted!'
167-
assert result.output.splitlines()[:-2] == expect
166+
assert not result.exception
167+
assert result.output.splitlines() == expect
168168

169169

170170
def test_args_and_chain(runner):

0 commit comments

Comments
 (0)