Skip to content

Commit

Permalink
Merge pull request #1593 from marscher/fix_cwd_explosion
Browse files Browse the repository at this point in the history
Fix cwd explosion
  • Loading branch information
RonnyPfannschmidt committed Jun 10, 2016
2 parents 70fdab4 + 09d163a commit 577cce2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Mark Abramowitz
Markus Unterwaditzer
Martijn Faassen
Martin Prusse
Martin K. Scherer
Matt Bachmann
Michael Aquilina
Michael Birtwell
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

*

* Fix exception visualization in case the current working directory (CWD) gets
deleted during testing. Fixes (`#1235`). Thanks `@bukzor` for reporting. PR by
`@marscher`. Thanks `@nicoddemus` for his help.

.. _#1580: https://github.com/pytest-dev/pytest/issues/1580

.. _@graingert: https://github.com/graingert
Expand Down
11 changes: 7 additions & 4 deletions _pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from inspect import CO_VARARGS, CO_VARKEYWORDS

import py

builtin_repr = repr

reprlib = py.builtin._tryimport('repr', 'reprlib')
Expand Down Expand Up @@ -35,12 +34,16 @@ def __ne__(self, other):
def path(self):
""" return a path object pointing to source code (note that it
might not point to an actually existing file). """
p = py.path.local(self.raw.co_filename)
# maybe don't try this checking
if not p.check():
try:
p = py.path.local(self.raw.co_filename)
# maybe don't try this checking
if not p.check():
raise OSError("py.path check failed.")
except OSError:
# XXX maybe try harder like the weird logic
# in the standard lib [linecache.updatecache] does?
p = self.raw.co_filename

return p

@property
Expand Down
8 changes: 7 additions & 1 deletion _pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,13 @@ def _repr_failure_py(self, excinfo, style=None):
else:
style = "long"

return excinfo.getrepr(funcargs=True,
try:
os.getcwd()
abspath = False
except OSError:
abspath = True

return excinfo.getrepr(funcargs=True, abspath=abspath,
showlocals=self.config.option.showlocals,
style=style, tbfilter=tbfilter)

Expand Down
11 changes: 11 additions & 0 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,3 +925,14 @@ def test_repr_traceback_with_unicode(style, encoding):
repr_traceback = formatter.repr_traceback(e_info)
assert repr_traceback is not None


def test_cwd_deleted(testdir):
testdir.makepyfile("""
def test(tmpdir):
tmpdir.chdir()
tmpdir.remove()
assert False
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines(['* 1 failed in *'])
assert 'INTERNALERROR' not in result.stdout.str() + result.stderr.str()

0 comments on commit 577cce2

Please sign in to comment.