summaryrefslogtreecommitdiff
path: root/tests/functional/test_no_color.py
blob: 4094bdd167a1760b53510179c04ae02ae2ebfe39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
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"