Skip to content

Commit

Permalink
Fix colored output on Windows (pallets#2606)
Browse files Browse the repository at this point in the history
  • Loading branch information
krisztianloki authored and AndreasBackx committed Oct 26, 2024
1 parent 02046e7 commit 2fba1b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/click/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,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
Original file line number Diff line number Diff line change
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

0 comments on commit 2fba1b4

Please sign in to comment.