-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #523 from Darewise/master
Don't use Ansi Colors if not supported.
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_enabled_terminal_disabled_piped(monkeypatch): | ||
monkeypatch.setenv('CLICOLOR', '1') | ||
|
||
if stdout.isatty(): | ||
expect_enabled('abc') | ||
else: | ||
expect_disabled('abc') | ||
|
||
|
||
def test_colorize_enabled_force(monkeypatch): | ||
monkeypatch.setenv('CLICOLOR_FORCE', '1') | ||
|
||
expect_enabled('abc') | ||
|
||
|
||
def test_colorize_disabled(monkeypatch): | ||
monkeypatch.setenv('CLICOLOR', '0') | ||
|
||
expect_disabled('abc') |