|
| 1 | +import sys |
| 2 | +from io import BytesIO |
| 3 | +from io import StringIO |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +import click._termui_impl |
| 8 | +import click.utils |
| 9 | +from click._compat import WIN |
| 10 | + |
| 11 | + |
| 12 | +def 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" |
| 21 | + |
| 22 | + # if wrapped, we expect bytes to survive. |
| 23 | + @click.command() |
| 24 | + def cli(): |
| 25 | + click.echo(b"\xf6") |
| 26 | + |
| 27 | + result = runner.invoke(cli, []) |
| 28 | + assert result.stdout_bytes == b"\xf6\n" |
| 29 | + |
| 30 | + # 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" |
| 34 | + |
| 35 | + |
| 36 | +def test_echo_custom_file(): |
| 37 | + f = StringIO() |
| 38 | + click.echo("hello", file=f) |
| 39 | + assert f.getvalue() == "hello\n" |
| 40 | + |
| 41 | + b = BytesIO() |
| 42 | + click.echo(b"", b) |
| 43 | + assert b.getvalue() == b"\n" |
| 44 | + |
| 45 | + |
| 46 | +def 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 = None |
| 50 | + sys.stderr = None |
| 51 | + click.echo("test") |
| 52 | + click.echo("test", err=True) |
| 53 | + |
| 54 | + |
| 55 | +def test_echo_with_capsys(capsys): |
| 56 | + click.echo("Capture me.") |
| 57 | + out, err = capsys.readouterr() |
| 58 | + assert out == "Capture me.\n" |
| 59 | + |
| 60 | + |
| 61 | +def test_echo_color_flag(monkeypatch, capfd): |
| 62 | + isatty = True |
| 63 | + monkeypatch.setattr(click._compat, "isatty", lambda x: isatty) |
| 64 | + |
| 65 | + text = "foo" |
| 66 | + styled_text = click.style(text, fg="red") |
| 67 | + assert styled_text == "\x1b[31mfoo\x1b[0m" |
| 68 | + |
| 69 | + click.echo(styled_text, color=False) |
| 70 | + out, err = capfd.readouterr() |
| 71 | + assert out == f"{text}\n" |
| 72 | + |
| 73 | + click.echo(styled_text, color=True) |
| 74 | + out, err = capfd.readouterr() |
| 75 | + assert out == f"{styled_text}\n" |
| 76 | + |
| 77 | + isatty = True |
| 78 | + click.echo(styled_text) |
| 79 | + out, err = capfd.readouterr() |
| 80 | + assert out == f"{styled_text}\n" |
| 81 | + |
| 82 | + isatty = False |
| 83 | + # Faking isatty() is not enough on Windows; |
| 84 | + # the implementation caches the colorama wrapped stream |
| 85 | + # so we have to use a new stream for each test |
| 86 | + stream = StringIO() |
| 87 | + click.echo(styled_text, file=stream) |
| 88 | + assert stream.getvalue() == f"{text}\n" |
| 89 | + |
| 90 | + stream = StringIO() |
| 91 | + click.echo(styled_text, file=stream, color=True) |
| 92 | + assert stream.getvalue() == f"{styled_text}\n" |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.skipif(WIN, reason="Test too complex to make work windows.") |
| 96 | +def test_echo_writing_to_standard_error(capfd, monkeypatch): |
| 97 | + def emulate_input(text): |
| 98 | + """Emulate keyboard input.""" |
| 99 | + monkeypatch.setattr(sys, "stdin", StringIO(text)) |
| 100 | + |
| 101 | + click.echo("Echo to standard output") |
| 102 | + out, err = capfd.readouterr() |
| 103 | + assert out == "Echo to standard output\n" |
| 104 | + assert err == "" |
| 105 | + |
| 106 | + click.echo("Echo to standard error", err=True) |
| 107 | + out, err = capfd.readouterr() |
| 108 | + assert out == "" |
| 109 | + assert err == "Echo to standard error\n" |
| 110 | + |
| 111 | + emulate_input("asdlkj\n") |
| 112 | + click.prompt("Prompt to stdin") |
| 113 | + out, err = capfd.readouterr() |
| 114 | + assert out == "Prompt to stdin: " |
| 115 | + assert err == "" |
| 116 | + |
| 117 | + 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 == "" |
| 122 | + |
| 123 | + # On non-Windows the full prompt goes through redirect_stdout so |
| 124 | + # nothing leaks to stdout when err=True. |
| 125 | + # https://github.com/pallets/click/issues/2968 |
| 126 | + 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: " |
| 131 | + |
| 132 | + # https://github.com/pallets/click/issues/3019 |
| 133 | + 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" |
| 138 | + |
| 139 | + 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 == "" |
| 144 | + |
| 145 | + 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 == "" |
| 150 | + |
| 151 | + # https://github.com/pallets/click/issues/2968 |
| 152 | + 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]: " |
| 157 | + |
| 158 | + # https://github.com/pallets/click/issues/3019 |
| 159 | + 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]" |
| 164 | + |
| 165 | + monkeypatch.setattr(click.termui, "isatty", lambda x: True) |
| 166 | + monkeypatch.setattr(click.termui, "getchar", lambda: " ") |
| 167 | + |
| 168 | + click.pause("Pause to stdout") |
| 169 | + out, err = capfd.readouterr() |
| 170 | + assert out == "Pause to stdout\n" |
| 171 | + assert err == "" |
| 172 | + |
| 173 | + click.pause("Pause to stderr", err=True) |
| 174 | + out, err = capfd.readouterr() |
| 175 | + assert out == "" |
| 176 | + assert err == "Pause to stderr\n" |
0 commit comments