From 09d163aa3a1f01701fe2e3f073a89d49b9d0cce2 Mon Sep 17 00:00:00 2001 From: marscher Date: Tue, 7 Jun 2016 15:43:05 +0200 Subject: [PATCH] [exception handling] Fix case the current working directory (CWD) gets deleted during testing. Fixes #1235. --- AUTHORS | 1 + CHANGELOG.rst | 4 ++++ _pytest/_code/code.py | 11 +++++++---- _pytest/main.py | 8 +++++++- testing/code/test_excinfo.py | 11 +++++++++++ 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/AUTHORS b/AUTHORS index b41e4c43d..d740fee5c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -69,6 +69,7 @@ Mark Abramowitz Markus Unterwaditzer Martijn Faassen Martin Prusse +Martin K. Scherer Matt Bachmann Michael Aquilina Michael Birtwell diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4783395a8..adc6993eb 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 diff --git a/_pytest/_code/code.py b/_pytest/_code/code.py index 8995cc1f7..c6f811c85 100644 --- a/_pytest/_code/code.py +++ b/_pytest/_code/code.py @@ -2,7 +2,6 @@ import sys from inspect import CO_VARARGS, CO_VARKEYWORDS import py - builtin_repr = repr reprlib = py.builtin._tryimport('repr', 'reprlib') @@ -35,12 +34,16 @@ class Code(object): 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 diff --git a/_pytest/main.py b/_pytest/main.py index 8654d7af6..f608a7ecd 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -403,7 +403,13 @@ class Node(object): 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) diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index 47ad50b06..ba047f2a9 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -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()