From 40c3a28620e688ade363d9709aa9e6314024e2fd Mon Sep 17 00:00:00 2001 From: shivansh02 Date: Mon, 4 Mar 2024 18:21:28 +0530 Subject: [PATCH] support for NO_COLOR env var --- borgmatic/logger.py | 4 ++++ docs/how-to/set-up-backups.md | 2 +- tests/unit/test_logger.py | 28 ++++++++++++++++++++++++---- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/borgmatic/logger.py b/borgmatic/logger.py index bb9cbee..d8f749c 100644 --- a/borgmatic/logger.py +++ b/borgmatic/logger.py @@ -41,6 +41,10 @@ def should_do_markup(no_color, configs): if any(config.get('output', {}).get('color') is False for config in configs.values()): return False + no_color_env = os.environ.get('NO_COLOR', None) + if no_color_env is not None and to_bool(no_color_env): + return False + py_colors = os.environ.get('PY_COLORS', None) if py_colors is not None: diff --git a/docs/how-to/set-up-backups.md b/docs/how-to/set-up-backups.md index 22554dd..a50c3a3 100644 --- a/docs/how-to/set-up-backups.md +++ b/docs/how-to/set-up-backups.md @@ -406,7 +406,7 @@ source /usr/share/fish/vendor_completions.d/borgmatic.fish borgmatic produces colored terminal output by default. It is disabled when a non-interactive terminal is detected (like a cron job), or when you use the `--json` flag. Otherwise, you can disable it by passing the `--no-color` flag, -setting the environment variable `PY_COLORS=False`, or setting the `color` +setting the environment variables `PY_COLORS=False` or `NO_COLOR=True`, or setting the `color` option to `false` in the `output` section of configuration. diff --git a/tests/unit/test_logger.py b/tests/unit/test_logger.py index c69b932..505af68 100644 --- a/tests/unit/test_logger.py +++ b/tests/unit/test_logger.py @@ -69,7 +69,9 @@ def test_should_do_markup_prefers_any_false_config_value(): def test_should_do_markup_respects_PY_COLORS_environment_variable(): - flexmock(module.os.environ).should_receive('get').and_return('True') + flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return('True') + flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return(None) + flexmock(module).should_receive('to_bool').and_return(True) assert module.should_do_markup(no_color=False, configs={}) is True @@ -82,7 +84,7 @@ def test_should_do_markup_prefers_no_color_value_to_config_value(): ) -def test_should_do_markup_prefers_config_value_to_PY_COLORS(): +def test_should_do_markup_prefers_config_value_to_environment_variables(): flexmock(module.os.environ).should_receive('get').and_return('True') flexmock(module).should_receive('to_bool').and_return(True) @@ -92,7 +94,7 @@ def test_should_do_markup_prefers_config_value_to_PY_COLORS(): ) -def test_should_do_markup_prefers_no_color_value_to_PY_COLORS(): +def test_should_do_markup_prefers_no_color_value_to_environment_variables(): flexmock(module.os.environ).should_receive('get').and_return('True') flexmock(module).should_receive('to_bool').and_return(True) @@ -107,12 +109,30 @@ def test_should_do_markup_respects_interactive_console_value(): def test_should_do_markup_prefers_PY_COLORS_to_interactive_console_value(): - flexmock(module.os.environ).should_receive('get').and_return('True') + flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return('True') + flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return(None) flexmock(module).should_receive('to_bool').and_return(True) flexmock(module).should_receive('interactive_console').and_return(False) assert module.should_do_markup(no_color=False, configs={}) is True +def test_should_do_markup_prefers_NO_COLOR_to_interactive_console_value(): + flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(None) + flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return('True') + flexmock(module).should_receive('to_bool').and_return(True) + flexmock(module).should_receive('interactive_console').and_return(False) + + assert module.should_do_markup(no_color=False, configs={}) is False + +def test_should_do_markup_respects_NO_COLOR_environment_variable(): + flexmock(module.os.environ).should_receive('get').with_args('NO_COLOR', None).and_return('True') + flexmock(module.os.environ).should_receive('get').with_args('PY_COLORS', None).and_return(None) + + flexmock(module).should_receive('to_bool').and_return(True) + + assert module.should_do_markup(no_color=False, configs={}) is False + + def test_multi_stream_handler_logs_to_handler_for_log_level(): error_handler = flexmock()