Skip to content
Myr

test_prompt.py

This file

Path

tests/test_utils/test_prompt.py

100 lines · 3.1 KiB · Python

Last changed by

`click.prompt` typing clarifications / improvements (#3407)Rowlando13before adoption5e906a8

History of this file

Take it

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)151617def test_prompt_default_round_trips_through_type(capfd, monkeypatch):18    """A default that already has the converted value type still passes19    through ``ParamType.convert``, so conversion side effects keep20    applying to defaults."""2122    class DoublingType(click.ParamType[int]):23        name = "doubling"2425        def convert(self, value, param, ctx):26            return int(value) * 22728    monkeypatch.setattr(sys, "stdin", StringIO("\n"))29    value = click.prompt("value", default=5, type=DoublingType())30    capfd.readouterr()31    assert value == 10323334def test_prompt_default_validated_by_type(capfd, monkeypatch):35    """An out-of-range default is rejected by the type's validation and36    prompts again instead of being returned as-is."""37    monkeypatch.setattr(sys, "stdin", StringIO("\n7\n"))38    value = click.prompt("value", default=100, type=click.IntRange(0, 10))39    capfd.readouterr()40    assert value == 7414243@pytest.mark.skipif(WIN, reason="Different behavior on windows.")44def test_prompts_abort(monkeypatch, capsys):45    def f(_):46        raise KeyboardInterrupt()4748    monkeypatch.setattr("click.termui.hidden_prompt_func", f)4950    try:51        click.prompt("Password", hide_input=True)52    except click.Abort:53        click.echo("interrupted")5455    out, err = capsys.readouterr()56    # On non-Windows, prompt is passed directly to getpass, not echoed separately57    assert out == "\ninterrupted\n"585960@pytest.mark.skipif(WIN, reason="Different behavior on windows.")61@pytest.mark.parametrize(62    ("call", "expected_prompt"),63    [64        (lambda: click.prompt("Name"), "Name: "),65        (lambda: click.prompt("Pw", hide_input=True), "Pw: "),66        (lambda: click.prompt("IP", prompt_suffix="."), "IP."),67        (lambda: click.confirm("OK"), "OK [y/N]: "),68    ],69    ids=["prompt", "prompt-hidden", "prompt-custom-suffix", "confirm"],70)71def test_full_prompt_passed_to_readline(monkeypatch, call, expected_prompt):72    """On non-Windows, prompt and confirm pass the full prompt text to the73    underlying prompt function so readline handles editing correctly.7475    https://github.com/pallets/click/issues/296876    https://github.com/pallets/click/pull/296977    """78    received = []7980    def capture(text):81        received.append(text)82        return "y"8384    monkeypatch.setattr("click.termui.visible_prompt_func", capture)85    monkeypatch.setattr("click.termui.hidden_prompt_func", capture)86    call()87    assert received == [expected_prompt]888990def test_prompts_eof(runner):91    """If too few lines of input are given, prompt should exit, not hang."""9293    @click.command94    def echo():95        for _ in range(3):96            click.echo(click.prompt("", type=int))9798    # only provide two lines of input for three prompts99    result = runner.invoke(echo, input="1\n2\n")100    assert result.exit_code == 1