From 88f425fcc517452337d8b2e0539fbd8f5fa3ebf4 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 10 Apr 2019 21:47:32 +0200 Subject: [PATCH] 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 --- tests/fixtures.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/fixtures.py b/tests/fixtures.py index 7c8b7ce91..928ac65bb 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -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))