Skip to content
Myr

test_echo.py

This file

Path

tests/test_utils/test_echo.py

176 lines · 5.2 KiB · Python

Last changed by

Add files for _detect_program_name, _expand_args, and make_default_short_help.Edward Gbefore adoption47cc96f

History of this file

Take it

1import sys2from io import BytesIO3from io import StringIO45import pytest67import click._termui_impl8import click.utils9from click._compat import WIN101112def test_echo(runner):13    with runner.isolation() as outstreams:14        click.echo("\N{SNOWMAN}")15        click.echo(b"\x44\x44")16        click.echo(42, nl=False)17        click.echo(b"a", nl=False)18        click.echo("\x1b[31mx\x1b[39m", nl=False)19        bytes = outstreams[0].getvalue().replace(b"\r\n", b"\n")20        assert bytes == b"\xe2\x98\x83\nDD\n42ax"2122    # if wrapped, we expect bytes to survive.23    @click.command()24    def cli():25        click.echo(b"\xf6")2627    result = runner.invoke(cli, [])28    assert result.stdout_bytes == b"\xf6\n"2930    # Ensure we do not strip for bytes.31    with runner.isolation() as outstreams:32        click.echo(bytearray(b"\x1b[31mx\x1b[39m"), nl=False)33        assert outstreams[0].getvalue() == b"\x1b[31mx\x1b[39m"343536def test_echo_custom_file():37    f = StringIO()38    click.echo("hello", file=f)39    assert f.getvalue() == "hello\n"4041    b = BytesIO()42    click.echo(b"", b)43    assert b.getvalue() == b"\n"444546def test_echo_no_streams(monkeypatch, runner):47    """echo should not fail when stdout and stderr are None with pythonw on Windows."""48    with runner.isolation():49        sys.stdout = None50        sys.stderr = None51        click.echo("test")52        click.echo("test", err=True)535455def test_echo_with_capsys(capsys):56    click.echo("Capture me.")57    out, err = capsys.readouterr()58    assert out == "Capture me.\n"596061def test_echo_color_flag(monkeypatch, capfd):62    isatty = True63    monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)6465    text = "foo"66    styled_text = click.style(text, fg="red")67    assert styled_text == "\x1b[31mfoo\x1b[0m"6869    click.echo(styled_text, color=False)70    out, err = capfd.readouterr()71    assert out == f"{text}\n"7273    click.echo(styled_text, color=True)74    out, err = capfd.readouterr()75    assert out == f"{styled_text}\n"7677    isatty = True78    click.echo(styled_text)79    out, err = capfd.readouterr()80    assert out == f"{styled_text}\n"8182    isatty = False83    # Faking isatty() is not enough on Windows;84    # the implementation caches the colorama wrapped stream85    # so we have to use a new stream for each test86    stream = StringIO()87    click.echo(styled_text, file=stream)88    assert stream.getvalue() == f"{text}\n"8990    stream = StringIO()91    click.echo(styled_text, file=stream, color=True)92    assert stream.getvalue() == f"{styled_text}\n"939495@pytest.mark.skipif(WIN, reason="Test too complex to make work windows.")96def test_echo_writing_to_standard_error(capfd, monkeypatch):97    def emulate_input(text):98        """Emulate keyboard input."""99        monkeypatch.setattr(sys, "stdin", StringIO(text))100101    click.echo("Echo to standard output")102    out, err = capfd.readouterr()103    assert out == "Echo to standard output\n"104    assert err == ""105106    click.echo("Echo to standard error", err=True)107    out, err = capfd.readouterr()108    assert out == ""109    assert err == "Echo to standard error\n"110111    emulate_input("asdlkj\n")112    click.prompt("Prompt to stdin")113    out, err = capfd.readouterr()114    assert out == "Prompt to stdin: "115    assert err == ""116117    emulate_input("asdlkj\n")118    click.prompt("Prompt to stdin with no suffix", prompt_suffix="")119    out, err = capfd.readouterr()120    assert out == "Prompt to stdin with no suffix"121    assert err == ""122123    # On non-Windows the full prompt goes through redirect_stdout so124    # nothing leaks to stdout when err=True.125    # https://github.com/pallets/click/issues/2968126    emulate_input("asdlkj\n")127    click.prompt("Prompt to stderr", err=True)128    out, err = capfd.readouterr()129    assert out == ""130    assert err == "Prompt to stderr: "131132    # https://github.com/pallets/click/issues/3019133    emulate_input("asdlkj\n")134    click.prompt("Prompt to stderr with no suffix", prompt_suffix="", err=True)135    out, err = capfd.readouterr()136    assert out == ""137    assert err == "Prompt to stderr with no suffix"138139    emulate_input("y\n")140    click.confirm("Prompt to stdin")141    out, err = capfd.readouterr()142    assert out == "Prompt to stdin [y/N]: "143    assert err == ""144145    emulate_input("y\n")146    click.confirm("Prompt to stdin with no suffix", prompt_suffix="")147    out, err = capfd.readouterr()148    assert out == "Prompt to stdin with no suffix [y/N]"149    assert err == ""150151    # https://github.com/pallets/click/issues/2968152    emulate_input("y\n")153    click.confirm("Prompt to stderr", err=True)154    out, err = capfd.readouterr()155    assert out == ""156    assert err == "Prompt to stderr [y/N]: "157158    # https://github.com/pallets/click/issues/3019159    emulate_input("y\n")160    click.confirm("Prompt to stderr with no suffix", prompt_suffix="", err=True)161    out, err = capfd.readouterr()162    assert out == ""163    assert err == "Prompt to stderr with no suffix [y/N]"164165    monkeypatch.setattr(click.termui, "isatty", lambda x: True)166    monkeypatch.setattr(click.termui, "getchar", lambda: " ")167168    click.pause("Pause to stdout")169    out, err = capfd.readouterr()170    assert out == "Pause to stdout\n"171    assert err == ""172173    click.pause("Pause to stderr", err=True)174    out, err = capfd.readouterr()175    assert out == ""176    assert err == "Pause to stderr\n"