Skip to content

Commit 3f4a835

Browse files
committed
Make test_no_color use output that actually has colors
Also, reflects that `--no-color` does not mean no-ansi-escapes.
1 parent 8d61c0a commit 3f4a835

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

tests/functional/test_no_color.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
Test specific for the --no-color option
33
"""
44
import os
5+
import shutil
56
import subprocess
7+
import sys
68

79
import pytest
810

9-
from tests.lib import PipTestEnvironment
11+
from tests.lib import PipTestEnvironment, TestData
1012

1113

12-
def test_no_color(script: PipTestEnvironment) -> None:
14+
@pytest.mark.skipif(shutil.which("script") is None, reason="no 'script' executable")
15+
def test_no_color(script: PipTestEnvironment, data: TestData) -> None:
1316
"""Ensure colour output disabled when --no-color is passed."""
1417
# Using 'script' in this test allows for transparently testing pip's output
1518
# since pip is smart enough to disable colour output when piped, which is
@@ -19,12 +22,14 @@ def test_no_color(script: PipTestEnvironment) -> None:
1922
# 'script' and well as the mere use of the same.
2023
#
2124
# This test will stay until someone has the time to rewrite it.
22-
command = (
23-
"script --flush --quiet --return /tmp/pip-test-no-color.txt "
24-
'--command "pip uninstall {} noSuchPackage"'
25-
)
25+
package = str(data.src / "pep518_invalid_build_system")
26+
pip_command = f"pip install --no-index {{}} {package}"
27+
if sys.platform == "darwin":
28+
command = f"script -q /tmp/pip-test-no-color.txt {pip_command}"
29+
else:
30+
command = f'script -q /tmp/pip-test-no-color.txt --command "{pip_command}"'
2631

27-
def get_run_output(option: str) -> str:
32+
def get_run_output(option: str = "") -> str:
2833
cmd = command.format(option)
2934
proc = subprocess.Popen(
3035
cmd,
@@ -33,8 +38,6 @@ def get_run_output(option: str) -> str:
3338
stderr=subprocess.PIPE,
3439
)
3540
proc.communicate()
36-
if proc.returncode:
37-
pytest.skip("Unable to capture output using script: " + cmd)
3841

3942
try:
4043
with open("/tmp/pip-test-no-color.txt") as output_file:
@@ -43,7 +46,5 @@ def get_run_output(option: str) -> str:
4346
finally:
4447
os.unlink("/tmp/pip-test-no-color.txt")
4548

46-
assert "\x1b" in get_run_output(option=""), "Expected color in output"
47-
assert "\x1b" not in get_run_output(
48-
option="--no-color"
49-
), "Expected no color in output"
49+
assert "\x1b[3" in get_run_output(""), "Expected color in output"
50+
assert "\x1b[3" not in get_run_output("--no-color"), "Expected no color in output"

0 commit comments

Comments
 (0)