Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions tests/test_compat.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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
8 changes: 4 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
Loading