Skip to content
Myr

Pass 'color' explicitly in error echoing (#2273)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Dan Ziring · committed by GitHub · parent afc86c7 · Browse files · 2 files+30 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Andreas Backx <andreas@backx.org>

Diff stats

2 files+30 added−1 removed

  • tests:+21 added−0 removed
  • other:+9 added−1 removed

2 files+30 added−1 removed

modified:src/click/exceptions.py+9 added-1 removed
1 unmodified line
2from gettext import gettext as _3from gettext import ngettext45from ._compat import get_text_stderr6from .globals import resolve_color_default7from .utils import echo8from .utils import format_filename910if t.TYPE_CHECKING:
18 unmodified lines
29 exit_code = 13031 def __init__(self, message: str) -> None:32 super().__init__(message)33 # The context will be removed by the time we print the message, so cache34 # the color settings here to be used later on (in `show`)35 self.show_color: t.Optional[bool] = resolve_color_default()36 self.message = message3738 def format_message(self) -> str:39 return self.message
4 unmodified lines
44 def show(self, file: t.Optional[t.IO[t.Any]] = None) -> None:45 if file is None:46 file = get_text_stderr()4744 echo(_("Error: {message}").format(message=self.format_message()), file=file)48 echo(49 _("Error: {message}").format(message=self.format_message()),50 file=file,51 color=self.show_color,52 )535455class UsageError(ClickException):56 """An internal exception that signals a usage error. This typically
240 unmodified lines
modified:tests/test_testing.py+21 added
3 unmodified lines
45import pytest67import click8from click.exceptions import ClickException9from click.testing import CliRunner101112def test_runner():
186 unmodified lines
199 assert result.output == f"{click.style('hello world', fg='blue')}\n"200 assert not result.exception201202203def test_with_color_errors():204 class CLIError(ClickException):205 def format_message(self) -> str:206 return click.style(self.message, fg="red")207208 @click.command()209 def cli():210 raise CLIError("Red error")211212 runner = CliRunner()213214 result = runner.invoke(cli)215 assert result.output == "Error: Red error\n"216 assert result.exception217218 result = runner.invoke(cli, color=True)219 assert result.output == f"Error: {click.style('Red error', fg='red')}\n"220 assert result.exception221222223def test_with_color_but_pause_not_blocking():224 @click.command()225 def cli():226 click.pause()
213 unmodified lines