Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 1 | """Tests for the uninstall command.""" |
| 2 | import os |
| 3 | import sys |
Tarek Ziade | 2da7565 | 2011-05-30 12:25:21 +0200 | [diff] [blame] | 4 | from io import StringIO |
Tarek Ziade | f47fa58 | 2011-05-30 23:26:51 +0200 | [diff] [blame] | 5 | import stat |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 6 | |
| 7 | from packaging.database import disable_cache, enable_cache |
| 8 | from packaging.run import main |
| 9 | from packaging.errors import PackagingError |
| 10 | from packaging.install import remove |
| 11 | from packaging.command.install_dist import install_dist |
| 12 | |
| 13 | from packaging.tests import unittest, support |
| 14 | |
| 15 | SETUP_CFG = """ |
| 16 | [metadata] |
| 17 | name = %(name)s |
| 18 | version = %(version)s |
| 19 | |
| 20 | [files] |
| 21 | packages = |
| 22 | %(pkg)s |
| 23 | %(pkg)s.sub |
| 24 | """ |
| 25 | |
| 26 | |
| 27 | class UninstallTestCase(support.TempdirManager, |
| 28 | support.LoggingCatcher, |
| 29 | support.EnvironRestorer, |
| 30 | unittest.TestCase): |
| 31 | |
| 32 | restore_environ = ['PLAT'] |
| 33 | |
| 34 | def setUp(self): |
| 35 | super(UninstallTestCase, self).setUp() |
| 36 | self.addCleanup(setattr, sys, 'stdout', sys.stdout) |
| 37 | self.addCleanup(setattr, sys, 'stderr', sys.stderr) |
| 38 | self.addCleanup(os.chdir, os.getcwd()) |
| 39 | self.addCleanup(enable_cache) |
| 40 | self.root_dir = self.mkdtemp() |
Tarek Ziade | e1177d9 | 2011-05-23 19:07:56 +0200 | [diff] [blame] | 41 | self.cwd = os.getcwd() |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 42 | disable_cache() |
| 43 | |
Tarek Ziade | e1177d9 | 2011-05-23 19:07:56 +0200 | [diff] [blame] | 44 | def tearDown(self): |
| 45 | os.chdir(self.cwd) |
| 46 | super(UninstallTestCase, self).tearDown() |
| 47 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 48 | def run_setup(self, *args): |
| 49 | # run setup with args |
| 50 | args = ['run'] + list(args) |
| 51 | dist = main(args) |
| 52 | return dist |
| 53 | |
| 54 | def get_path(self, dist, name): |
| 55 | cmd = install_dist(dist) |
| 56 | cmd.prefix = self.root_dir |
| 57 | cmd.finalize_options() |
| 58 | return getattr(cmd, 'install_' + name) |
| 59 | |
| 60 | def make_dist(self, name='Foo', **kw): |
| 61 | kw['name'] = name |
| 62 | pkg = name.lower() |
| 63 | if 'version' not in kw: |
| 64 | kw['version'] = '0.1' |
| 65 | project_dir, dist = self.create_dist(**kw) |
| 66 | kw['pkg'] = pkg |
| 67 | |
| 68 | pkg_dir = os.path.join(project_dir, pkg) |
| 69 | os.mkdir(pkg_dir) |
| 70 | os.mkdir(os.path.join(pkg_dir, 'sub')) |
| 71 | |
| 72 | self.write_file((project_dir, 'setup.cfg'), SETUP_CFG % kw) |
| 73 | self.write_file((pkg_dir, '__init__.py'), '#') |
| 74 | self.write_file((pkg_dir, pkg + '_utils.py'), '#') |
| 75 | self.write_file((pkg_dir, 'sub', '__init__.py'), '#') |
| 76 | self.write_file((pkg_dir, 'sub', pkg + '_utils.py'), '#') |
| 77 | |
| 78 | return project_dir |
| 79 | |
| 80 | def install_dist(self, name='Foo', dirname=None, **kw): |
| 81 | if not dirname: |
| 82 | dirname = self.make_dist(name, **kw) |
| 83 | os.chdir(dirname) |
Tarek Ziade | 1cc9bbd | 2011-05-30 19:16:49 +0200 | [diff] [blame] | 84 | old_out = sys.stderr |
Tarek Ziade | 2da7565 | 2011-05-30 12:25:21 +0200 | [diff] [blame] | 85 | sys.stderr = StringIO() |
Tarek Ziade | f47fa58 | 2011-05-30 23:26:51 +0200 | [diff] [blame] | 86 | dist = self.run_setup('install_dist', '--prefix=' + self.root_dir) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 87 | install_lib = self.get_path(dist, 'purelib') |
| 88 | return dist, install_lib |
| 89 | |
| 90 | def test_uninstall_unknow_distribution(self): |
| 91 | self.assertRaises(PackagingError, remove, 'Foo', |
| 92 | paths=[self.root_dir]) |
| 93 | |
Tarek Ziade | 99d4623 | 2011-05-21 23:05:19 +0200 | [diff] [blame] | 94 | @unittest.skipIf(sys.platform == 'win32', 'deactivated for now') |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 95 | def test_uninstall(self): |
| 96 | dist, install_lib = self.install_dist() |
| 97 | self.assertIsFile(install_lib, 'foo', '__init__.py') |
| 98 | self.assertIsFile(install_lib, 'foo', 'sub', '__init__.py') |
| 99 | self.assertIsFile(install_lib, 'Foo-0.1.dist-info', 'RECORD') |
Tarek Ziade | f47fa58 | 2011-05-30 23:26:51 +0200 | [diff] [blame] | 100 | self.assertTrue(remove('Foo', paths=[install_lib])) |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 101 | self.assertIsNotFile(install_lib, 'foo', 'sub', '__init__.py') |
| 102 | self.assertIsNotFile(install_lib, 'Foo-0.1.dist-info', 'RECORD') |
| 103 | |
Tarek Ziade | f47fa58 | 2011-05-30 23:26:51 +0200 | [diff] [blame] | 104 | @unittest.skipIf(sys.platform == 'win32', 'deactivated for now') |
| 105 | def test_remove_issue(self): |
| 106 | # makes sure if there are OSErrors (like permission denied) |
| 107 | # remove() stops and display a clean error |
| 108 | dist, install_lib = self.install_dist('Meh') |
| 109 | |
| 110 | # breaking os.rename |
| 111 | old = os.rename |
| 112 | |
| 113 | def _rename(source, target): |
| 114 | raise OSError() |
| 115 | |
| 116 | os.rename = _rename |
| 117 | try: |
| 118 | self.assertFalse(remove('Meh', paths=[install_lib])) |
| 119 | finally: |
| 120 | os.rename = old |
| 121 | |
| 122 | self.assertTrue(remove('Meh', paths=[install_lib])) |
| 123 | |
Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 124 | |
| 125 | def test_suite(): |
| 126 | return unittest.makeSuite(UninstallTestCase) |
| 127 | |
| 128 | if __name__ == '__main__': |
| 129 | unittest.main(defaultTest='test_suite') |