Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Version 8.2.1
:issue:`2897` :pr:`2930`
- Fix shell completion for nested groups. :issue:`2906` :pr:`2907`
- Flush ``sys.stderr`` at the end of ``CliRunner.invoke``. :issue:`2682`
- Fix EOF handling for stdin input in CliRunner. :issue:`2787`

Version 8.2.0
-------------
Expand Down
11 changes: 9 additions & 2 deletions src/click/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ def name(self) -> str:
def mode(self) -> str:
return self._mode

def __next__(self) -> str: # type: ignore
Copy link
Contributor Author

@kieranyyu kieranyyu May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy claims the return type should be bytes because of IOBase but it should be matching with TextIOWrapper
Same issue as python/mypy#9643

try:
line = super().__next__()
except StopIteration as e:
raise EOFError() from e
return line


def make_input_stream(
input: str | bytes | t.IO[t.Any] | None, charset: str
Expand Down Expand Up @@ -341,7 +348,7 @@ def isolation(
@_pause_echo(echo_input) # type: ignore
def visible_input(prompt: str | None = None) -> str:
sys.stdout.write(prompt or "")
val = text_input.readline().rstrip("\r\n")
val = next(text_input).rstrip("\r\n")
sys.stdout.write(f"{val}\n")
sys.stdout.flush()
return val
Expand All @@ -350,7 +357,7 @@ def visible_input(prompt: str | None = None) -> str:
def hidden_input(prompt: str | None = None) -> str:
sys.stdout.write(f"{prompt or ''}\n")
sys.stdout.flush()
return text_input.readline().rstrip("\r\n")
return next(text_input).rstrip("\r\n")

@_pause_echo(echo_input) # type: ignore
def _getchar(echo: bool) -> str:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ def processor(iterator):
return processor

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


def test_args_and_chain(runner):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def test_file_prompt_default_format(runner, file_kwargs):
def cli(f):
click.echo(f.name)

result = runner.invoke(cli)
result = runner.invoke(cli, input="\n")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline needs to be inputted to simulate user input

assert result.output == f"file [{__file__}]: \n{__file__}\n"


Expand Down Expand Up @@ -451,7 +451,7 @@ def cli(value, o):
[
(True, "password\npassword", None, "password"),
("Confirm Password", "password\npassword\n", None, "password"),
(True, "", "", ""),
(True, "\n\n", "", ""),
(False, None, None, None),
],
)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ def f(_):
assert out == "Password:\ninterrupted\n"


def test_prompts_eof(runner):
"""If too few lines of input are given, prompt should exit, not hang."""

@click.command
def echo():
for _ in range(3):
click.echo(click.prompt("", type=int))

# only provide two lines of input for three prompts
result = runner.invoke(echo, input="1\n2\n")
assert result.exit_code == 1


def _test_gen_func():
yield "a"
yield "b"
Expand Down