From 03d730046bb864d2ff94b9ce01fcf87a462974d1 Mon Sep 17 00:00:00 2001 From: Ernest MARIN Date: Wed, 9 Mar 2022 11:22:42 +0100 Subject: [PATCH 1/4] Don't use Ansi Colors if not supported. --- detect_secrets/util/color.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/detect_secrets/util/color.py b/detect_secrets/util/color.py index 9d9f869b0..0f8799cb1 100644 --- a/detect_secrets/util/color.py +++ b/detect_secrets/util/color.py @@ -1,4 +1,11 @@ from enum import Enum +from os import getenv +from sys import stdout + + +def support_ansi_colors(): + return (getenv('CLICOLOR', '1') != '0' and stdout.isatty())\ + or getenv('CLICOLOR_FORCE', '0') != '0' class AnsiColor(Enum): @@ -11,6 +18,9 @@ class AnsiColor(Enum): def colorize(text: str, color: AnsiColor) -> str: + if not support_ansi_colors(): + return text + return '\x1b{}{}\x1b{}'.format( color.value, text, From 6a5576b6221990142179fa557a90cfed9f0f4bae Mon Sep 17 00:00:00 2001 From: Ernest MARIN Date: Sat, 2 Apr 2022 16:10:30 +0200 Subject: [PATCH 2/4] Add test coverage for utils/color --- detect_secrets/util/color.py | 2 +- tests/util/color_test.py | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/util/color_test.py diff --git a/detect_secrets/util/color.py b/detect_secrets/util/color.py index 0f8799cb1..d817ffe55 100644 --- a/detect_secrets/util/color.py +++ b/detect_secrets/util/color.py @@ -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' diff --git a/tests/util/color_test.py b/tests/util/color_test.py new file mode 100644 index 000000000..4e8641e9f --- /dev/null +++ b/tests/util/color_test.py @@ -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') From e13e5aa2cabdf9efcf3e99d2b3824adc17c21476 Mon Sep 17 00:00:00 2001 From: Ernest MARIN Date: Tue, 5 Apr 2022 10:25:12 +0200 Subject: [PATCH 3/4] Rename util.color.supports_ansi_colors --- detect_secrets/util/color.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/detect_secrets/util/color.py b/detect_secrets/util/color.py index d817ffe55..12c114bc3 100644 --- a/detect_secrets/util/color.py +++ b/detect_secrets/util/color.py @@ -3,7 +3,7 @@ from sys import stdout -def support_ansi_colors() -> bool: +def supports_ansi_colors() -> bool: return (getenv('CLICOLOR', '1') != '0' and stdout.isatty())\ or getenv('CLICOLOR_FORCE', '0') != '0' @@ -18,7 +18,7 @@ class AnsiColor(Enum): def colorize(text: str, color: AnsiColor) -> str: - if not support_ansi_colors(): + if not supports_ansi_colors(): return text return '\x1b{}{}\x1b{}'.format( From 41ce349c9af47bb90e0124af7c540a16f027dd99 Mon Sep 17 00:00:00 2001 From: Ernest MARIN Date: Wed, 6 Apr 2022 10:49:26 +0200 Subject: [PATCH 4/4] Rename color_test functions to better describe what the test is doing --- tests/util/color_test.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/util/color_test.py b/tests/util/color_test.py index 4e8641e9f..fc70a1b24 100644 --- a/tests/util/color_test.py +++ b/tests/util/color_test.py @@ -23,7 +23,7 @@ def expect_disabled(text: str): assert colorize(text, color) == text -def test_colorize_1(monkeypatch): +def test_colorize_enabled_terminal_disabled_piped(monkeypatch): monkeypatch.setenv('CLICOLOR', '1') if stdout.isatty(): @@ -32,13 +32,13 @@ def test_colorize_1(monkeypatch): expect_disabled('abc') -def test_colorize_2(monkeypatch): +def test_colorize_enabled_force(monkeypatch): monkeypatch.setenv('CLICOLOR_FORCE', '1') expect_enabled('abc') -def test_colorize_3(monkeypatch): +def test_colorize_disabled(monkeypatch): monkeypatch.setenv('CLICOLOR', '0') expect_disabled('abc')