test_echo.py
This file
Path
tests/test_utils/test_echo.py
172 lines · 5.0 KiB · Python
Last changed by
Restore `test_echo_color_flag` from #3505 (#3676)Rowlando13before adoption8dd4d60
History of this fileTake 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 click.echo(styled_text)78 out, err = capfd.readouterr()79 assert out == f"{styled_text}\n"8081 isatty = False82 click.echo(styled_text)83 out, err = capfd.readouterr()84 assert out == f"{text}\n"8586 click.echo(styled_text, color=True)87 out, err = capfd.readouterr()88 assert out == f"{styled_text}\n"899091@pytest.mark.skipif(WIN, reason="Test too complex to make work windows.")92def test_echo_writing_to_standard_error(capfd, monkeypatch):93 def emulate_input(text):94 """Emulate keyboard input."""95 monkeypatch.setattr(sys, "stdin", StringIO(text))9697 click.echo("Echo to standard output")98 out, err = capfd.readouterr()99 assert out == "Echo to standard output\n"100 assert err == ""101102 click.echo("Echo to standard error", err=True)103 out, err = capfd.readouterr()104 assert out == ""105 assert err == "Echo to standard error\n"106107 emulate_input("asdlkj\n")108 click.prompt("Prompt to stdin")109 out, err = capfd.readouterr()110 assert out == "Prompt to stdin: "111 assert err == ""112113 emulate_input("asdlkj\n")114 click.prompt("Prompt to stdin with no suffix", prompt_suffix="")115 out, err = capfd.readouterr()116 assert out == "Prompt to stdin with no suffix"117 assert err == ""118119 # On non-Windows the full prompt goes through redirect_stdout so120 # nothing leaks to stdout when err=True.121 # https://github.com/pallets/click/issues/2968122 emulate_input("asdlkj\n")123 click.prompt("Prompt to stderr", err=True)124 out, err = capfd.readouterr()125 assert out == ""126 assert err == "Prompt to stderr: "127128 # https://github.com/pallets/click/issues/3019129 emulate_input("asdlkj\n")130 click.prompt("Prompt to stderr with no suffix", prompt_suffix="", err=True)131 out, err = capfd.readouterr()132 assert out == ""133 assert err == "Prompt to stderr with no suffix"134135 emulate_input("y\n")136 click.confirm("Prompt to stdin")137 out, err = capfd.readouterr()138 assert out == "Prompt to stdin [y/N]: "139 assert err == ""140141 emulate_input("y\n")142 click.confirm("Prompt to stdin with no suffix", prompt_suffix="")143 out, err = capfd.readouterr()144 assert out == "Prompt to stdin with no suffix [y/N]"145 assert err == ""146147 # https://github.com/pallets/click/issues/2968148 emulate_input("y\n")149 click.confirm("Prompt to stderr", err=True)150 out, err = capfd.readouterr()151 assert out == ""152 assert err == "Prompt to stderr [y/N]: "153154 # https://github.com/pallets/click/issues/3019155 emulate_input("y\n")156 click.confirm("Prompt to stderr with no suffix", prompt_suffix="", err=True)157 out, err = capfd.readouterr()158 assert out == ""159 assert err == "Prompt to stderr with no suffix [y/N]"160161 monkeypatch.setattr(click.termui, "isatty", lambda x: True)162 monkeypatch.setattr(click.termui, "getchar", lambda: " ")163164 click.pause("Pause to stdout")165 out, err = capfd.readouterr()166 assert out == "Pause to stdout\n"167 assert err == ""168169 click.pause("Pause to stderr", err=True)170 out, err = capfd.readouterr()171 assert out == ""172 assert err == "Pause to stderr\n"