pytest: Make directory cleanup robust against setup failures

We were triggering a second exception in the directory cleanup step by
attempting to access a field that'd only be set upon entering the test code
itself. That error did not contribute to the problem resolution, so now we
check whether that field is set before accessing it.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker 2019-04-10 21:47:32 +02:00 committed by neil saitug
parent 20a2e3ca6d
commit 88f425fcc5

View File

@ -54,8 +54,13 @@ def directory(request, test_base_dir, test_name):
yield directory
# This uses the status set in conftest.pytest_runtest_makereport to
# determine whether we succeeded or failed.
if not request.node.has_errors and request.node.rep_call.outcome == 'passed':
# determine whether we succeeded or failed. Outcome can be None if the
# failure occurs during the setup phase, hence the use to getattr instead
# of accessing it directly.
outcome = getattr(request.node, 'rep_call', None)
failed = not outcome or request.node.has_errors or outcome != 'passed'
if not failed:
shutil.rmtree(directory)
else:
logging.debug("Test execution failed, leaving the test directory {} intact.".format(directory))