Skip to content

Commit

Permalink
Disable color when redirecting or piping stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
dosisod committed Dec 27, 2023
1 parent 42292a4 commit 1e49b1d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion refurb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1e49b1d

Please sign in to comment.