Skip to content

Commit

Permalink
Forward --no-color to the rich.Console instance (#11048)
Browse files Browse the repository at this point in the history
Co-authored-by: Pradyun Gedam <[email protected]>
  • Loading branch information
q0w and pradyunsg authored Jul 9, 2024
1 parent d483ff3 commit f0bb386
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/11045.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Set ``no_color`` to global ``rich.Console`` instance.
2 changes: 2 additions & 0 deletions src/pip/_internal/cli/base_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from optparse import Values
from typing import Any, Callable, List, Optional, Tuple

from pip._vendor.rich import reconfigure
from pip._vendor.rich import traceback as rich_traceback

from pip._internal.cli import cmdoptions
Expand Down Expand Up @@ -115,6 +116,7 @@ def _main(self, args: List[str]) -> int:
# Set verbosity so that it can be used elsewhere.
self.verbosity = options.verbose - options.quiet

reconfigure(no_color=options.no_color)
level_number = setup_logging(
verbosity=self.verbosity,
no_color=options.no_color,
Expand Down
4 changes: 3 additions & 1 deletion tests/functional/test_no_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from tests.lib import PipTestEnvironment


@pytest.mark.network
@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."""
Expand All @@ -23,7 +24,7 @@ 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.
pip_command = "pip uninstall {} noSuchPackage"
pip_command = "pip download {} setuptools==62.0.0 --no-cache-dir -d /tmp/"
if sys.platform == "darwin":
command = f"script -q /tmp/pip-test-no-color.txt {pip_command}"
else:
Expand All @@ -45,6 +46,7 @@ def get_run_output(option: str = "") -> str:
return retval
finally:
os.unlink("/tmp/pip-test-no-color.txt")
os.unlink("/tmp/setuptools-62.0.0-py3-none-any.whl")

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 f0bb386

Please sign in to comment.