[Python-checkins] r85708 - python/branches/pep-382/Lib/test/test_pep382.py

barry.warsaw python-checkins at python.org
Tue Oct 19 01:57:09 CEST 2010


Author: barry.warsaw
Date: Tue Oct 19 01:57:09 2010
New Revision: 85708

Log:
* Refactor the tests in preparation for the zipimport version.
* Use absolute import to get test.support so that you can still do "./python
  Lib/test/test_pep382.py" -- relative imports in stdlib tests are evil!
* XXX for tearDown() as that should go away with Eric's refactoring.



Modified:
   python/branches/pep-382/Lib/test/test_pep382.py

Modified: python/branches/pep-382/Lib/test/test_pep382.py
==============================================================================
--- python/branches/pep-382/Lib/test/test_pep382.py	(original)
+++ python/branches/pep-382/Lib/test/test_pep382.py	Tue Oct 19 01:57:09 2010
@@ -1,15 +1,16 @@
 import unittest, sys, os
-from . import support
+from test import support
 
 d1="d1"
 d2="d2"
 d3="d3"
 d4="d4"
 
-class PthTests(unittest.TestCase):
+
+class PthTestsBase(unittest.TestCase):
     """Try various combinations of __init__ and .pth files.
     At the end, remove everything from sys.path and sys.modules.
-    The test package is always pep382test.
+    The namespace package package begin tested is always pep382test.
     Directory structure:
     - d1: wildcard pth file, plus d1.py
     - d2: wildcard pth file,
@@ -19,23 +20,14 @@
     - d4: __init__, plus wildcard pth, plus d4.py
     """
 
-    def tearDown(self):
-        # delete directories from sys.path
-        i = 0
-        while i < len(sys.path):
-            if "pep382" in sys.path[i]:
-                del sys.path[i]
-            else:
-                i += 1
-        # delete all pep382test modules
-        for key in list(sys.modules):
-            if "pep382test" in key:
-                del sys.modules[key]
-
-    def add(self, *dirs):
-        base = os.path.dirname(__file__)
-        for dir in dirs:
-            sys.path.append(os.path.join(base, "pep382", dir))
+    def add_to_syspath(self, *items):
+        # Must be implemented by subclasses to add the proper items
+        # (e.g. directories or zipfiles) to sys.path.  This method does not
+        # need to keep track of the items added since a context manager will
+        # be used to restore sys.path after the test.  items will be a
+        # sequence of subpath names (e.g. d1, d2, d3, d4) which the subclasses
+        # must map into the actual thing to add.
+        raise NotImplementedError
 
     def test_d1_d2_d3_d4(self):
         'All directories should show up in the __path__'
@@ -87,9 +79,34 @@
         self.assertTrue(pep382test.d3.imported)
         self.assertEquals(pep382test.D3, "d3")
 
+
+class PthFilesystemTests(PthTestsBase):
+    """Test namespace packages that live solely on the filesystem."""
+
+    def add(self, *dirs):
+        base = os.path.dirname(__file__)
+        for dir in dirs:
+            sys.path.append(os.path.join(base, "pep382", dir))
+
+    # XXX for now.
+    def tearDown(self):
+        # delete directories from sys.path
+        i = 0
+        while i < len(sys.path):
+            if "pep382" in sys.path[i]:
+                del sys.path[i]
+            else:
+                i += 1
+        # delete all pep382test modules
+        for key in list(sys.modules):
+            if "pep382test" in key:
+                del sys.modules[key]
+
+
 def test_main():
-    tests = [PthTests]
+    tests = [PthFilesystemTests]
     support.run_unittest(*tests)
 
+
 if __name__ == "__main__":
     test_main()


More information about the Python-checkins mailing list