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

Only instantiate colored objects if color is not disabled #634

Merged
merged 3 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/syrupy/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,48 @@ def _is_color_disabled() -> bool:
return any(map(get_env_value, DISABLE_COLOR_ENV_VARS))


def _attr(color: Any) -> str:
if _is_color_disabled():
return ""
return colored.attr(color)


def _fg(color: Any) -> str:
if _is_color_disabled():
return ""
return colored.fg(color)


def _bg(color: Any) -> str:
if _is_color_disabled():
return ""
return colored.bg(color)


def _stylize(text: Union[str, int], *args: Any) -> str:
if _is_color_disabled():
return str(text)
return colored.stylize(text, *args)


def reset(text: Union[str, int]) -> str:
return _stylize(text, colored.attr("reset"))
return _stylize(text, _attr("reset"))


def red(text: Union[str, int]) -> str:
return _stylize(text, colored.fg("red"))
return _stylize(text, _fg("red"))


def yellow(text: Union[str, int]) -> str:
return _stylize(text, colored.fg("yellow"))
return _stylize(text, _fg("yellow"))


def green(text: Union[str, int]) -> str:
return _stylize(text, colored.fg("green"))
return _stylize(text, _fg("green"))


def bold(text: Union[str, int]) -> str:
return _stylize(text, colored.attr("bold"))
return _stylize(text, _attr("bold"))


def error_style(text: Union[str, int]) -> str:
Expand All @@ -52,20 +70,20 @@ def success_style(text: Union[str, int]) -> str:


def snapshot_style(text: Union[str, int]) -> str:
return _stylize(text, colored.bg(225) + colored.fg(90))
return _stylize(text, _bg(225) + _fg(90))


def snapshot_diff_style(text: Union[str, int]) -> str:
return _stylize(text, colored.bg(90) + colored.fg(225))
return _stylize(text, _bg(90) + _fg(225))


def received_style(text: Union[str, int]) -> str:
return _stylize(text, colored.bg(195) + colored.fg(23))
return _stylize(text, _bg(195) + _fg(23))


def received_diff_style(text: Union[str, int]) -> str:
return _stylize(text, colored.bg(23) + colored.fg(195))
return _stylize(text, _bg(23) + _fg(195))


def context_style(text: Union[str, int]) -> str:
return _stylize(text, colored.attr("dim"))
return _stylize(text, _attr("dim"))
58 changes: 58 additions & 0 deletions tests/syrupy/test_terminal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from unittest.mock import (
NonCallableMock,
patch,
)

import pytest

from syrupy.constants import DISABLE_COLOR_ENV_VAR
from syrupy.terminal import (
bold,
context_style,
error_style,
green,
received_diff_style,
received_style,
red,
reset,
snapshot_diff_style,
snapshot_style,
success_style,
warning_style,
yellow,
)


def test_colors_off_does_not_call_colored():
"""
Test that disabling colors prevents instantiating colored object.
Enables workarounds for when instantiating the colored object causes crashes,
see issue #633
"""

with patch(
"syrupy.terminal.colored.colored.__init__", new_callable=NonCallableMock
):
with patch.dict("os.environ", {DISABLE_COLOR_ENV_VAR: "true"}):
for method in (
reset,
red,
yellow,
green,
bold,
error_style,
warning_style,
success_style,
snapshot_style,
snapshot_diff_style,
received_style,
received_diff_style,
context_style,
):
_ = method("foo")

# Prevent test from accidentally passing by patching wrong object
with pytest.raises(TypeError) as excinfo:
_ = red("foo")

assert "NonCallableMock" in str(excinfo.value)