Skip to content

Commit 5e7a97d

Browse files
Tyl13davidism
authored andcommitted
refactor tests for color handling
1 parent fdc6b02 commit 5e7a97d

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

tests/test_compat.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
from click._compat import should_strip_ansi
1+
from __future__ import annotations
2+
3+
import sys
4+
5+
import pytest
6+
7+
import click
28

39

410
def test_is_jupyter_kernel_output():
@@ -7,4 +13,43 @@ class JupyterKernelFakeStream:
713

814
# implementation detail, aka cheapskate test
915
JupyterKernelFakeStream.__module__ = "ipykernel.faked"
10-
assert not should_strip_ansi(stream=JupyterKernelFakeStream())
16+
assert click._compat._is_jupyter_kernel_output(stream=JupyterKernelFakeStream())
17+
18+
19+
@pytest.mark.parametrize(
20+
"stream",
21+
[None, sys.stdin, sys.stderr, sys.stdout],
22+
)
23+
@pytest.mark.parametrize(
24+
"color,expected_override",
25+
[
26+
(True, False),
27+
(False, True),
28+
(None, None),
29+
],
30+
)
31+
@pytest.mark.parametrize(
32+
"isatty,is_jupyter,expected",
33+
[
34+
(True, False, False),
35+
(False, True, False),
36+
(False, False, True),
37+
],
38+
)
39+
def test_should_strip_ansi(
40+
monkeypatch,
41+
stream,
42+
color: bool | None,
43+
expected_override: bool | None,
44+
isatty: bool,
45+
is_jupyter: bool,
46+
expected: bool,
47+
) -> None:
48+
monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)
49+
monkeypatch.setattr(
50+
click._compat, "_is_jupyter_kernel_output", lambda x: is_jupyter
51+
)
52+
53+
if expected_override is not None:
54+
expected = expected_override
55+
assert click._compat.should_strip_ansi(stream=stream, color=color) == expected

tests/test_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ def test_echo_via_pager(monkeypatch, capfd, cat, test):
207207

208208

209209
def test_echo_color_flag(monkeypatch, capfd):
210-
isatty = True
211-
monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)
210+
should_strip_ansi = False
211+
monkeypatch.setattr(click._compat, "should_strip_ansi", lambda x: should_strip_ansi)
212212

213213
text = "foo"
214214
styled_text = click.style(text, fg="red")
@@ -222,12 +222,12 @@ def test_echo_color_flag(monkeypatch, capfd):
222222
out, err = capfd.readouterr()
223223
assert out == f"{styled_text}\n"
224224

225-
isatty = True
225+
should_strip_ansi = False
226226
click.echo(styled_text)
227227
out, err = capfd.readouterr()
228228
assert out == f"{styled_text}\n"
229229

230-
isatty = False
230+
should_strip_ansi = True
231231
# Faking isatty() is not enough on Windows;
232232
# the implementation caches the colorama wrapped stream
233233
# so we have to use a new stream for each test

0 commit comments

Comments
 (0)