[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164 stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 10 Mar 2014 16:07:41 +0100
branchstable
changeset 9564 e2d5b0712974
parent 9560 97fed7743dbe
child 9565 fa00fc251d57
[test] when the config change, we should cleanup sys.path and sys.modules. Closes #3634164 Without this, modules imported within instance home will be messed up.
devtools/__init__.py
--- a/devtools/__init__.py	Wed Mar 05 15:49:23 2014 +0100
+++ b/devtools/__init__.py	Mon Mar 10 16:07:41 2014 +0100
@@ -108,6 +108,7 @@
         repo._needs_refresh = True
         repo._has_started = False
 
+
 def turn_repo_on(repo):
     """Idea: this is less costly than a full re-creation of the repo object.
     on:
@@ -820,7 +821,22 @@
 
 
 # XXX a class method on Test ?
+
+_CONFIG = None
 def get_test_db_handler(config):
+    global _CONFIG
+    if _CONFIG is not None and config is not _CONFIG:
+        from logilab.common.modutils import cleanup_sys_modules
+        # cleanup all dynamically loaded modules and everything in the instance
+        # directory
+        apphome = _CONFIG.apphome
+        if apphome: # may be unset in tests
+            cleanup_sys_modules([apphome])
+        # also cleanup sys.path
+        if apphome in sys.path:
+            sys.path.remove(apphome)
+    _CONFIG = config
+    config.adjust_sys_path()
     handler = HCACHE.get(config)
     if handler is not None:
         return handler