2011-05-19 08:07:25 -03:00
|
|
|
"""Tests for the uninstall command."""
|
|
|
|
import os
|
|
|
|
import sys
|
2011-05-30 07:25:21 -03:00
|
|
|
from io import StringIO
|
2011-05-30 18:26:51 -03:00
|
|
|
import stat
|
2011-06-17 10:43:18 -03:00
|
|
|
import packaging.util
|
2011-05-19 08:07:25 -03:00
|
|
|
|
|
|
|
from packaging.database import disable_cache, enable_cache
|
|
|
|
from packaging.run import main
|
|
|
|
from packaging.errors import PackagingError
|
|
|
|
from packaging.install import remove
|
|
|
|
from packaging.command.install_dist import install_dist
|
|
|
|
|
|
|
|
from packaging.tests import unittest, support
|
|
|
|
|
|
|
|
SETUP_CFG = """
|
|
|
|
[metadata]
|
|
|
|
name = %(name)s
|
|
|
|
version = %(version)s
|
|
|
|
|
|
|
|
[files]
|
|
|
|
packages =
|
|
|
|
%(pkg)s
|
|
|
|
%(pkg)s.sub
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class UninstallTestCase(support.TempdirManager,
|
|
|
|
support.LoggingCatcher,
|
|
|
|
support.EnvironRestorer,
|
|
|
|
unittest.TestCase):
|
|
|
|
|
|
|
|
restore_environ = ['PLAT']
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(UninstallTestCase, self).setUp()
|
|
|
|
self.addCleanup(setattr, sys, 'stdout', sys.stdout)
|
|
|
|
self.addCleanup(setattr, sys, 'stderr', sys.stderr)
|
|
|
|
self.addCleanup(os.chdir, os.getcwd())
|
|
|
|
self.addCleanup(enable_cache)
|
|
|
|
self.root_dir = self.mkdtemp()
|
2011-05-23 14:07:56 -03:00
|
|
|
self.cwd = os.getcwd()
|
2011-05-19 08:07:25 -03:00
|
|
|
disable_cache()
|
|
|
|
|
2011-05-23 14:07:56 -03:00
|
|
|
def tearDown(self):
|
|
|
|
os.chdir(self.cwd)
|
2011-06-17 10:43:18 -03:00
|
|
|
packaging.util._path_created.clear()
|
2011-05-23 14:07:56 -03:00
|
|
|
super(UninstallTestCase, self).tearDown()
|
|
|
|
|
2011-05-19 08:07:25 -03:00
|
|
|
def run_setup(self, *args):
|
|
|
|
# run setup with args
|
|
|
|
args = ['run'] + list(args)
|
|
|
|
dist = main(args)
|
|
|
|
return dist
|
|
|
|
|
|
|
|
def get_path(self, dist, name):
|
|
|
|
cmd = install_dist(dist)
|
|
|
|
cmd.prefix = self.root_dir
|
|
|
|
cmd.finalize_options()
|
|
|
|
return getattr(cmd, 'install_' + name)
|
|
|
|
|
|
|
|
def make_dist(self, name='Foo', **kw):
|
|
|
|
kw['name'] = name
|
|
|
|
pkg = name.lower()
|
|
|
|
if 'version' not in kw:
|
|
|
|
kw['version'] = '0.1'
|
|
|
|
project_dir, dist = self.create_dist(**kw)
|
|
|
|
kw['pkg'] = pkg
|
|
|
|
|
|
|
|
pkg_dir = os.path.join(project_dir, pkg)
|
|
|
|
os.mkdir(pkg_dir)
|
|
|
|
os.mkdir(os.path.join(pkg_dir, 'sub'))
|
|
|
|
|
|
|
|
self.write_file((project_dir, 'setup.cfg'), SETUP_CFG % kw)
|
|
|
|
self.write_file((pkg_dir, '__init__.py'), '#')
|
|
|
|
self.write_file((pkg_dir, pkg + '_utils.py'), '#')
|
|
|
|
self.write_file((pkg_dir, 'sub', '__init__.py'), '#')
|
|
|
|
self.write_file((pkg_dir, 'sub', pkg + '_utils.py'), '#')
|
|
|
|
|
|
|
|
return project_dir
|
|
|
|
|
|
|
|
def install_dist(self, name='Foo', dirname=None, **kw):
|
|
|
|
if not dirname:
|
|
|
|
dirname = self.make_dist(name, **kw)
|
|
|
|
os.chdir(dirname)
|
2011-05-30 14:16:49 -03:00
|
|
|
old_out = sys.stderr
|
2011-05-30 07:25:21 -03:00
|
|
|
sys.stderr = StringIO()
|
2011-05-30 18:26:51 -03:00
|
|
|
dist = self.run_setup('install_dist', '--prefix=' + self.root_dir)
|
2011-10-05 23:59:41 -03:00
|
|
|
site_packages = self.get_path(dist, 'purelib')
|
|
|
|
return dist, site_packages
|
2011-05-19 08:07:25 -03:00
|
|
|
|
|
|
|
def test_uninstall_unknow_distribution(self):
|
|
|
|
self.assertRaises(PackagingError, remove, 'Foo',
|
|
|
|
paths=[self.root_dir])
|
|
|
|
|
|
|
|
def test_uninstall(self):
|
2011-10-05 23:59:41 -03:00
|
|
|
dist, site_packages = self.install_dist()
|
|
|
|
self.assertIsFile(site_packages, 'foo', '__init__.py')
|
|
|
|
self.assertIsFile(site_packages, 'foo', 'sub', '__init__.py')
|
|
|
|
self.assertIsFile(site_packages, 'Foo-0.1.dist-info', 'RECORD')
|
|
|
|
self.assertTrue(remove('Foo', paths=[site_packages]))
|
|
|
|
self.assertIsNotFile(site_packages, 'foo', 'sub', '__init__.py')
|
|
|
|
self.assertIsNotFile(site_packages, 'Foo-0.1.dist-info', 'RECORD')
|
2011-05-19 08:07:25 -03:00
|
|
|
|
2011-05-30 18:26:51 -03:00
|
|
|
def test_remove_issue(self):
|
|
|
|
# makes sure if there are OSErrors (like permission denied)
|
|
|
|
# remove() stops and display a clean error
|
2011-10-05 23:59:41 -03:00
|
|
|
dist, site_packages = self.install_dist('Meh')
|
2011-05-30 18:26:51 -03:00
|
|
|
|
|
|
|
# breaking os.rename
|
|
|
|
old = os.rename
|
|
|
|
|
|
|
|
def _rename(source, target):
|
2011-06-04 17:33:16 -03:00
|
|
|
raise OSError
|
2011-05-30 18:26:51 -03:00
|
|
|
|
|
|
|
os.rename = _rename
|
|
|
|
try:
|
2011-10-05 23:59:41 -03:00
|
|
|
self.assertFalse(remove('Meh', paths=[site_packages]))
|
2011-05-30 18:26:51 -03:00
|
|
|
finally:
|
|
|
|
os.rename = old
|
|
|
|
|
2011-10-05 23:59:41 -03:00
|
|
|
self.assertTrue(remove('Meh', paths=[site_packages]))
|
2011-05-30 18:26:51 -03:00
|
|
|
|
2011-05-19 08:07:25 -03:00
|
|
|
|
|
|
|
def test_suite():
|
|
|
|
return unittest.makeSuite(UninstallTestCase)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(defaultTest='test_suite')
|