From 79ae86cc3f76d69460e1c7beca4ce95e68ab80a6 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 21 Jan 2020 20:06:14 +0100 Subject: [PATCH] tests: fix test_repr_traceback_with_invalid_cwd This never worked as expected (since a912d3745), and only py38-windows triggered the mocked `os.getcwd` unintentionally, via `inspect`. --- testing/code/test_excinfo.py | 40 +++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index 55e487fe3..c67201191 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -13,7 +13,7 @@ from _pytest._code.code import ExceptionChainRepr from _pytest._code.code import ExceptionInfo from _pytest._code.code import FormattedExcinfo from _pytest._io import TerminalWriter - +from _pytest.pytester import LineMatcher try: import importlib @@ -776,14 +776,43 @@ raise ValueError() ) excinfo = pytest.raises(ValueError, mod.entry) - p = FormattedExcinfo() + p = FormattedExcinfo(abspath=False) + + raised = 0 + + orig_getcwd = os.getcwd def raiseos(): - raise OSError(2) + nonlocal raised + if sys._getframe().f_back.f_code.co_name == "checked_call": + # Only raise with expected calls, but not via e.g. inspect for + # py38-windows. + raised += 1 + raise OSError(2, "custom_oserror") + return orig_getcwd() monkeypatch.setattr(os, "getcwd", raiseos) assert p._makepath(__file__) == __file__ - p.repr_traceback(excinfo) + assert raised == 1 + repr_tb = p.repr_traceback(excinfo) + + matcher = LineMatcher(str(repr_tb).splitlines()) + matcher.fnmatch_lines( + [ + "def entry():", + "> f(0)", + "", + "{}:5: ".format(mod.__file__), + "_ _ *", + "", + " def f(x):", + "> raise ValueError(x)", + "E ValueError: 0", + "", + "{}:3: ValueError".format(mod.__file__), + ] + ) + assert raised == 3 def test_repr_excinfo_addouterr(self, importasmod, tw_mock): mod = importasmod( @@ -1201,8 +1230,6 @@ raise ValueError() real traceback, such as those raised in a subprocess submitted by the multiprocessing module (#1984). """ - from _pytest.pytester import LineMatcher - exc_handling_code = " from e" if reason == "cause" else "" mod = importasmod( """ @@ -1321,7 +1348,6 @@ def test_exception_repr_extraction_error_on_recursion(): Ensure we can properly detect a recursion error even if some locals raise error on comparison (#2459). """ - from _pytest.pytester import LineMatcher class numpy_like: def __eq__(self, other):