Skip to content

Commit 15b0e9d

Browse files
Fix forcing color through termcolor
Since termcolor 2.1.0, the library will now detect whether or not the process is running a TTY and disable outputting color if so. This broke using the `--color` argument to force color when piping to another process (e.g. `less`), as well as our pytest function for checking rendered output. The fix is ensuring that we set the environment variable FORCE_COLOR in the cases where we want color, and might not have a TTY (e.g. in the pytest, or when using `--color` option).
1 parent 5f9b88a commit 15b0e9d

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

pytest-shutil/pytest_shutil/cmdline.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def chdir(dirname):
5252
class PrettyFormatter(object):
5353
def __init__(self, color=True):
5454
from termcolor import colored
55+
if color is False:
56+
os.environ["FORCE_COLOR"] = "true"
5557
self.color = color
5658
self.colored = colored
5759
self.buffer = []

pytest-shutil/tests/unit/test_cmdline.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def test_umask(workspace):
1010
f.touch()
1111
assert (f.stat().st_mode & 0o777) == 0o464
1212

13-
def test_pretty_formatter():
13+
def test_pretty_formatter(monkeypatch):
14+
monkeypatch.setenv("FORCE_COLOR", "1")
1415
f = cmdline.PrettyFormatter()
1516
f.title('A Title')
1617
f.hr()

0 commit comments

Comments
 (0)