Commits

Paul Moore committed e2dec42

Added a simple PEP 302 importer test case

  • Participants
  • Parent commits 051660a

Comments (0)

Files changed (1)

 import unittest
 import pkgutil2
 from pkgutil2 import *
+import cStringIO
 import sys
 import os
 
                 '%r not in %r' % (elem, container))
     def abspath(self, rel):
         parts = rel.split('/')
-        return os.path.abspath(os.path.join(self.container, *parts))
+        return os.path.join(self.container, *parts)
 
 class Distributions:
     def test_list_distributions(self):
         m = pkgutil2._DistributionMetadata()
         self.assertEqual(m.name, None)
 
+class DummyImporter(object):
+    def get_file(self, dir, name):
+        fn = os.path.join(SITE_PACKAGES, dir + '.egg-info', name)
+        return open(fn, "rb").read()
+    def __init__(self):
+        self.distributions = dict(mercurial='1.0.1', processing='0.52')
+        self.packages = [ 'mercurial', 'processing' ]
+        self.modules = [ 'mercurial.filelog' ]
+        self.files = [
+                'mercurial/filelog.py',
+                'mercurial/filelog.pyc',
+                'processing/__init__.py'
+            ]
+        self.distfiles = {
+            'mercurial' : {
+                'PKG-INFO' : self.get_file('mercurial-1.0.1','PKG-INFO'),
+                'RECORD' : self.get_file('mercurial-1.0.1','RECORD'),
+            },
+            'processing' : {
+                'PKG-INFO' : self.get_file('processing-0.52','PKG-INFO'),
+                'RECORD' : self.get_file('processing-0.52','RECORD'),
+            },
+        }
+
+    def _get_code(self, fullname):
+        if fullname in self.modules:
+            CODE = "def foo():\n    return 'Hello, world!'"
+        else:
+            CODE = ''
+        return fullname in self.packages, CODE
+
+    def find_module(self, fullname, path=None):
+        if fullname in self.modules or fullname in self.packages:
+            return self
+        return None
+
+    def load_module(self, fullname):
+        ispkg, code = self._get_code(fullname)
+        mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
+        mod.__file__ = "<dummy>/%s" % (fullname,)
+        mod.__loader__ = self
+        if ispkg:
+            mod.__path__ = []
+        exec code in mod.__dict__
+        return mod
+
+    # PEP 376 support methods
+    class Metadata(pkgutil2.Distribution):
+        def __init__(self, distname, distfiles):
+            pkgutil2.Distribution.__init__(self)
+            self.distname = distname
+            self.distfiles = distfiles
+        def _local_path(self, path):
+            path = path.split('/')
+            return os.path.join('<dummy>', *path)
+        def get_metadata_file(self, pathname):
+            return cStringIO.StringIO(self.distfiles[pathname])
+
+    def list_distributions(self):
+        for dist in self.distributions:
+            yield dist
+    def get_metadata(self, dist):
+        distname = pkgutil2.metadata_dirname(dist, self.distributions[dist])
+        return self.Metadata(distname, self.distfiles[dist])
+
+class TestPEP302(Distributions, Helpers, unittest.TestCase):
+    container = '<dummy>'
+    def setUp(self):
+        self.meta_path = sys.meta_path
+        self.path = sys.path
+        self.path_hooks = sys.path_hooks
+        self.path_importer_cache = sys.path_importer_cache
+        sys.meta_path = [ DummyImporter() ]
+        sys.path = []
+        sys.path_hooks = []
+        sys.path_importer_cache = {}
+    def tearDown(self):
+        sys.meta_path = self.meta_path
+        sys.path = self.path
+        sys.path_hooks = self.path_hooks
+        sys.path_importer_cache = self.path_importer_cache
+
 if __name__ == '__main__':
     unittest.main()