Skip to content

Commit

Permalink
Make test_no_color use output that actually has colors
Browse files Browse the repository at this point in the history
Also, reflects that `--no-color` does not mean no-ansi-escapes.
  • Loading branch information
pradyunsg committed Dec 4, 2021
1 parent 8d61c0a commit 3f4a835
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions tests/functional/test_no_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
Test specific for the --no-color option
"""
import os
import shutil
import subprocess
import sys

import pytest

from tests.lib import PipTestEnvironment
from tests.lib import PipTestEnvironment, TestData


def test_no_color(script: PipTestEnvironment) -> None:
@pytest.mark.skipif(shutil.which("script") is None, reason="no 'script' executable")
def test_no_color(script: PipTestEnvironment, data: TestData) -> 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
Expand All @@ -19,12 +22,14 @@ def test_no_color(script: PipTestEnvironment) -> None:
# 'script' and well as the mere use of the same.
#
# This test will stay until someone has the time to rewrite it.
command = (
"script --flush --quiet --return /tmp/pip-test-no-color.txt "
'--command "pip uninstall {} noSuchPackage"'
)
package = str(data.src / "pep518_invalid_build_system")
pip_command = f"pip install --no-index {{}} {package}"
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:
def get_run_output(option: str = "") -> str:
cmd = command.format(option)
proc = subprocess.Popen(
cmd,
Expand All @@ -33,8 +38,6 @@ def get_run_output(option: str) -> str:
stderr=subprocess.PIPE,
)
proc.communicate()
if proc.returncode:
pytest.skip("Unable to capture output using script: " + cmd)

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

assert "\x1b" in get_run_output(option=""), "Expected color in output"
assert "\x1b" not in get_run_output(
option="--no-color"
), "Expected no color in output"
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"

0 comments on commit 3f4a835

Please sign in to comment.