1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/tests/functional/test_no_color.py
Pradyun Gedam b82dfd1745
Restore colors in regular logging messages
Also, reflects that `--no-color` does not mean no-ansi-escapes but only
affects colour rules.
2021-12-12 13:11:36 +00:00

50 lines
1.6 KiB
Python

"""
Test specific for the --no-color option
"""
import os
import shutil
import subprocess
import sys
import pytest
from tests.lib import PipTestEnvironment
@pytest.mark.skipif(shutil.which("script") is None, reason="no 'script' executable")
def test_no_color(script: PipTestEnvironment) -> None:
"""Ensure colour output disabled when --no-color is passed."""
# Using 'script' in this test allows for transparently testing pip's output
# since pip is smart enough to disable colour output when piped, which is
# not the behaviour we want to be testing here.
#
# On the other hand, this test is non-portable due to the options passed to
# 'script' and well as the mere use of the same.
#
# This test will stay until someone has the time to rewrite it.
pip_command = "pip uninstall {} noSuchPackage"
if sys.platform == "darwin":
command = f"script -q /tmp/pip-test-no-color.txt {pip_command}"
else:
command = f'script -q /tmp/pip-test-no-color.txt --command "{pip_command}"'
def get_run_output(option: str = "") -> str:
cmd = command.format(option)
proc = subprocess.Popen(
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
proc.communicate()
try:
with open("/tmp/pip-test-no-color.txt") as output_file:
retval = output_file.read()
return retval
finally:
os.unlink("/tmp/pip-test-no-color.txt")
assert "\x1b[3" in get_run_output(""), "Expected color in output"
assert "\x1b[3" not in get_run_output("--no-color"), "Expected no color in output"