Move TWMock class to a fixture

Using a relative import like before was not very nice
This commit is contained in:
Bruno Oliveira 2019-08-26 11:32:57 -03:00
parent 3c82b1cb97
commit 7a69365486
3 changed files with 194 additions and 203 deletions

View File

@ -1,8 +1,6 @@
import sys
from unittest import mock
from test_excinfo import TWMock
import _pytest._code
import pytest
@ -168,17 +166,15 @@ class TestTracebackEntry:
class TestReprFuncArgs:
def test_not_raise_exception_with_mixed_encoding(self):
def test_not_raise_exception_with_mixed_encoding(self, tw_mock):
from _pytest._code.code import ReprFuncArgs
tw = TWMock()
args = [("unicode_string", "São Paulo"), ("utf8_string", b"S\xc3\xa3o Paulo")]
r = ReprFuncArgs(args)
r.toterminal(tw)
r.toterminal(tw_mock)
assert (
tw.lines[0]
tw_mock.lines[0]
== r"unicode_string = São Paulo, utf8_string = b'S\xc3\xa3o Paulo'"
)

View File

@ -31,33 +31,6 @@ def limited_recursion_depth():
sys.setrecursionlimit(before)
class TWMock:
WRITE = object()
def __init__(self):
self.lines = []
self.is_writing = False
def sep(self, sep, line=None):
self.lines.append((sep, line))
def write(self, msg, **kw):
self.lines.append((TWMock.WRITE, msg))
def line(self, line, **kw):
self.lines.append(line)
def markup(self, text, **kw):
return text
def get_write_msg(self, idx):
flag, msg = self.lines[idx]
assert flag == TWMock.WRITE
return msg
fullwidth = 80
def test_excinfo_simple() -> None:
try:
raise ValueError
@ -658,7 +631,7 @@ raise ValueError()
assert loc.lineno == 3
# assert loc.message == "ValueError: hello"
def test_repr_tracebackentry_lines2(self, importasmod):
def test_repr_tracebackentry_lines2(self, importasmod, tw_mock):
mod = importasmod(
"""
def func1(m, x, y, z):
@ -678,13 +651,12 @@ raise ValueError()
p = FormattedExcinfo(funcargs=True)
repr_entry = p.repr_traceback_entry(entry)
assert repr_entry.reprfuncargs.args == reprfuncargs.args
tw = TWMock()
repr_entry.toterminal(tw)
assert tw.lines[0] == "m = " + repr("m" * 90)
assert tw.lines[1] == "x = 5, y = 13"
assert tw.lines[2] == "z = " + repr("z" * 120)
repr_entry.toterminal(tw_mock)
assert tw_mock.lines[0] == "m = " + repr("m" * 90)
assert tw_mock.lines[1] == "x = 5, y = 13"
assert tw_mock.lines[2] == "z = " + repr("z" * 120)
def test_repr_tracebackentry_lines_var_kw_args(self, importasmod):
def test_repr_tracebackentry_lines_var_kw_args(self, importasmod, tw_mock):
mod = importasmod(
"""
def func1(x, *y, **z):
@ -703,9 +675,8 @@ raise ValueError()
p = FormattedExcinfo(funcargs=True)
repr_entry = p.repr_traceback_entry(entry)
assert repr_entry.reprfuncargs.args == reprfuncargs.args
tw = TWMock()
repr_entry.toterminal(tw)
assert tw.lines[0] == "x = 'a', y = ('b',), z = {'c': 'd'}"
repr_entry.toterminal(tw_mock)
assert tw_mock.lines[0] == "x = 'a', y = ('b',), z = {'c': 'd'}"
def test_repr_tracebackentry_short(self, importasmod):
mod = importasmod(
@ -842,7 +813,7 @@ raise ValueError()
assert p._makepath(__file__) == __file__
p.repr_traceback(excinfo)
def test_repr_excinfo_addouterr(self, importasmod):
def test_repr_excinfo_addouterr(self, importasmod, tw_mock):
mod = importasmod(
"""
def entry():
@ -852,10 +823,9 @@ raise ValueError()
excinfo = pytest.raises(ValueError, mod.entry)
repr = excinfo.getrepr()
repr.addsection("title", "content")
twmock = TWMock()
repr.toterminal(twmock)
assert twmock.lines[-1] == "content"
assert twmock.lines[-2] == ("-", "title")
repr.toterminal(tw_mock)
assert tw_mock.lines[-1] == "content"
assert tw_mock.lines[-2] == ("-", "title")
def test_repr_excinfo_reprcrash(self, importasmod):
mod = importasmod(
@ -920,7 +890,7 @@ raise ValueError()
x = str(MyRepr())
assert x == "я"
def test_toterminal_long(self, importasmod):
def test_toterminal_long(self, importasmod, tw_mock):
mod = importasmod(
"""
def g(x):
@ -932,27 +902,26 @@ raise ValueError()
excinfo = pytest.raises(ValueError, mod.f)
excinfo.traceback = excinfo.traceback.filter()
repr = excinfo.getrepr()
tw = TWMock()
repr.toterminal(tw)
assert tw.lines[0] == ""
tw.lines.pop(0)
assert tw.lines[0] == " def f():"
assert tw.lines[1] == "> g(3)"
assert tw.lines[2] == ""
line = tw.get_write_msg(3)
repr.toterminal(tw_mock)
assert tw_mock.lines[0] == ""
tw_mock.lines.pop(0)
assert tw_mock.lines[0] == " def f():"
assert tw_mock.lines[1] == "> g(3)"
assert tw_mock.lines[2] == ""
line = tw_mock.get_write_msg(3)
assert line.endswith("mod.py")
assert tw.lines[4] == (":5: ")
assert tw.lines[5] == ("_ ", None)
assert tw.lines[6] == ""
assert tw.lines[7] == " def g(x):"
assert tw.lines[8] == "> raise ValueError(x)"
assert tw.lines[9] == "E ValueError: 3"
assert tw.lines[10] == ""
line = tw.get_write_msg(11)
assert tw_mock.lines[4] == (":5: ")
assert tw_mock.lines[5] == ("_ ", None)
assert tw_mock.lines[6] == ""
assert tw_mock.lines[7] == " def g(x):"
assert tw_mock.lines[8] == "> raise ValueError(x)"
assert tw_mock.lines[9] == "E ValueError: 3"
assert tw_mock.lines[10] == ""
line = tw_mock.get_write_msg(11)
assert line.endswith("mod.py")
assert tw.lines[12] == ":3: ValueError"
assert tw_mock.lines[12] == ":3: ValueError"
def test_toterminal_long_missing_source(self, importasmod, tmpdir):
def test_toterminal_long_missing_source(self, importasmod, tmpdir, tw_mock):
mod = importasmod(
"""
def g(x):
@ -965,25 +934,24 @@ raise ValueError()
tmpdir.join("mod.py").remove()
excinfo.traceback = excinfo.traceback.filter()
repr = excinfo.getrepr()
tw = TWMock()
repr.toterminal(tw)
assert tw.lines[0] == ""
tw.lines.pop(0)
assert tw.lines[0] == "> ???"
assert tw.lines[1] == ""
line = tw.get_write_msg(2)
repr.toterminal(tw_mock)
assert tw_mock.lines[0] == ""
tw_mock.lines.pop(0)
assert tw_mock.lines[0] == "> ???"
assert tw_mock.lines[1] == ""
line = tw_mock.get_write_msg(2)
assert line.endswith("mod.py")
assert tw.lines[3] == ":5: "
assert tw.lines[4] == ("_ ", None)
assert tw.lines[5] == ""
assert tw.lines[6] == "> ???"
assert tw.lines[7] == "E ValueError: 3"
assert tw.lines[8] == ""
line = tw.get_write_msg(9)
assert tw_mock.lines[3] == ":5: "
assert tw_mock.lines[4] == ("_ ", None)
assert tw_mock.lines[5] == ""
assert tw_mock.lines[6] == "> ???"
assert tw_mock.lines[7] == "E ValueError: 3"
assert tw_mock.lines[8] == ""
line = tw_mock.get_write_msg(9)
assert line.endswith("mod.py")
assert tw.lines[10] == ":3: ValueError"
assert tw_mock.lines[10] == ":3: ValueError"
def test_toterminal_long_incomplete_source(self, importasmod, tmpdir):
def test_toterminal_long_incomplete_source(self, importasmod, tmpdir, tw_mock):
mod = importasmod(
"""
def g(x):
@ -996,25 +964,24 @@ raise ValueError()
tmpdir.join("mod.py").write("asdf")
excinfo.traceback = excinfo.traceback.filter()
repr = excinfo.getrepr()
tw = TWMock()
repr.toterminal(tw)
assert tw.lines[0] == ""
tw.lines.pop(0)
assert tw.lines[0] == "> ???"
assert tw.lines[1] == ""
line = tw.get_write_msg(2)
repr.toterminal(tw_mock)
assert tw_mock.lines[0] == ""
tw_mock.lines.pop(0)
assert tw_mock.lines[0] == "> ???"
assert tw_mock.lines[1] == ""
line = tw_mock.get_write_msg(2)
assert line.endswith("mod.py")
assert tw.lines[3] == ":5: "
assert tw.lines[4] == ("_ ", None)
assert tw.lines[5] == ""
assert tw.lines[6] == "> ???"
assert tw.lines[7] == "E ValueError: 3"
assert tw.lines[8] == ""
line = tw.get_write_msg(9)
assert tw_mock.lines[3] == ":5: "
assert tw_mock.lines[4] == ("_ ", None)
assert tw_mock.lines[5] == ""
assert tw_mock.lines[6] == "> ???"
assert tw_mock.lines[7] == "E ValueError: 3"
assert tw_mock.lines[8] == ""
line = tw_mock.get_write_msg(9)
assert line.endswith("mod.py")
assert tw.lines[10] == ":3: ValueError"
assert tw_mock.lines[10] == ":3: ValueError"
def test_toterminal_long_filenames(self, importasmod):
def test_toterminal_long_filenames(self, importasmod, tw_mock):
mod = importasmod(
"""
def f():
@ -1022,23 +989,22 @@ raise ValueError()
"""
)
excinfo = pytest.raises(ValueError, mod.f)
tw = TWMock()
path = py.path.local(mod.__file__)
old = path.dirpath().chdir()
try:
repr = excinfo.getrepr(abspath=False)
repr.toterminal(tw)
repr.toterminal(tw_mock)
x = py.path.local().bestrelpath(path)
if len(x) < len(str(path)):
msg = tw.get_write_msg(-2)
msg = tw_mock.get_write_msg(-2)
assert msg == "mod.py"
assert tw.lines[-1] == ":3: ValueError"
assert tw_mock.lines[-1] == ":3: ValueError"
repr = excinfo.getrepr(abspath=True)
repr.toterminal(tw)
msg = tw.get_write_msg(-2)
repr.toterminal(tw_mock)
msg = tw_mock.get_write_msg(-2)
assert msg == path
line = tw.lines[-1]
line = tw_mock.lines[-1]
assert line == ":3: ValueError"
finally:
old.chdir()
@ -1073,7 +1039,7 @@ raise ValueError()
repr.toterminal(tw)
assert tw.stringio.getvalue()
def test_traceback_repr_style(self, importasmod):
def test_traceback_repr_style(self, importasmod, tw_mock):
mod = importasmod(
"""
def f():
@ -1091,35 +1057,34 @@ raise ValueError()
excinfo.traceback[1].set_repr_style("short")
excinfo.traceback[2].set_repr_style("short")
r = excinfo.getrepr(style="long")
tw = TWMock()
r.toterminal(tw)
for line in tw.lines:
r.toterminal(tw_mock)
for line in tw_mock.lines:
print(line)
assert tw.lines[0] == ""
assert tw.lines[1] == " def f():"
assert tw.lines[2] == "> g()"
assert tw.lines[3] == ""
msg = tw.get_write_msg(4)
assert tw_mock.lines[0] == ""
assert tw_mock.lines[1] == " def f():"
assert tw_mock.lines[2] == "> g()"
assert tw_mock.lines[3] == ""
msg = tw_mock.get_write_msg(4)
assert msg.endswith("mod.py")
assert tw.lines[5] == ":3: "
assert tw.lines[6] == ("_ ", None)
tw.get_write_msg(7)
assert tw.lines[8].endswith("in g")
assert tw.lines[9] == " h()"
tw.get_write_msg(10)
assert tw.lines[11].endswith("in h")
assert tw.lines[12] == " i()"
assert tw.lines[13] == ("_ ", None)
assert tw.lines[14] == ""
assert tw.lines[15] == " def i():"
assert tw.lines[16] == "> raise ValueError()"
assert tw.lines[17] == "E ValueError"
assert tw.lines[18] == ""
msg = tw.get_write_msg(19)
assert tw_mock.lines[5] == ":3: "
assert tw_mock.lines[6] == ("_ ", None)
tw_mock.get_write_msg(7)
assert tw_mock.lines[8].endswith("in g")
assert tw_mock.lines[9] == " h()"
tw_mock.get_write_msg(10)
assert tw_mock.lines[11].endswith("in h")
assert tw_mock.lines[12] == " i()"
assert tw_mock.lines[13] == ("_ ", None)
assert tw_mock.lines[14] == ""
assert tw_mock.lines[15] == " def i():"
assert tw_mock.lines[16] == "> raise ValueError()"
assert tw_mock.lines[17] == "E ValueError"
assert tw_mock.lines[18] == ""
msg = tw_mock.get_write_msg(19)
msg.endswith("mod.py")
assert tw.lines[20] == ":9: ValueError"
assert tw_mock.lines[20] == ":9: ValueError"
def test_exc_chain_repr(self, importasmod):
def test_exc_chain_repr(self, importasmod, tw_mock):
mod = importasmod(
"""
class Err(Exception):
@ -1140,72 +1105,71 @@ raise ValueError()
)
excinfo = pytest.raises(AttributeError, mod.f)
r = excinfo.getrepr(style="long")
tw = TWMock()
r.toterminal(tw)
for line in tw.lines:
r.toterminal(tw_mock)
for line in tw_mock.lines:
print(line)
assert tw.lines[0] == ""
assert tw.lines[1] == " def f():"
assert tw.lines[2] == " try:"
assert tw.lines[3] == "> g()"
assert tw.lines[4] == ""
line = tw.get_write_msg(5)
assert tw_mock.lines[0] == ""
assert tw_mock.lines[1] == " def f():"
assert tw_mock.lines[2] == " try:"
assert tw_mock.lines[3] == "> g()"
assert tw_mock.lines[4] == ""
line = tw_mock.get_write_msg(5)
assert line.endswith("mod.py")
assert tw.lines[6] == ":6: "
assert tw.lines[7] == ("_ ", None)
assert tw.lines[8] == ""
assert tw.lines[9] == " def g():"
assert tw.lines[10] == "> raise ValueError()"
assert tw.lines[11] == "E ValueError"
assert tw.lines[12] == ""
line = tw.get_write_msg(13)
assert tw_mock.lines[6] == ":6: "
assert tw_mock.lines[7] == ("_ ", None)
assert tw_mock.lines[8] == ""
assert tw_mock.lines[9] == " def g():"
assert tw_mock.lines[10] == "> raise ValueError()"
assert tw_mock.lines[11] == "E ValueError"
assert tw_mock.lines[12] == ""
line = tw_mock.get_write_msg(13)
assert line.endswith("mod.py")
assert tw.lines[14] == ":12: ValueError"
assert tw.lines[15] == ""
assert tw_mock.lines[14] == ":12: ValueError"
assert tw_mock.lines[15] == ""
assert (
tw.lines[16]
tw_mock.lines[16]
== "The above exception was the direct cause of the following exception:"
)
assert tw.lines[17] == ""
assert tw.lines[18] == " def f():"
assert tw.lines[19] == " try:"
assert tw.lines[20] == " g()"
assert tw.lines[21] == " except Exception as e:"
assert tw.lines[22] == "> raise Err() from e"
assert tw.lines[23] == "E test_exc_chain_repr0.mod.Err"
assert tw.lines[24] == ""
line = tw.get_write_msg(25)
assert tw_mock.lines[17] == ""
assert tw_mock.lines[18] == " def f():"
assert tw_mock.lines[19] == " try:"
assert tw_mock.lines[20] == " g()"
assert tw_mock.lines[21] == " except Exception as e:"
assert tw_mock.lines[22] == "> raise Err() from e"
assert tw_mock.lines[23] == "E test_exc_chain_repr0.mod.Err"
assert tw_mock.lines[24] == ""
line = tw_mock.get_write_msg(25)
assert line.endswith("mod.py")
assert tw.lines[26] == ":8: Err"
assert tw.lines[27] == ""
assert tw_mock.lines[26] == ":8: Err"
assert tw_mock.lines[27] == ""
assert (
tw.lines[28]
tw_mock.lines[28]
== "During handling of the above exception, another exception occurred:"
)
assert tw.lines[29] == ""
assert tw.lines[30] == " def f():"
assert tw.lines[31] == " try:"
assert tw.lines[32] == " g()"
assert tw.lines[33] == " except Exception as e:"
assert tw.lines[34] == " raise Err() from e"
assert tw.lines[35] == " finally:"
assert tw.lines[36] == "> h()"
assert tw.lines[37] == ""
line = tw.get_write_msg(38)
assert tw_mock.lines[29] == ""
assert tw_mock.lines[30] == " def f():"
assert tw_mock.lines[31] == " try:"
assert tw_mock.lines[32] == " g()"
assert tw_mock.lines[33] == " except Exception as e:"
assert tw_mock.lines[34] == " raise Err() from e"
assert tw_mock.lines[35] == " finally:"
assert tw_mock.lines[36] == "> h()"
assert tw_mock.lines[37] == ""
line = tw_mock.get_write_msg(38)
assert line.endswith("mod.py")
assert tw.lines[39] == ":10: "
assert tw.lines[40] == ("_ ", None)
assert tw.lines[41] == ""
assert tw.lines[42] == " def h():"
assert tw.lines[43] == "> raise AttributeError()"
assert tw.lines[44] == "E AttributeError"
assert tw.lines[45] == ""
line = tw.get_write_msg(46)
assert tw_mock.lines[39] == ":10: "
assert tw_mock.lines[40] == ("_ ", None)
assert tw_mock.lines[41] == ""
assert tw_mock.lines[42] == " def h():"
assert tw_mock.lines[43] == "> raise AttributeError()"
assert tw_mock.lines[44] == "E AttributeError"
assert tw_mock.lines[45] == ""
line = tw_mock.get_write_msg(46)
assert line.endswith("mod.py")
assert tw.lines[47] == ":15: AttributeError"
assert tw_mock.lines[47] == ":15: AttributeError"
@pytest.mark.parametrize("mode", ["from_none", "explicit_suppress"])
def test_exc_repr_chain_suppression(self, importasmod, mode):
def test_exc_repr_chain_suppression(self, importasmod, mode, tw_mock):
"""Check that exc repr does not show chained exceptions in Python 3.
- When the exception is raised with "from None"
- Explicitly suppressed with "chain=False" to ExceptionInfo.getrepr().
@ -1226,24 +1190,23 @@ raise ValueError()
)
excinfo = pytest.raises(AttributeError, mod.f)
r = excinfo.getrepr(style="long", chain=mode != "explicit_suppress")
tw = TWMock()
r.toterminal(tw)
for line in tw.lines:
r.toterminal(tw_mock)
for line in tw_mock.lines:
print(line)
assert tw.lines[0] == ""
assert tw.lines[1] == " def f():"
assert tw.lines[2] == " try:"
assert tw.lines[3] == " g()"
assert tw.lines[4] == " except Exception:"
assert tw.lines[5] == "> raise AttributeError(){}".format(
assert tw_mock.lines[0] == ""
assert tw_mock.lines[1] == " def f():"
assert tw_mock.lines[2] == " try:"
assert tw_mock.lines[3] == " g()"
assert tw_mock.lines[4] == " except Exception:"
assert tw_mock.lines[5] == "> raise AttributeError(){}".format(
raise_suffix
)
assert tw.lines[6] == "E AttributeError"
assert tw.lines[7] == ""
line = tw.get_write_msg(8)
assert tw_mock.lines[6] == "E AttributeError"
assert tw_mock.lines[7] == ""
line = tw_mock.get_write_msg(8)
assert line.endswith("mod.py")
assert tw.lines[9] == ":6: AttributeError"
assert len(tw.lines) == 10
assert tw_mock.lines[9] == ":6: AttributeError"
assert len(tw_mock.lines) == 10
@pytest.mark.parametrize(
"reason, description",
@ -1304,7 +1267,7 @@ raise ValueError()
]
)
def test_exc_chain_repr_cycle(self, importasmod):
def test_exc_chain_repr_cycle(self, importasmod, tw_mock):
mod = importasmod(
"""
class Err(Exception):
@ -1325,9 +1288,8 @@ raise ValueError()
)
excinfo = pytest.raises(ZeroDivisionError, mod.unreraise)
r = excinfo.getrepr(style="short")
tw = TWMock()
r.toterminal(tw)
out = "\n".join(line for line in tw.lines if isinstance(line, str))
r.toterminal(tw_mock)
out = "\n".join(line for line in tw_mock.lines if isinstance(line, str))
expected_out = textwrap.dedent(
"""\
:13: in unreraise

View File

@ -55,3 +55,36 @@ def pytest_collection_modifyitems(config, items):
items[:] = fast_items + neutral_items + slow_items + slowest_items
yield
@pytest.fixture
def tw_mock():
"""Returns a mock terminal writer"""
class TWMock:
WRITE = object()
def __init__(self):
self.lines = []
self.is_writing = False
def sep(self, sep, line=None):
self.lines.append((sep, line))
def write(self, msg, **kw):
self.lines.append((TWMock.WRITE, msg))
def line(self, line, **kw):
self.lines.append(line)
def markup(self, text, **kw):
return text
def get_write_msg(self, idx):
flag, msg = self.lines[idx]
assert flag == TWMock.WRITE
return msg
fullwidth = 80
return TWMock()