From 307add025b5464e87a55b49c6e5e4ce4c6373eee Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 16 Nov 2019 12:53:49 +0200 Subject: [PATCH] Simplify a FormattedExcinfo test The previous test was better in that it used fakes to test all of the real code paths. The problem with that is that it makes it impossible to simplify the code with `isinstance` checks. So let's just simulate the issue directly with a monkeypatch. --- src/_pytest/_code/code.py | 4 +-- testing/code/test_excinfo.py | 63 +++++------------------------------- 2 files changed, 9 insertions(+), 58 deletions(-) diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 19d5efaa6..334365042 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -283,8 +283,6 @@ class Traceback(list): access to Traceback entries. """ - Entry = TracebackEntry - def __init__(self, tb, excinfo=None): """ initialize from given python traceback object and ExceptionInfo """ self._excinfo = excinfo @@ -292,7 +290,7 @@ class Traceback(list): def f(cur): while cur is not None: - yield self.Entry(cur, excinfo=excinfo) + yield TracebackEntry(cur, excinfo=excinfo) cur = cur.tb_next list.__init__(self, f(tb)) diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index b83ad93e2..f08808648 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -502,65 +502,18 @@ raise ValueError() assert repr.reprtraceback.reprentries[1].lines[0] == "> ???" assert repr.chain[0][0].reprentries[1].lines[0] == "> ???" - def test_repr_source_failing_fullsource(self): + def test_repr_source_failing_fullsource(self, monkeypatch) -> None: pr = FormattedExcinfo() - class FakeCode: - class raw: - co_filename = "?" + try: + 1 / 0 + except ZeroDivisionError: + excinfo = ExceptionInfo.from_current() - path = "?" - firstlineno = 5 + with monkeypatch.context() as m: + m.setattr(_pytest._code.Code, "fullsource", property(lambda self: None)) + repr = pr.repr_excinfo(excinfo) - def fullsource(self): - return None - - fullsource = property(fullsource) - - class FakeFrame: - code = FakeCode() - f_locals = {} - f_globals = {} - - class FakeTracebackEntry(_pytest._code.Traceback.Entry): - def __init__(self, tb, excinfo=None): - self.lineno = 5 + 3 - - @property - def frame(self): - return FakeFrame() - - class Traceback(_pytest._code.Traceback): - Entry = FakeTracebackEntry - - class FakeExcinfo(_pytest._code.ExceptionInfo): - typename = "Foo" - value = Exception() - - def __init__(self): - pass - - def exconly(self, tryshort): - return "EXC" - - def errisinstance(self, cls): - return False - - excinfo = FakeExcinfo() - - class FakeRawTB: - tb_next = None - - tb = FakeRawTB() - excinfo.traceback = Traceback(tb) - - fail = IOError() - repr = pr.repr_excinfo(excinfo) - assert repr.reprtraceback.reprentries[0].lines[0] == "> ???" - assert repr.chain[0][0].reprentries[0].lines[0] == "> ???" - - fail = py.error.ENOENT # noqa - repr = pr.repr_excinfo(excinfo) assert repr.reprtraceback.reprentries[0].lines[0] == "> ???" assert repr.chain[0][0].reprentries[0].lines[0] == "> ???"