blob: 49fcb42897fb923c1e9a40864e04e57b7fabeb3e [file] [log] [blame]
Éric Araujo8ed34a12011-10-06 05:18:41 +02001"""Tests for the packaging.uninstall module."""
Tarek Ziade1231a4e2011-05-19 13:07:25 +02002import os
3import sys
Éric Araujofa23cc82011-10-06 05:15:09 +02004import logging
Éric Araujoc06f46f2011-06-17 15:43:18 +02005import packaging.util
Tarek Ziade1231a4e2011-05-19 13:07:25 +02006
Éric Araujo8ed34a12011-10-06 05:18:41 +02007from io import StringIO
Tarek Ziade1231a4e2011-05-19 13:07:25 +02008from packaging.run import main
9from packaging.errors import PackagingError
10from packaging.install import remove
Éric Araujo8ed34a12011-10-06 05:18:41 +020011from packaging.database import disable_cache, enable_cache
Tarek Ziade1231a4e2011-05-19 13:07:25 +020012from packaging.command.install_dist import install_dist
13
14from packaging.tests import unittest, support
15
16SETUP_CFG = """
17[metadata]
18name = %(name)s
19version = %(version)s
20
21[files]
22packages =
23 %(pkg)s
24 %(pkg)s.sub
25"""
26
27
28class UninstallTestCase(support.TempdirManager,
29 support.LoggingCatcher,
30 support.EnvironRestorer,
31 unittest.TestCase):
32
33 restore_environ = ['PLAT']
34
35 def setUp(self):
36 super(UninstallTestCase, self).setUp()
37 self.addCleanup(setattr, sys, 'stdout', sys.stdout)
38 self.addCleanup(setattr, sys, 'stderr', sys.stderr)
39 self.addCleanup(os.chdir, os.getcwd())
40 self.addCleanup(enable_cache)
41 self.root_dir = self.mkdtemp()
Tarek Ziadee1177d92011-05-23 19:07:56 +020042 self.cwd = os.getcwd()
Tarek Ziade1231a4e2011-05-19 13:07:25 +020043 disable_cache()
44
Tarek Ziadee1177d92011-05-23 19:07:56 +020045 def tearDown(self):
46 os.chdir(self.cwd)
Éric Araujoc06f46f2011-06-17 15:43:18 +020047 packaging.util._path_created.clear()
Tarek Ziadee1177d92011-05-23 19:07:56 +020048 super(UninstallTestCase, self).tearDown()
49
Tarek Ziade1231a4e2011-05-19 13:07:25 +020050 def run_setup(self, *args):
51 # run setup with args
52 args = ['run'] + list(args)
53 dist = main(args)
54 return dist
55
56 def get_path(self, dist, name):
57 cmd = install_dist(dist)
58 cmd.prefix = self.root_dir
59 cmd.finalize_options()
60 return getattr(cmd, 'install_' + name)
61
62 def make_dist(self, name='Foo', **kw):
63 kw['name'] = name
64 pkg = name.lower()
65 if 'version' not in kw:
66 kw['version'] = '0.1'
67 project_dir, dist = self.create_dist(**kw)
68 kw['pkg'] = pkg
69
70 pkg_dir = os.path.join(project_dir, pkg)
71 os.mkdir(pkg_dir)
72 os.mkdir(os.path.join(pkg_dir, 'sub'))
73
74 self.write_file((project_dir, 'setup.cfg'), SETUP_CFG % kw)
75 self.write_file((pkg_dir, '__init__.py'), '#')
76 self.write_file((pkg_dir, pkg + '_utils.py'), '#')
77 self.write_file((pkg_dir, 'sub', '__init__.py'), '#')
78 self.write_file((pkg_dir, 'sub', pkg + '_utils.py'), '#')
79
80 return project_dir
81
82 def install_dist(self, name='Foo', dirname=None, **kw):
83 if not dirname:
84 dirname = self.make_dist(name, **kw)
85 os.chdir(dirname)
Tarek Ziade2da75652011-05-30 12:25:21 +020086 sys.stderr = StringIO()
Tarek Ziadef47fa582011-05-30 23:26:51 +020087 dist = self.run_setup('install_dist', '--prefix=' + self.root_dir)
Éric Araujo2c30e392011-10-06 04:59:41 +020088 site_packages = self.get_path(dist, 'purelib')
89 return dist, site_packages
Tarek Ziade1231a4e2011-05-19 13:07:25 +020090
Éric Araujo9556a5b2011-10-06 05:10:09 +020091 def test_uninstall_unknown_distribution(self):
92 dist, site_packages = self.install_dist('Foospam')
Tarek Ziade1231a4e2011-05-19 13:07:25 +020093 self.assertRaises(PackagingError, remove, 'Foo',
Éric Araujo9556a5b2011-10-06 05:10:09 +020094 paths=[site_packages])
Tarek Ziade1231a4e2011-05-19 13:07:25 +020095
Tarek Ziade1231a4e2011-05-19 13:07:25 +020096 def test_uninstall(self):
Éric Araujo2c30e392011-10-06 04:59:41 +020097 dist, site_packages = self.install_dist()
98 self.assertIsFile(site_packages, 'foo', '__init__.py')
99 self.assertIsFile(site_packages, 'foo', 'sub', '__init__.py')
100 self.assertIsFile(site_packages, 'Foo-0.1.dist-info', 'RECORD')
101 self.assertTrue(remove('Foo', paths=[site_packages]))
102 self.assertIsNotFile(site_packages, 'foo', 'sub', '__init__.py')
103 self.assertIsNotFile(site_packages, 'Foo-0.1.dist-info', 'RECORD')
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200104
Éric Araujo8ed34a12011-10-06 05:18:41 +0200105 def test_uninstall_error_handling(self):
Tarek Ziadef47fa582011-05-30 23:26:51 +0200106 # makes sure if there are OSErrors (like permission denied)
Éric Araujofa23cc82011-10-06 05:15:09 +0200107 # remove() stops and displays a clean error
Éric Araujo2c30e392011-10-06 04:59:41 +0200108 dist, site_packages = self.install_dist('Meh')
Tarek Ziadef47fa582011-05-30 23:26:51 +0200109
110 # breaking os.rename
111 old = os.rename
112
113 def _rename(source, target):
Éric Araujofa23cc82011-10-06 05:15:09 +0200114 raise OSError(42, 'impossible operation')
Tarek Ziadef47fa582011-05-30 23:26:51 +0200115
116 os.rename = _rename
117 try:
Éric Araujo2c30e392011-10-06 04:59:41 +0200118 self.assertFalse(remove('Meh', paths=[site_packages]))
Tarek Ziadef47fa582011-05-30 23:26:51 +0200119 finally:
120 os.rename = old
121
Éric Araujofa23cc82011-10-06 05:15:09 +0200122 logs = [log for log in self.get_logs(logging.INFO)
123 if log.startswith('Error:')]
124 self.assertEqual(logs, ['Error: [Errno 42] impossible operation'])
125
Éric Araujo2c30e392011-10-06 04:59:41 +0200126 self.assertTrue(remove('Meh', paths=[site_packages]))
Tarek Ziadef47fa582011-05-30 23:26:51 +0200127
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200128
129def test_suite():
130 return unittest.makeSuite(UninstallTestCase)
131
132if __name__ == '__main__':
133 unittest.main(defaultTest='test_suite')