From 3f4a835d273665ba8b7a098b0b952acfb4879bc4 Mon Sep 17 00:00:00 2001 From: Pradyun Gedam Date: Sat, 4 Dec 2021 18:47:54 +0000 Subject: [PATCH] Make test_no_color use output that actually has colors Also, reflects that `--no-color` does not mean no-ansi-escapes. --- tests/functional/test_no_color.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tests/functional/test_no_color.py b/tests/functional/test_no_color.py index 9ead9996ad8..e6bccca1efc 100644 --- a/tests/functional/test_no_color.py +++ b/tests/functional/test_no_color.py @@ -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 @@ -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, @@ -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: @@ -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"