Skip to content

Commit 2f03e5a

Browse files
Merge pull request #258 from blueyed/NO_COLOR
terminalwriter.should_do_markup: support $NO_COLOR
2 parents e116b2b + 56384fb commit 2f03e5a

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

py/_io/terminalwriter.py

+2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ def should_do_markup(file):
133133
return True
134134
if os.environ.get('PY_COLORS') == '0':
135135
return False
136+
if 'NO_COLOR' in os.environ:
137+
return False
136138
return hasattr(file, 'isatty') and file.isatty() \
137139
and os.environ.get('TERM') != 'dumb' \
138140
and not (sys.platform.startswith('java') and os._name == 'nt')

testing/io_/test_terminalwriter.py

+36
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,39 @@ def test_should_do_markup_PY_COLORS_eq_0(monkeypatch):
303303
tw.line("hello", bold=True)
304304
s = f.getvalue()
305305
assert s == "hello\n"
306+
307+
def test_should_do_markup(monkeypatch):
308+
monkeypatch.delenv("PY_COLORS", raising=False)
309+
monkeypatch.delenv("NO_COLOR", raising=False)
310+
311+
should_do_markup = terminalwriter.should_do_markup
312+
313+
f = py.io.TextIO()
314+
f.isatty = lambda: True
315+
316+
assert should_do_markup(f) is True
317+
318+
# NO_COLOR without PY_COLORS.
319+
monkeypatch.setenv("NO_COLOR", "0")
320+
assert should_do_markup(f) is False
321+
monkeypatch.setenv("NO_COLOR", "1")
322+
assert should_do_markup(f) is False
323+
monkeypatch.setenv("NO_COLOR", "any")
324+
assert should_do_markup(f) is False
325+
326+
# PY_COLORS overrides NO_COLOR ("0" and "1" only).
327+
monkeypatch.setenv("PY_COLORS", "1")
328+
assert should_do_markup(f) is True
329+
monkeypatch.setenv("PY_COLORS", "0")
330+
assert should_do_markup(f) is False
331+
# Uses NO_COLOR.
332+
monkeypatch.setenv("PY_COLORS", "any")
333+
assert should_do_markup(f) is False
334+
monkeypatch.delenv("NO_COLOR")
335+
assert should_do_markup(f) is True
336+
337+
# Back to defaults.
338+
monkeypatch.delenv("PY_COLORS")
339+
assert should_do_markup(f) is True
340+
f.isatty = lambda: False
341+
assert should_do_markup(f) is False

0 commit comments

Comments
 (0)