Skip to content

Commit

Permalink
Merge pull request #6529 from blueyed/fix-test_repr_traceback_with_in…
Browse files Browse the repository at this point in the history
…valid_cwd

tests: fix test_repr_traceback_with_invalid_cwd
  • Loading branch information
blueyed authored Jan 24, 2020
2 parents 040a61e + 79ae86c commit 09ab5fd
Showing 1 changed file with 33 additions and 7 deletions.
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

0 comments on commit 09ab5fd

Please sign in to comment.