Commits

Marc Abramowitz  committed 911572f

Use env var PY_COLORS=1 to force color output. PY_COLORS=0 to force disable.

Add tests.

  • Participants
  • Parent commits 3a2c807
  • Branches py_force_color

Comments (0)

Files changed (2)

File py/_io/terminalwriter.py

         file.flush()
 
 def should_do_markup(file):
-    if os.environ.get('PY_FORCE_COLOR'):
+    if os.environ.get('PY_COLORS') == '1':
         return True
+    if os.environ.get('PY_COLORS') == '0':
+        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')

File testing/io_/test_terminalwriter.py

     assert len(text2) >= len("hello\n")
     assert u'\x1b[50m' in text2
     assert u'\x1b[0m' in text2
+
+def test_should_do_markup_PY_COLORS_eq_1(monkeypatch):
+    monkeypatch.setitem(os.environ, 'PY_COLORS', '1')
+    tw = py.io.TerminalWriter(stringio=True)
+    assert tw.hasmarkup
+    tw.line("hello", bold=True)
+    s = tw.stringio.getvalue()
+    assert len(s) > len("hello\n")
+    assert u'\x1b[1m' in s
+    assert u'\x1b[0m' in s
+
+def test_should_do_markup_PY_COLORS_eq_0(monkeypatch):
+    monkeypatch.setitem(os.environ, 'PY_COLORS', '0')
+    f = py.io.TextIO()
+    f.isatty = lambda: True
+    tw = py.io.TerminalWriter(file=f)
+    assert not tw.hasmarkup
+    tw.line("hello", bold=True)
+    s = f.getvalue()
+    assert s == "hello\n"
+