Skip to content

Commit 577cce2

Browse files
Merge pull request #1593 from marscher/fix_cwd_explosion
Fix cwd explosion
2 parents 70fdab4 + 09d163a commit 577cce2

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Mark Abramowitz
6969
Markus Unterwaditzer
7070
Martijn Faassen
7171
Martin Prusse
72+
Martin K. Scherer
7273
Matt Bachmann
7374
Michael Aquilina
7475
Michael Birtwell

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
*
1212

13+
* Fix exception visualization in case the current working directory (CWD) gets
14+
deleted during testing. Fixes (`#1235`). Thanks `@bukzor` for reporting. PR by
15+
`@marscher`. Thanks `@nicoddemus` for his help.
16+
1317
.. _#1580: https://github.com/pytest-dev/pytest/issues/1580
1418

1519
.. _@graingert: https://github.com/graingert

_pytest/_code/code.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from inspect import CO_VARARGS, CO_VARKEYWORDS
33

44
import py
5-
65
builtin_repr = repr
76

87
reprlib = py.builtin._tryimport('repr', 'reprlib')
@@ -35,12 +34,16 @@ def __ne__(self, other):
3534
def path(self):
3635
""" return a path object pointing to source code (note that it
3736
might not point to an actually existing file). """
38-
p = py.path.local(self.raw.co_filename)
39-
# maybe don't try this checking
40-
if not p.check():
37+
try:
38+
p = py.path.local(self.raw.co_filename)
39+
# maybe don't try this checking
40+
if not p.check():
41+
raise OSError("py.path check failed.")
42+
except OSError:
4143
# XXX maybe try harder like the weird logic
4244
# in the standard lib [linecache.updatecache] does?
4345
p = self.raw.co_filename
46+
4447
return p
4548

4649
@property

_pytest/main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,13 @@ def _repr_failure_py(self, excinfo, style=None):
403403
else:
404404
style = "long"
405405

406-
return excinfo.getrepr(funcargs=True,
406+
try:
407+
os.getcwd()
408+
abspath = False
409+
except OSError:
410+
abspath = True
411+
412+
return excinfo.getrepr(funcargs=True, abspath=abspath,
407413
showlocals=self.config.option.showlocals,
408414
style=style, tbfilter=tbfilter)
409415

testing/code/test_excinfo.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,3 +925,14 @@ def test_repr_traceback_with_unicode(style, encoding):
925925
repr_traceback = formatter.repr_traceback(e_info)
926926
assert repr_traceback is not None
927927

928+
929+
def test_cwd_deleted(testdir):
930+
testdir.makepyfile("""
931+
def test(tmpdir):
932+
tmpdir.chdir()
933+
tmpdir.remove()
934+
assert False
935+
""")
936+
result = testdir.runpytest()
937+
result.stdout.fnmatch_lines(['* 1 failed in *'])
938+
assert 'INTERNALERROR' not in result.stdout.str() + result.stderr.str()

0 commit comments

Comments
 (0)