Skip to content

Commit e06c2bf

Browse files
committed
Move should_strip_ansi test to test_compat
1 parent c6ee3f5 commit e06c2bf

File tree

2 files changed

+63
-155
lines changed

2 files changed

+63
-155
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: 16 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -207,171 +207,34 @@ def test_echo_via_pager(monkeypatch, capfd, cat, test):
207207
assert out == expected_output
208208

209209

210-
"""
211-
These variables are used for the next three tests.
212-
213-
_text_str is guranteeing that each color test is using the same
214-
string.
215-
216-
_styled_text_str is running _text_str through click.style with fg
217-
set to red. This gurantees that each test is getting the same styled text.
218-
219-
expected_stripped_text is using an fstring on _text_str to add a newline chracter
220-
to the end of it.
221-
222-
expected_unstripped_text is using an fstring on_styled_text_str to add a newline
223-
character to the end of it.
224-
225-
Each expected variable is what is being used in the asserts to gurantee the output
226-
is correct.
227-
228-
The unstripped version is for when an the ANSI style should stay applied.
229-
The stripped version is for when the ANSI style should be removed from the text.
230-
231-
These can be removed if you would rather have the parametrize values be explict
232-
in the following tests. The variables could also be extended and the tests truncated
233-
by adding isatty and jupyter_kernel variables here. Expanding the parametrize
234-
matrix to use those within the test instead of it being explict based on tests.
235-
"""
236-
_text_str = "test"
237-
_styled_text_str = click.style(_text_str, fg="red")
238-
expected_stripped_text = f"{_text_str}\n"
239-
expected_unstripped_text = f"{_styled_text_str}\n"
240-
241-
242-
@pytest.mark.parametrize(
243-
"color, text, stylized_text, expected",
244-
[
245-
(False, _text_str, _styled_text_str, expected_stripped_text),
246-
(True, _text_str, _styled_text_str, expected_unstripped_text),
247-
(None, _text_str, _styled_text_str, expected_unstripped_text),
248-
],
249-
)
250-
def test_echo_color_flag_atty_and_not_jupyter_kernel(
251-
monkeypatch, capfd, color, text, stylized_text, expected
252-
):
253-
"""
254-
This is the test for echo color when the stream is a tty and
255-
not a jupyter kernel output.
256-
257-
If color is set to False, then the text should strip the ANSI values regardless
258-
of operating system.
259-
260-
If color is set to True, then we are forcing click to not care about
261-
the type of stream or operating system. This means ANSI style codes will be applied
262-
even if the output would not support them. Instances of this would be piping an
263-
echo to a file.
264-
265-
If color is set to None / not set, then click will check if the stream is a tty.
266-
This test is forcing that to always be true, and will then mean that the output
267-
should keep the ANSI style.
268-
269-
The mocking of _is_jupyter_kernel_output is not needed, but was added to keep it
270-
consistent with the other tests.
271-
"""
272-
isatty = True
273-
monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)
274-
275-
jupyter_kernel = False
276-
monkeypatch.setattr(
277-
click._compat, "_is_jupyter_kernel_output", lambda x: jupyter_kernel
278-
)
210+
def test_echo_color_flag(monkeypatch, capfd):
211+
should_strip_ansi = False
212+
monkeypatch.setattr(click._compat, "should_strip_ansi", lambda x: should_strip_ansi)
279213

214+
text = "foo"
280215
styled_text = click.style(text, fg="red")
281-
assert styled_text == stylized_text
216+
assert styled_text == "\x1b[31mfoo\x1b[0m"
282217

283-
stream = StringIO()
284-
click.echo(styled_text, color=color, file=stream)
218+
click.echo(styled_text, color=False)
285219
out, err = capfd.readouterr()
286-
assert stream.getvalue() == expected
220+
assert out == f"{text}\n"
287221

288-
289-
@pytest.mark.parametrize(
290-
"color, text, stylized_text, expected",
291-
[
292-
(False, _text_str, _styled_text_str, expected_stripped_text),
293-
(True, _text_str, _styled_text_str, expected_unstripped_text),
294-
(None, _text_str, _styled_text_str, expected_unstripped_text),
295-
],
296-
)
297-
def test_echo_color_flag_jupyter_kernel_and_not_atty(
298-
monkeypatch, capfd, color, text, stylized_text, expected
299-
):
300-
"""
301-
This is the test for echo color when the stream is not a tty and
302-
is a jupyter kernel output.
303-
304-
If color is set to False, then the text should strip the ANSI values regardless
305-
of operating system.
306-
307-
If color is set to True, then we are forcing click to not care about
308-
the type of stream or operating system. This means ANSI style codes will be applied
309-
even if the output would not support them. Instances of this would be piping an
310-
echo to a file.
311-
312-
If color is set to None / not set, then click will check if the stream is a tty.
313-
Which is being forced to be False so, the strip function will next check if
314-
the stream is a jupyter kernel output which is being set as True. This means
315-
that the ANSI style should not be stripped.
316-
"""
317-
jupyter_kernel = True
318-
monkeypatch.setattr(
319-
click._compat, "_is_jupyter_kernel_output", lambda x: jupyter_kernel
320-
)
321-
isatty = False
322-
monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)
323-
324-
styled_text = click.style(text, fg="red")
325-
assert styled_text == stylized_text
326-
327-
click.echo(styled_text, color=color)
222+
click.echo(styled_text, color=True)
328223
out, err = capfd.readouterr()
329-
assert out == expected
224+
assert out == f"{styled_text}\n"
330225

226+
should_strip_ansi = False
227+
click.echo(styled_text)
228+
out, err = capfd.readouterr()
229+
assert out == f"{styled_text}\n"
331230

332-
@pytest.mark.parametrize(
333-
"color, text, stylized_text, expected",
334-
[
335-
(False, _text_str, _styled_text_str, expected_stripped_text),
336-
(True, _text_str, _styled_text_str, expected_unstripped_text),
337-
(None, _text_str, _styled_text_str, expected_stripped_text),
338-
],
339-
)
340-
def test_echo_color_flag_not_atty_and_not_jupyter_kernel(
341-
monkeypatch, capfd, color, text, stylized_text, expected
342-
):
343-
"""
344-
This is the test for echo color when the stream is not a tty nor a jupyter kernel
345-
output.
346-
347-
If color is set to False, then the text should strip the ANSI values regardless
348-
of operating system.
349-
350-
If color is set to True, then we are forcing click to not care about
351-
the type of stream or operating system. This means ANSI style codes will be applied
352-
even if the output would not support them. Instances of this would be piping an
353-
echo to a file.
354-
355-
If color is set to None / not set, then click will check that the stream is neither
356-
a tty nor a jupyter notebook. This means the styling should be stripped regardless
357-
of the operating system.
358-
"""
359-
isatty = False
231+
should_strip_ansi = True
360232
# Faking isatty() is not enough on Windows;
361233
# the implementation caches the colorama wrapped stream
362234
# so we have to use a new stream for each test
363235
stream = StringIO()
364-
monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)
365-
jupyter_kernel = False
366-
monkeypatch.setattr(
367-
click._compat, "_is_jupyter_kernel_output", lambda x: jupyter_kernel
368-
)
369-
370-
styled_text = click.style(text, fg="red")
371-
assert styled_text == stylized_text
372-
373-
click.echo(styled_text, color=color, file=stream)
374-
assert stream.getvalue() == expected
236+
click.echo(styled_text, file=stream)
237+
assert stream.getvalue() == f"{text}\n"
375238

376239
stream = StringIO()
377240
click.echo(styled_text, file=stream, color=True)

0 commit comments

Comments
 (0)