diff --git a/tests/test_compat.py b/tests/test_compat.py index 0e2e424cb..308c35dbd 100644 --- a/tests/test_compat.py +++ b/tests/test_compat.py @@ -1,4 +1,10 @@ -from click._compat import should_strip_ansi +from __future__ import annotations + +import sys + +import pytest + +import click def test_is_jupyter_kernel_output(): @@ -7,4 +13,43 @@ class JupyterKernelFakeStream: # implementation detail, aka cheapskate test JupyterKernelFakeStream.__module__ = "ipykernel.faked" - assert not should_strip_ansi(stream=JupyterKernelFakeStream()) + assert click._compat._is_jupyter_kernel_output(stream=JupyterKernelFakeStream()) + + +@pytest.mark.parametrize( + "stream", + [None, sys.stdin, sys.stderr, sys.stdout], +) +@pytest.mark.parametrize( + "color,expected_override", + [ + (True, False), + (False, True), + (None, None), + ], +) +@pytest.mark.parametrize( + "isatty,is_jupyter,expected", + [ + (True, False, False), + (False, True, False), + (False, False, True), + ], +) +def test_should_strip_ansi( + monkeypatch, + stream, + color: bool | None, + expected_override: bool | None, + isatty: bool, + is_jupyter: bool, + expected: bool, +) -> None: + monkeypatch.setattr(click._compat, "isatty", lambda x: isatty) + monkeypatch.setattr( + click._compat, "_is_jupyter_kernel_output", lambda x: is_jupyter + ) + + if expected_override is not None: + expected = expected_override + assert click._compat.should_strip_ansi(stream=stream, color=color) == expected diff --git a/tests/test_utils.py b/tests/test_utils.py index d80aee716..657e37c92 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -207,8 +207,8 @@ def test_echo_via_pager(monkeypatch, capfd, cat, test): def test_echo_color_flag(monkeypatch, capfd): - isatty = True - monkeypatch.setattr(click._compat, "isatty", lambda x: isatty) + should_strip_ansi = False + monkeypatch.setattr(click._compat, "should_strip_ansi", lambda x: should_strip_ansi) text = "foo" styled_text = click.style(text, fg="red") @@ -222,12 +222,12 @@ def test_echo_color_flag(monkeypatch, capfd): out, err = capfd.readouterr() assert out == f"{styled_text}\n" - isatty = True + should_strip_ansi = False click.echo(styled_text) out, err = capfd.readouterr() assert out == f"{styled_text}\n" - isatty = False + should_strip_ansi = True # 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