Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terminalwriter.should_do_markup: support $NO_COLOR #258

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions py/_io/terminalwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ def should_do_markup(file):
return True
if os.environ.get('PY_COLORS') == '0':
return False
if 'NO_COLOR' in os.environ:
return False
return hasattr(file, 'isatty') and file.isatty() \
and os.environ.get('TERM') != 'dumb' \
and not (sys.platform.startswith('java') and os._name == 'nt')
Expand Down
36 changes: 36 additions & 0 deletions testing/io_/test_terminalwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,39 @@ def test_should_do_markup_PY_COLORS_eq_0(monkeypatch):
tw.line("hello", bold=True)
s = f.getvalue()
assert s == "hello\n"

def test_should_do_markup(monkeypatch):
monkeypatch.delenv("PY_COLORS", raising=False)
monkeypatch.delenv("NO_COLOR", raising=False)

should_do_markup = terminalwriter.should_do_markup

f = py.io.TextIO()
f.isatty = lambda: True

assert should_do_markup(f) is True

# NO_COLOR without PY_COLORS.
monkeypatch.setenv("NO_COLOR", "0")
assert should_do_markup(f) is False
monkeypatch.setenv("NO_COLOR", "1")
assert should_do_markup(f) is False
monkeypatch.setenv("NO_COLOR", "any")
assert should_do_markup(f) is False

# PY_COLORS overrides NO_COLOR ("0" and "1" only).
monkeypatch.setenv("PY_COLORS", "1")
assert should_do_markup(f) is True
monkeypatch.setenv("PY_COLORS", "0")
assert should_do_markup(f) is False
# Uses NO_COLOR.
monkeypatch.setenv("PY_COLORS", "any")
assert should_do_markup(f) is False
monkeypatch.delenv("NO_COLOR")
assert should_do_markup(f) is True

# Back to defaults.
monkeypatch.delenv("PY_COLORS")
assert should_do_markup(f) is True
f.isatty = lambda: False
assert should_do_markup(f) is False