Skip to content

Commit 09ab5fd

Browse files
authored
Merge pull request #6529 from blueyed/fix-test_repr_traceback_with_invalid_cwd
tests: fix test_repr_traceback_with_invalid_cwd
2 parents 040a61e + 79ae86c commit 09ab5fd

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

testing/code/test_excinfo.py

+33-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from _pytest._code.code import ExceptionInfo
1414
from _pytest._code.code import FormattedExcinfo
1515
from _pytest._io import TerminalWriter
16-
16+
from _pytest.pytester import LineMatcher
1717

1818
try:
1919
import importlib
@@ -776,14 +776,43 @@ def entry():
776776
)
777777
excinfo = pytest.raises(ValueError, mod.entry)
778778

779-
p = FormattedExcinfo()
779+
p = FormattedExcinfo(abspath=False)
780+
781+
raised = 0
782+
783+
orig_getcwd = os.getcwd
780784

781785
def raiseos():
782-
raise OSError(2)
786+
nonlocal raised
787+
if sys._getframe().f_back.f_code.co_name == "checked_call":
788+
# Only raise with expected calls, but not via e.g. inspect for
789+
# py38-windows.
790+
raised += 1
791+
raise OSError(2, "custom_oserror")
792+
return orig_getcwd()
783793

784794
monkeypatch.setattr(os, "getcwd", raiseos)
785795
assert p._makepath(__file__) == __file__
786-
p.repr_traceback(excinfo)
796+
assert raised == 1
797+
repr_tb = p.repr_traceback(excinfo)
798+
799+
matcher = LineMatcher(str(repr_tb).splitlines())
800+
matcher.fnmatch_lines(
801+
[
802+
"def entry():",
803+
"> f(0)",
804+
"",
805+
"{}:5: ".format(mod.__file__),
806+
"_ _ *",
807+
"",
808+
" def f(x):",
809+
"> raise ValueError(x)",
810+
"E ValueError: 0",
811+
"",
812+
"{}:3: ValueError".format(mod.__file__),
813+
]
814+
)
815+
assert raised == 3
787816

788817
def test_repr_excinfo_addouterr(self, importasmod, tw_mock):
789818
mod = importasmod(
@@ -1201,8 +1230,6 @@ def test_exc_chain_repr_without_traceback(self, importasmod, reason, description
12011230
real traceback, such as those raised in a subprocess submitted by the multiprocessing
12021231
module (#1984).
12031232
"""
1204-
from _pytest.pytester import LineMatcher
1205-
12061233
exc_handling_code = " from e" if reason == "cause" else ""
12071234
mod = importasmod(
12081235
"""
@@ -1321,7 +1348,6 @@ def test_exception_repr_extraction_error_on_recursion():
13211348
Ensure we can properly detect a recursion error even
13221349
if some locals raise error on comparison (#2459).
13231350
"""
1324-
from _pytest.pytester import LineMatcher
13251351

13261352
class numpy_like:
13271353
def __eq__(self, other):

0 commit comments

Comments
 (0)