Skip to content
Myr

Move move test to prompt file.

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

authored by Edward G · committed by Edward G · parent 182944f · Browse files · 2 files+63 added−68 removed
This commit

State

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

Diff stats

2 files+63 added−68 removed

  • tests:+63 added−68 removed

2 files+63 added−68 removed

modified:tests/test_utils_2/test_prompt.py+63 added
1import sys2from io import StringIO34import pytest56import click7from click._compat import WIN8910def test_prompt_cast_default(capfd, monkeypatch):11 monkeypatch.setattr(sys, "stdin", StringIO("\n"))12 value = click.prompt("value", default="100", type=int)13 capfd.readouterr()14 assert isinstance(value, int)151617@pytest.mark.skipif(WIN, reason="Different behavior on windows.")18def test_prompts_abort(monkeypatch, capsys):19 def f(_):20 raise KeyboardInterrupt()2122 monkeypatch.setattr("click.termui.hidden_prompt_func", f)2324 try:25 click.prompt("Password", hide_input=True)26 except click.Abort:27 click.echo("interrupted")2829 out, err = capsys.readouterr()30 # On non-Windows, prompt is passed directly to getpass, not echoed separately31 assert out == "\ninterrupted\n"323334@pytest.mark.skipif(WIN, reason="Different behavior on windows.")35@pytest.mark.parametrize(36 ("call", "expected_prompt"),37 [38 (lambda: click.prompt("Name"), "Name: "),39 (lambda: click.prompt("Pw", hide_input=True), "Pw: "),40 (lambda: click.prompt("IP", prompt_suffix="."), "IP."),41 (lambda: click.confirm("OK"), "OK [y/N]: "),42 ],43 ids=["prompt", "prompt-hidden", "prompt-custom-suffix", "confirm"],44)45def test_full_prompt_passed_to_readline(monkeypatch, call, expected_prompt):46 """On non-Windows, prompt and confirm pass the full prompt text to the47 underlying prompt function so readline handles editing correctly.4849 https://github.com/pallets/click/issues/296850 https://github.com/pallets/click/pull/296951 """52 received = []5354 def capture(text):55 received.append(text)56 return "y"5758 monkeypatch.setattr("click.termui.visible_prompt_func", capture)59 monkeypatch.setattr("click.termui.hidden_prompt_func", capture)60 call()61 assert received == [expected_prompt]626364def test_prompts_eof(runner):65 """If too few lines of input are given, prompt should exit, not hang."""6667 @click.command68 def echo():69 for _ in range(3):70 click.echo(click.prompt("", type=int))7172 # only provide two lines of input for three prompts73 result = runner.invoke(echo, input="1\n2\n")74 assert result.exit_code == 1
modified:tests/test_utils.py-68 removed
66 unmodified lines
67 assert click.format_filename("/x/foo.txt", shorten=True) == "foo.txt"68 assert click.format_filename("/x/\ufffd.txt", shorten=True) == "�.txt"697071def test_confirm_repeat(runner):72 cli = click.Command(73 "cli", params=[click.Option(["--a/--no-a"], default=None, prompt=True)]74 )75 result = runner.invoke(cli, input="\ny\n")76 assert result.output == "A [y/n]: \nError: invalid input\nA [y/n]: y\n"777879@pytest.mark.skipif(WIN, reason="Different behavior on windows.")80def test_prompts_abort(monkeypatch, capsys):81 def f(_):82 raise KeyboardInterrupt()8384 monkeypatch.setattr("click.termui.hidden_prompt_func", f)8586 try:87 click.prompt("Password", hide_input=True)88 except click.Abort:89 click.echo("interrupted")9091 out, err = capsys.readouterr()92 # On non-Windows, prompt is passed directly to getpass, not echoed separately93 assert out == "\ninterrupted\n"949596@pytest.mark.skipif(WIN, reason="Different behavior on windows.")97@pytest.mark.parametrize(98 ("call", "expected_prompt"),99 [100 (lambda: click.prompt("Name"), "Name: "),101 (lambda: click.prompt("Pw", hide_input=True), "Pw: "),102 (lambda: click.prompt("IP", prompt_suffix="."), "IP."),103 (lambda: click.confirm("OK"), "OK [y/N]: "),104 ],105 ids=["prompt", "prompt-hidden", "prompt-custom-suffix", "confirm"],106)107def test_full_prompt_passed_to_readline(monkeypatch, call, expected_prompt):108 """On non-Windows, prompt and confirm pass the full prompt text to the109 underlying prompt function so readline handles editing correctly.110111 https://github.com/pallets/click/issues/2968112 https://github.com/pallets/click/pull/2969113 """114 received = []115116 def capture(text):117 received.append(text)118 return "y"119120 monkeypatch.setattr("click.termui.visible_prompt_func", capture)121 monkeypatch.setattr("click.termui.hidden_prompt_func", capture)122 call()123 assert received == [expected_prompt]124125126def test_prompts_eof(runner):127 """If too few lines of input are given, prompt should exit, not hang."""128129 @click.command130 def echo():131 for _ in range(3):132 click.echo(click.prompt("", type=int))133134 # only provide two lines of input for three prompts135 result = runner.invoke(echo, input="1\n2\n")136 assert result.exit_code == 113713871def test_open_file(runner):72 @click.command()73 @click.argument("filename")74 def cli(filename):
199 unmodified lines