diff tests/common.py @ 67:730c42743ba3

tests: cleanup on test finish so Windows doesn't complain about used files - chdir out of the test dir before rmtree - cache instances of hgclient and close them explicitly on tearDown before rmtree
author Idan Kamara <idankk86@gmail.com>
date Mon, 05 Sep 2011 20:51:29 +0300
parents 3d413c54e048
children a0328b08e028
line wrap: on
line diff
--- a/tests/common.py	Tue Aug 23 21:57:40 2011 +0300
+++ b/tests/common.py	Mon Sep 05 20:51:29 2011 +0300
@@ -3,11 +3,24 @@
 
 import hglib
 
+def resultappender(list):
+    def decorator(f):
+        def decorated(*args, **kwargs):
+            result = f(*args, **kwargs)
+            list.append(result)
+            return result
+        return decorated
+    return decorator
+
 class basetest(unittest.TestCase):
     def setUp(self):
         self._testtmp = os.environ["TESTTMP"] = os.environ["HOME"] = \
             os.path.join(os.environ["HGTMP"], self.__class__.__name__)
 
+        self.clients = []
+        self._oldopen = hglib.open
+        hglib.open = resultappender(self.clients)(hglib.open)
+
         os.mkdir(self._testtmp)
         os.chdir(self._testtmp)
         # until we can run norepo commands in the cmdserver
@@ -15,6 +28,13 @@
         self.client = hglib.open()
 
     def tearDown(self):
+        # on Windows we cannot rmtree before closing all instances because of used
+        # files
+        hglib.open = self._oldopen
+        for client in self.clients:
+            if client.server is not None:
+                client.close()
+        os.chdir('..')
         try:
             shutil.rmtree(self._testtmp)
         except AttributeError: