[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[PATCH] Improve module cleanup in our TestCase class and fix issues in FS mock class.



---
 tests/mock/__init__.py |   20 +++++++++++++++++---
 tests/mock/disk.py     |   11 +++++++++--
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/tests/mock/__init__.py b/tests/mock/__init__.py
index 375044c..b19f488 100644
--- a/tests/mock/__init__.py
+++ b/tests/mock/__init__.py
@@ -34,13 +34,27 @@ class TestCase(unittest.TestCase):
            properly unloaded during tearDown"""
 
         import sys
+        self.preexistingModules = set(sys.modules.keys())
+
         for m in a:
             sys.modules[m] = Mock()
             self.injectedModules[m] = sys.modules[m]
 
-    def tearDownModules(self):
+    def modifiedModule(self, mname, mod = None):
+        """Mark module (and all it's parents) as tainted"""
+        
+        oldname=""
+        for m in mname.split("."):
+            self.injectedModules[oldname+m] = mod
+            oldname += m + "."
+        self.injectedModules[mname] = mod    
+
+    def tearDown(self):
         """Unload previously Mocked modules"""
 
         import sys
-        for k in self.injectedModules.keys():
-            del sys.modules[k]
+        for m in sys.modules.keys():
+            if m in self.preexistingModules and not m in self.injectedModules:
+                continue
+            
+            del sys.modules[m]
diff --git a/tests/mock/disk.py b/tests/mock/disk.py
index b87a3a2..39af5e8 100644
--- a/tests/mock/disk.py
+++ b/tests/mock/disk.py
@@ -17,6 +17,7 @@
 # Author(s): Martin Sivak <msivak redhat com>
 
 from StringIO import StringIO
+import os.path
 
 class DiskIO(object):
     """Simple object to simplify mocking of file operations in Mock
@@ -45,6 +46,12 @@ class DiskIO(object):
     def __init__(self):
         self.reset()
 
+    def __getitem__(self, key):
+        return self.fs[key]
+
+    def __setitem__(self, key, value):
+        self.fs[key] = value
+
     def reset(self):
         self.fs = {
             "/proc": self.Dir,
@@ -60,11 +67,11 @@ class DiskIO(object):
             raise IOError("[Errno 21] Is a directory: '%s'" % (path))
         elif not content and mode.startswith("w"):
             self.fs[path] = ""
-            f = self.IO(self.fs, path, self.fs[path])
+            f = self.TestFile(self.fs, path, self.fs[path])
         elif not content:
             raise IOError("[Errno 2] No such file or directory: '%s'" % (path,))
         elif mode.endswith("+") or mode.endswith("a"):
-            f = self.IO(self.fs, path, content)
+            f = self.TestFile(self.fs, path, content)
             f.seek(0, os.SEEK_END)
         else:
             f = self.IO(self.fs, path, content)
-- 
1.6.6.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]