Source

pep376 / test_pkgutil.py

Full commit
from nose.tools import *
from pkgutil import *
import sys
import os

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

def setup():
    sys.old = sys.path
    sys.path = [SITE_PKG]

def teardown():
    sys.path = sys.old
    del sys.old

def test_get_egg_infos():
    projects = list(get_egg_infos([SITE_PKG]))
    assert_equals(len(projects), 2)

def test_egg_info_directory():
    assert_equals(get_egg_info('xxx', [SITE_PKG]), None)

    project = get_egg_info('mercurial', [SITE_PKG])
    assert_equals(project.name, 'mercurial')

    project = get_egg_info('processing', [SITE_PKG])
    assert_equals(project.name, 'processing')

def test_egg_info():

    egg_info = get_egg_info('mercurial', [SITE_PKG])
    assert_equals(str(egg_info), "EggInfo('mercurial')")

    files = list(egg_info.get_installed_files())
    assert_equals(len(files), 4)
    assert_equals(files[0], ('mercurial/filelog.py', '98676876876876', '12'))

    files = list(egg_info.get_installed_files(local=True))
    location = os.path.join(egg_info.info_path, 'mercurial', 'filelog.py')
    assert_equals(files[0], (location, '98676876876876', '12'))

    f = egg_info.get_file('RECORD')
    record = os.path.join(SITE_PKG, 'mercurial-1.0.1.egg-info', 'RECORD')
    assert_equals(open(record).read(), f.read())

    assert egg_info.uses('mercurial/filelog.py')
    assert not egg_info.uses('mercurial/filelog.sasasasa')


def test_directory():
    dir = EggInfoDirectory(SITE_PKG)

    egg_info = dir.owner('mercurial/filelog.pyc')
    assert egg_info.name == 'mercurial'
    assert dir.owner('mercurial/filelog.py') is None

def test_get_file_users():

    users = list(get_file_users('mercurial/filelog.py', [SITE_PKG]))
    assert_equals(len(users), 2)