pep376 / test_pep376.py

import unittest
import pkgutil2
from pkgutil2 import *
import cStringIO
import sys
import os

SITE_PACKAGES = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'site-packages')

class Helpers:
    def assertIn(self, container, elem):
        self.assertTrue(container.__contains__(elem),
                '%r not in %r' % (elem, container))
    def abspath(self, rel):
        parts = rel.split('/')
        return os.path.join(self.container, *parts)

class Distributions:
    def test_list_distributions(self):
        projects = list(get_distributions())
        self.assertEqual(len(projects), 2)
        names = [p.name for p in projects]
        self.assertIn(names, 'mercurial')
        self.assertIn(names, 'processing')
    def test_get_distribution(self):
        self.assertEqual(get_distribution('unknown'), None)
        self.assertEqual(get_distribution('mercurial').name, 'mercurial')
        self.assertEqual(get_distribution('processing').name, 'processing')
    def test_get_file_users(self):
        path = self.abspath('processing/__init__.py')
        users = list(get_file_users(path))
        self.assertEqual(len(users), 1)
        self.assertEqual(users[0].name, 'processing')
        # Note: mercurial/filelog.py is in both RECORD files!
        path = self.abspath('mercurial/filelog.py')
        users = list(get_file_users(path))
        self.assertEqual(len(users), 2)
        self.assertEqual(sorted(u.name for u in users),
                ['mercurial', 'processing'])
    def test_distribution_metadata_10(self):
        mercurial = get_distribution('mercurial')
        self.assertEqual(mercurial.name, 'mercurial')
        self.assertEqual(mercurial.metadata.version, '1.0.1')
        self.assertEqual(mercurial.metadata.license, 'GNU GPL')
    def test_distribution_metadata_11(self):
        processing = get_distribution('processing')
        self.assertEqual(processing.name, 'processing')
        self.assertEqual(processing.metadata.version, '0.52')
        self.assertEqual(processing.metadata.author, 'R Oudkerk')
        self.assertEqual(len(processing.metadata.provides), 2)
        self.assertEqual(len(processing.metadata.requires), 2)
        self.assertEqual(len(processing.metadata.obsoletes), 3)
    def test_distribution_metadata_files(self):
        mercurial = get_distribution('mercurial')
        # Note - deliberate discrepancy - the file is called PKG-INFO, but the
        # RECORD file holds the name PKG_INFO - we should see what is in the
        # RECORD file, not the actual file name!!!
        self.assertEqual(sorted(mercurial.get_metadata_files()),
                ['PKG_INFO', 'RECORD'])

class TestFilesystem(Distributions, Helpers, unittest.TestCase):
    container = SITE_PACKAGES
    def setUp(self):
        self.path = sys.path
        sys.path = [ self.container ]
    def tearDown(self):
        sys.path = self.path

class TestZipfile(Distributions, Helpers, unittest.TestCase):
    container = SITE_PACKAGES + '.zip'
    def setUp(self):
        self.path = sys.path
        self.path_hooks = sys.path_hooks
        self.path_importer_cache = sys.path_importer_cache
        sys.path = [ self.container ]
        sys.path_hooks = [ pkgutil2.ZipFinder ]
        sys.path_importer_cache = {}
    def tearDown(self):
        sys.path = self.path
        sys.path_hooks = self.path_hooks
        sys.path_importer_cache = self.path_importer_cache

class TestMetadata(unittest.TestCase):
    def test_metadata_dirname(self):
        self.assertEqual(metadata_dirname('zlib', '2.5.2'), 'zlib-2.5.2.egg-info')
        self.assertEqual(metadata_dirname('python-ldap', '2.5'), 'python_ldap-2.5.egg-info')
        self.assertEqual(metadata_dirname('python-ldap', '2.5 a---5'), 'python_ldap-2.5.a_5.egg-info')

    def test_null_metadata(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()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.