From 68698f4b04876c299a3f48bfb44074c252997bfd Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Wed, 13 Nov 2024 10:53:18 -0500 Subject: [PATCH] Tests: Add pre-delete callbacks to TemporaryDirectory destructor MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This will allow certain test clean-up operations (e.g., `umount`) to be run before trying to to remove the directory. Change-Id: I193030dfe4aeb0eeebc4370849681cbd9dcf500e Signed-off-by: Kienan Stewart Signed-off-by: Jérémie Galarneau --- tests/utils/lttngtest/environment.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/utils/lttngtest/environment.py b/tests/utils/lttngtest/environment.py index 03c1b5c81..1cc87a1fe 100644 --- a/tests/utils/lttngtest/environment.py +++ b/tests/utils/lttngtest/environment.py @@ -32,11 +32,19 @@ import bt2 class TemporaryDirectory: def __init__(self, prefix): # type: (str) -> None + self._cleanup_callbacks = [] self._directory_path = tempfile.mkdtemp(prefix=prefix) def __del__(self): - if os.getenv("LTTNG_TEST_PRESERVE_TEST_ENV", "0") != "1": - shutil.rmtree(self._directory_path, ignore_errors=True) + try: + for func, args, kwargs in self._cleanup_callbacks: + func(*args, **kwargs) + finally: + if os.getenv("LTTNG_TEST_PRESERVE_TEST_ENV", "0") != "1": + shutil.rmtree(self._directory_path, ignore_errors=True) + + def add_cleanup_callback(self, func, *args, **kwargs): + self._cleanup_callbacks.append((func, args, kwargs)) @property def path(self): -- 2.39.5