From 1e49b1ddc8ffcc77405417bfd946d02df3dbbb3b Mon Sep 17 00:00:00 2001 From: dosisod <39638017+dosisod@users.noreply.github.com> Date: Tue, 26 Dec 2023 20:38:44 -0800 Subject: [PATCH] Disable color when redirecting or piping stdout --- README.md | 2 ++ refurb/settings.py | 2 +- test/conftest.py | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 test/conftest.py diff --git a/README.md b/README.md index 4a159ad..a6e1f36 100644 --- a/README.md +++ b/README.md @@ -316,6 +316,8 @@ Color output is enabled by default in Refurb. To disable it, do one of the follo * Set `color = false` in the config file. +* Pipe/redirect Refurb output to another program or file. + ## Developing / Contributing ### Setup diff --git a/refurb/settings.py b/refurb/settings.py index aa185f3..6e69881 100644 --- a/refurb/settings.py +++ b/refurb/settings.py @@ -52,7 +52,7 @@ def __post_init__(self) -> None: 'refurb: "enable all" and "disable all" can\'t be used at the same time' # noqa: E501 ) - if os.getenv("NO_COLOR"): + if os.getenv("NO_COLOR") or not sys.stdout.isatty(): self.color = False @staticmethod diff --git a/test/conftest.py b/test/conftest.py new file mode 100644 index 0000000..02947c9 --- /dev/null +++ b/test/conftest.py @@ -0,0 +1,10 @@ +from unittest.mock import patch +import pytest + +@pytest.fixture(autouse=True) +def fake_tty(): + # Pytest doesnt run in a TTY, so the new TTY detection code is causing a lot of color related + # tests to fail. This hack makes it so color is always enabled, like it would in a normal TTY. + with patch("sys.stdout.isatty") as p: + p.return_value = True + yield p