summaryrefslogtreecommitdiff
path: root/testlib.py
diff options
context:
space:
mode:
authorPierre-Yves David <pierre-yves.david@logilab.fr>2008-06-27 16:22:10 +0200
committerPierre-Yves David <pierre-yves.david@logilab.fr>2008-06-27 16:22:10 +0200
commit8bc19ba9563bb934f57767e27a6f243aa27fdf20 (patch)
tree6e10f195df6d0be0a9d9e6851b3cbf214c1f662c /testlib.py
parent9c2bba4cc83f9ca438d409370b4bc4b5083f363c (diff)
downloadlogilab-common-8bc19ba9563bb934f57767e27a6f243aa27fdf20.tar.gz
add a with_tempdir decorator ensuring all temporary files and dirs are removed
Diffstat (limited to 'testlib.py')
-rw-r--r--testlib.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/testlib.py b/testlib.py
index d95bdd1..b91a297 100644
--- a/testlib.py
+++ b/testlib.py
@@ -33,11 +33,14 @@ import traceback
import unittest
import difflib
import types
+import tempfile
+from shutil import rmtree
from operator import itemgetter
from warnings import warn
from compiler.consts import CO_GENERATOR
from ConfigParser import ConfigParser
+
# PRINT_ = file('stdout.txt', 'w').write
try:
@@ -65,6 +68,24 @@ DEFAULT_PREFIXES = ('test', 'regrtest', 'smoketest', 'unittest',
ENABLE_DBC = False
+
+def with_tempdir(callable):
+ """A decorator ensuring no temporary file left when the function return
+ Work only for temporary file create with the tempfile module"""
+ def proxy(*args, **kargs):
+
+ old_tmpdir = tempfile.gettempdir()
+ new_tmpdir = tempfile.mkdtemp("-logilab-common-testlib","temp-dir-")
+ tempfile.tempdir = new_tmpdir
+ try:
+ return callable(*args, **kargs)
+ finally:
+ try:
+ rmtree(new_tmpdir, ignore_errors=True)
+ finally:
+ tempfile.tempdir = old_tmpdir
+ return proxy
+
def main(testdir=None, exitafter=True):
"""Execute a test suite.