Source

pep376 / test_pep376.py

import unittest
import pkgutil2
from pkgutil2 import *
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.abspath(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)

if __name__ == '__main__':
    unittest.main()