Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward --no-color to the rich.Console instance #11048

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions news/11045.bugfix.rst
@@ -0,0 +1 @@
Set ``no_color`` to global ``rich.Console`` instance.
2 changes: 2 additions & 0 deletions src/pip/_internal/cli/base_command.py
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
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"