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
Provenance
- Commit
1103c5c- Tree
551a55c- Author
Edward G <edward.g2013@gmail.com>- Committer
Edward G <edward.g2013@gmail.com>- Parent
182944f
2 files changed+63 added−68 removed
modified:tests/test_utils_2/test_prompt.py+63 added
1 import sys2 from io import StringIO3 4+import pytest5+6 import click7+from click._compat import WIN8 9 10 def 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)15+16+17+@pytest.mark.skipif(WIN, reason="Different behavior on windows.")18+def test_prompts_abort(monkeypatch, capsys):19+ def f(_):20+ raise KeyboardInterrupt()21+22+ monkeypatch.setattr("click.termui.hidden_prompt_func", f)23+24+ try:25+ click.prompt("Password", hide_input=True)26+ except click.Abort:27+ click.echo("interrupted")28+29+ out, err = capsys.readouterr()30+ # On non-Windows, prompt is passed directly to getpass, not echoed separately31+ assert out == "\ninterrupted\n"32+33+34+@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+)45+def 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.48+49+ https://github.com/pallets/click/issues/296850+ https://github.com/pallets/click/pull/296951+ """52+ received = []53+54+ def capture(text):55+ received.append(text)56+ return "y"57+58+ monkeypatch.setattr("click.termui.visible_prompt_func", capture)59+ monkeypatch.setattr("click.termui.hidden_prompt_func", capture)60+ call()61+ assert received == [expected_prompt]62+63+64+def test_prompts_eof(runner):65+ """If too few lines of input are given, prompt should exit, not hang."""66+67+ @click.command68+ def echo():69+ for _ in range(3):70+ click.echo(click.prompt("", type=int))71+72+ # only provide two lines of input for three prompts73+ result = runner.invoke(echo, input="1\n2\n")74+ assert result.exit_code == 1modified:tests/test_utils.py-68 removed
66 unmodified lines67 assert click.format_filename("/x/foo.txt", shorten=True) == "foo.txt"68 assert click.format_filename("/x/\ufffd.txt", shorten=True) == "�.txt"69 70 71-def 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"77-78-79-@pytest.mark.skipif(WIN, reason="Different behavior on windows.")80-def test_prompts_abort(monkeypatch, capsys):81- def f(_):82- raise KeyboardInterrupt()83-84- monkeypatch.setattr("click.termui.hidden_prompt_func", f)85-86- try:87- click.prompt("Password", hide_input=True)88- except click.Abort:89- click.echo("interrupted")90-91- out, err = capsys.readouterr()92- # On non-Windows, prompt is passed directly to getpass, not echoed separately93- assert out == "\ninterrupted\n"94-95-96-@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-)107-def 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.110-111- https://github.com/pallets/click/issues/2968112- https://github.com/pallets/click/pull/2969113- """114- received = []115-116- def capture(text):117- received.append(text)118- return "y"119-120- monkeypatch.setattr("click.termui.visible_prompt_func", capture)121- monkeypatch.setattr("click.termui.hidden_prompt_func", capture)122- call()123- assert received == [expected_prompt]124-125-126-def test_prompts_eof(runner):127- """If too few lines of input are given, prompt should exit, not hang."""128-129- @click.command130- def echo():131- for _ in range(3):132- click.echo(click.prompt("", type=int))133-134- # only provide two lines of input for three prompts135- result = runner.invoke(echo, input="1\n2\n")136- assert result.exit_code == 1137-138-71 def test_open_file(runner):72 @click.command()73 @click.argument("filename")74 def cli(filename):199 unmodified lines