Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix colored output on Windows (#2606) #2607

Open
wants to merge 1 commit into
base: 8.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/click/utils.py
Expand Up @@ -311,7 +311,7 @@ def echo(
out = strip_ansi(out)
elif WIN:
if auto_wrap_for_ansi is not None:
file = auto_wrap_for_ansi(file) # type: ignore
file = auto_wrap_for_ansi(file, color) # type: ignore
elif not color:
out = strip_ansi(out)

Expand Down
18 changes: 11 additions & 7 deletions tests/test_utils.py
Expand Up @@ -36,9 +36,7 @@ def cli():


def test_echo_custom_file():
import io

f = io.StringIO()
f = StringIO()
click.echo("hello", file=f)
assert f.getvalue() == "hello\n"

Expand Down Expand Up @@ -209,7 +207,6 @@ def test_echo_via_pager(monkeypatch, capfd, cat, test):
assert out == expected_output


@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")
def test_echo_color_flag(monkeypatch, capfd):
isatty = True
monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)
Expand All @@ -232,9 +229,16 @@ def test_echo_color_flag(monkeypatch, capfd):
assert out == f"{styled_text}\n"

isatty = False
click.echo(styled_text)
out, err = capfd.readouterr()
assert out == f"{text}\n"
# Faking isatty() is not enough on Windows;
# the implementation caches the colorama wrapped stream
# so we have to use a new stream for each test
stream = StringIO()
click.echo(styled_text, file=stream)
assert stream.getvalue() == f"{text}\n"

stream = StringIO()
click.echo(styled_text, file=stream, color=True)
assert stream.getvalue() == f"{styled_text}\n"


def test_prompt_cast_default(capfd, monkeypatch):
Expand Down