Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: fix test_repr_traceback_with_invalid_cwd #6529

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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
Expand Down Expand Up @@ -776,14 +776,43 @@ def entry():
)
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(
Expand Down Expand Up @@ -1201,8 +1230,6 @@ def test_exc_chain_repr_without_traceback(self, importasmod, reason, description
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(
"""
Expand Down Expand Up @@ -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):
Expand Down