Skip to content

Commit

Permalink
Add test coverage for utils/color
Browse files Browse the repository at this point in the history
  • Loading branch information
zer0nim committed Apr 2, 2022
1 parent 03d7300 commit 224b3d6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion detect_secrets/util/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sys import stdout


def support_ansi_colors():
def support_ansi_colors() -> bool:
return (getenv('CLICOLOR', '1') != '0' and stdout.isatty())\
or getenv('CLICOLOR_FORCE', '0') != '0'

Expand Down
44 changes: 44 additions & 0 deletions tests/util/color_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from sys import stdout

from detect_secrets.util.color import AnsiColor
from detect_secrets.util.color import colorize


def colorize_enabled(text: str, color: AnsiColor) -> str:
return '\x1b{}{}\x1b{}'.format(
color.value,
text,
AnsiColor.RESET.value,
)


def expect_enabled(text: str):
for color in AnsiColor:
expected = colorize_enabled(text, color)
assert colorize(text, color) == expected


def expect_disabled(text: str):
for color in AnsiColor:
assert colorize(text, color) == text


def test_colorize_1(monkeypatch):
monkeypatch.setenv('CLICOLOR', '1')

if stdout.isatty():
expect_enabled('abc')
else:
expect_disabled('abc')


def test_colorize_2(monkeypatch):
monkeypatch.setenv('CLICOLOR_FORCE', '1')

expect_enabled('abc')


def test_colorize_3(monkeypatch):
monkeypatch.setenv('CLICOLOR', '0')

expect_disabled('abc')

0 comments on commit 224b3d6

Please sign in to comment.