2008-08-16 23:26:59 +08:00
|
|
|
import py
|
2009-03-11 09:40:08 +08:00
|
|
|
import os, sys
|
2010-01-14 00:15:54 +08:00
|
|
|
from py._io import terminalwriter
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-08-22 18:45:58 +08:00
|
|
|
def test_terminal_width_COLUMNS(monkeypatch):
|
|
|
|
""" Dummy test for get_terminal_width
|
|
|
|
"""
|
|
|
|
fcntl = py.test.importorskip("fcntl")
|
|
|
|
monkeypatch.setattr(fcntl, 'ioctl', lambda *args: int('x'))
|
|
|
|
monkeypatch.setenv('COLUMNS', '42')
|
2010-01-19 17:34:41 +08:00
|
|
|
assert terminalwriter.get_terminal_width() == 42
|
2009-08-22 18:45:58 +08:00
|
|
|
monkeypatch.delenv('COLUMNS', raising=False)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-04-04 01:45:25 +08:00
|
|
|
def test_terminalwriter_defaultwidth_80(monkeypatch):
|
|
|
|
monkeypatch.setattr(terminalwriter, '_getdimensions', lambda: 0/0)
|
2009-08-22 18:45:58 +08:00
|
|
|
monkeypatch.delenv('COLUMNS', raising=False)
|
2009-04-04 01:45:25 +08:00
|
|
|
tw = py.io.TerminalWriter()
|
2010-01-19 17:34:41 +08:00
|
|
|
assert tw.fullwidth == 80
|
|
|
|
|
|
|
|
def test_terminalwriter_getdimensions_bogus(monkeypatch):
|
|
|
|
monkeypatch.setattr(terminalwriter, '_getdimensions', lambda: (10,10))
|
|
|
|
monkeypatch.delenv('COLUMNS', raising=False)
|
|
|
|
tw = py.io.TerminalWriter()
|
|
|
|
assert tw.fullwidth == 80
|
2009-08-22 18:45:58 +08:00
|
|
|
|
|
|
|
def test_terminalwriter_computes_width(monkeypatch):
|
|
|
|
monkeypatch.setattr(terminalwriter, 'get_terminal_width', lambda: 42)
|
|
|
|
tw = py.io.TerminalWriter()
|
|
|
|
assert tw.fullwidth == 42
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
def test_terminalwriter_default_instantiation():
|
|
|
|
tw = py.io.TerminalWriter(stringio=True)
|
|
|
|
assert hasattr(tw, 'stringio')
|
|
|
|
|
2009-05-13 19:06:32 +08:00
|
|
|
def test_terminalwriter_dumb_term_no_markup(monkeypatch):
|
|
|
|
monkeypatch.setattr(os, 'environ', {'TERM': 'dumb', 'PATH': ''})
|
2009-08-21 02:47:39 +08:00
|
|
|
class MyFile:
|
|
|
|
def isatty(self):
|
|
|
|
return True
|
|
|
|
monkeypatch.setattr(sys, 'stdout', MyFile())
|
2010-04-23 18:05:29 +08:00
|
|
|
try:
|
|
|
|
assert sys.stdout.isatty()
|
|
|
|
tw = py.io.TerminalWriter()
|
|
|
|
assert not tw.hasmarkup
|
|
|
|
finally:
|
|
|
|
monkeypatch.undo()
|
2009-05-13 19:06:32 +08:00
|
|
|
|
2009-08-21 02:47:39 +08:00
|
|
|
def test_unicode_encoding():
|
2009-08-29 21:51:49 +08:00
|
|
|
msg = py.builtin._totext('b\u00f6y', 'utf8')
|
2009-08-21 02:47:39 +08:00
|
|
|
for encoding in 'utf8', 'latin1':
|
2009-08-29 21:51:49 +08:00
|
|
|
l = []
|
|
|
|
tw = py.io.TerminalWriter(l.append, encoding=encoding)
|
2009-08-21 02:47:39 +08:00
|
|
|
tw.line(msg)
|
2009-10-30 03:10:05 +08:00
|
|
|
assert l[0].strip() == msg.encode(encoding)
|
2009-08-21 02:47:39 +08:00
|
|
|
|
2010-01-14 01:04:58 +08:00
|
|
|
class TestTerminalWriter:
|
|
|
|
def pytest_generate_tests(self, metafunc):
|
|
|
|
if "tw" in metafunc.funcargnames:
|
|
|
|
metafunc.addcall(id="path", param="path")
|
|
|
|
metafunc.addcall(id="stringio", param="stringio")
|
|
|
|
metafunc.addcall(id="callable", param="callable")
|
|
|
|
def pytest_funcarg__tw(self, request):
|
|
|
|
if request.param == "path":
|
|
|
|
tmpdir = request.getfuncargvalue("tmpdir")
|
|
|
|
p = tmpdir.join("tmpfile")
|
|
|
|
tw = py.io.TerminalWriter(p.open('w+'))
|
|
|
|
def getlines():
|
|
|
|
tw._file.flush()
|
|
|
|
return p.open('r').readlines()
|
|
|
|
elif request.param == "stringio":
|
|
|
|
tw = py.io.TerminalWriter(stringio=True)
|
|
|
|
def getlines():
|
|
|
|
tw.stringio.seek(0)
|
|
|
|
return tw.stringio.readlines()
|
|
|
|
elif request.param == "callable":
|
|
|
|
writes = []
|
|
|
|
tw = py.io.TerminalWriter(writes.append)
|
|
|
|
def getlines():
|
|
|
|
io = py.io.TextIO()
|
|
|
|
io.write("".join(writes))
|
|
|
|
io.seek(0)
|
|
|
|
return io.readlines()
|
|
|
|
tw.getlines = getlines
|
|
|
|
return tw
|
|
|
|
|
|
|
|
def test_line(self, tw):
|
2008-08-16 23:26:59 +08:00
|
|
|
tw.line("hello")
|
2010-01-14 01:04:58 +08:00
|
|
|
l = tw.getlines()
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(l) == 1
|
|
|
|
assert l[0] == "hello\n"
|
|
|
|
|
2010-01-14 01:04:58 +08:00
|
|
|
def test_line_unicode(self, tw):
|
2009-08-06 20:34:19 +08:00
|
|
|
for encoding in 'utf8', 'latin1':
|
|
|
|
tw._encoding = encoding
|
2009-08-29 21:51:49 +08:00
|
|
|
msg = py.builtin._totext('b\u00f6y', 'utf8')
|
2009-08-06 20:34:19 +08:00
|
|
|
tw.line(msg)
|
2010-01-14 01:04:58 +08:00
|
|
|
l = tw.getlines()
|
2009-08-21 02:47:39 +08:00
|
|
|
assert l[0] == msg + "\n"
|
2009-08-06 20:34:19 +08:00
|
|
|
|
2010-01-14 01:04:58 +08:00
|
|
|
def test_sep_no_title(self, tw):
|
2008-08-16 23:26:59 +08:00
|
|
|
tw.sep("-", fullwidth=60)
|
2010-01-14 01:04:58 +08:00
|
|
|
l = tw.getlines()
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(l) == 1
|
|
|
|
assert l[0] == "-" * 60 + "\n"
|
|
|
|
|
2010-01-14 01:04:58 +08:00
|
|
|
def test_sep_with_title(self, tw):
|
2008-08-16 23:26:59 +08:00
|
|
|
tw.sep("-", "hello", fullwidth=60)
|
2010-01-14 01:04:58 +08:00
|
|
|
l = tw.getlines()
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(l) == 1
|
|
|
|
assert l[0] == "-" * 26 + " hello " + "-" * 27 + "\n"
|
|
|
|
|
2009-10-16 02:10:06 +08:00
|
|
|
@py.test.mark.skipif("sys.platform == 'win32'")
|
2010-01-14 01:04:58 +08:00
|
|
|
def test__escaped(self, tw):
|
2008-08-16 23:26:59 +08:00
|
|
|
text2 = tw._escaped("hello", (31))
|
|
|
|
assert text2.find("hello") != -1
|
|
|
|
|
2009-10-16 02:10:06 +08:00
|
|
|
@py.test.mark.skipif("sys.platform == 'win32'")
|
2010-01-14 01:04:58 +08:00
|
|
|
def test_markup(self, tw):
|
2008-08-16 23:26:59 +08:00
|
|
|
for bold in (True, False):
|
|
|
|
for color in ("red", "green"):
|
|
|
|
text2 = tw.markup("hello", **{color: True, 'bold': bold})
|
|
|
|
assert text2.find("hello") != -1
|
|
|
|
py.test.raises(ValueError, "tw.markup('x', wronkw=3)")
|
|
|
|
py.test.raises(ValueError, "tw.markup('x', wronkw=0)")
|
|
|
|
|
2010-01-14 01:04:58 +08:00
|
|
|
def test_line_write_markup(self, tw):
|
2008-08-16 23:26:59 +08:00
|
|
|
tw.hasmarkup = True
|
|
|
|
tw.line("x", bold=True)
|
|
|
|
tw.write("x\n", red=True)
|
2010-01-14 01:04:58 +08:00
|
|
|
l = tw.getlines()
|
2009-10-16 02:10:06 +08:00
|
|
|
if sys.platform != "win32":
|
|
|
|
assert len(l[0]) > 2, l
|
|
|
|
assert len(l[1]) > 2, l
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-01-14 01:04:58 +08:00
|
|
|
def test_attr_fullwidth(self, tw):
|
2008-08-16 23:26:59 +08:00
|
|
|
tw.sep("-", "hello", fullwidth=70)
|
|
|
|
tw.fullwidth = 70
|
|
|
|
tw.sep("-", "hello")
|
2010-01-14 01:04:58 +08:00
|
|
|
l = tw.getlines()
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(l[0]) == len(l[1])
|
|
|
|
|
|
|
|
|
|
|
|
def test_attr_hasmarkup():
|
|
|
|
tw = py.io.TerminalWriter(stringio=True)
|
|
|
|
assert not tw.hasmarkup
|
|
|
|
tw.hasmarkup = True
|
|
|
|
tw.line("hello", bold=True)
|
|
|
|
s = tw.stringio.getvalue()
|
|
|
|
assert len(s) > len("hello")
|