| 
									
										
										
										
											2011-10-06 05:18:41 +02:00
										 |  |  | """Tests for the packaging.uninstall module.""" | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2011-10-06 05:15:09 +02:00
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2011-06-17 15:43:18 +02:00
										 |  |  | import packaging.util | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from packaging.errors import PackagingError | 
					
						
							|  |  |  | from packaging.install import remove | 
					
						
							| 
									
										
										
										
											2011-10-06 05:18:41 +02:00
										 |  |  | from packaging.database import disable_cache, enable_cache | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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(enable_cache) | 
					
						
							| 
									
										
										
										
											2011-11-14 19:46:31 +01:00
										 |  |  |         self.addCleanup(packaging.util._path_created.clear) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |         disable_cache() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_path(self, dist, name): | 
					
						
							| 
									
										
										
										
											2011-10-06 05:28:56 +02:00
										 |  |  |         # the dist argument must contain an install_dist command correctly | 
					
						
							|  |  |  |         # initialized with a prefix option and finalized befored this method | 
					
						
							|  |  |  |         # can be called successfully; practically, this means that you should | 
					
						
							|  |  |  |         # call self.install_dist before self.get_path | 
					
						
							|  |  |  |         cmd = dist.get_command_obj('install_dist') | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |         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) | 
					
						
							| 
									
										
										
										
											2011-10-19 08:49:20 +02:00
										 |  |  |         os.makedirs(os.path.join(pkg_dir, 'sub')) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         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-10-06 05:28:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         dist = support.TestDistribution() | 
					
						
							|  |  |  |         # for some unfathomable reason, the tests will fail horribly if the | 
					
						
							|  |  |  |         # parse_config_files method is not called, even if it doesn't do | 
					
						
							|  |  |  |         # anything useful; trying to build and use a command object manually | 
					
						
							|  |  |  |         # also fails | 
					
						
							|  |  |  |         dist.parse_config_files() | 
					
						
							|  |  |  |         dist.finalize_options() | 
					
						
							|  |  |  |         dist.run_command('install_dist', | 
					
						
							| 
									
										
										
										
											2011-11-14 19:46:31 +01:00
										 |  |  |                          {'prefix': ('command line', self.mkdtemp())}) | 
					
						
							| 
									
										
										
										
											2011-10-06 05:28:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 04:59:41 +02:00
										 |  |  |         site_packages = self.get_path(dist, 'purelib') | 
					
						
							|  |  |  |         return dist, site_packages | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 05:10:09 +02:00
										 |  |  |     def test_uninstall_unknown_distribution(self): | 
					
						
							|  |  |  |         dist, site_packages = self.install_dist('Foospam') | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |         self.assertRaises(PackagingError, remove, 'Foo', | 
					
						
							| 
									
										
										
										
											2011-10-06 05:10:09 +02:00
										 |  |  |                           paths=[site_packages]) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_uninstall(self): | 
					
						
							| 
									
										
										
										
											2011-10-06 04:59:41 +02: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 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 05:18:41 +02:00
										 |  |  |     def test_uninstall_error_handling(self): | 
					
						
							| 
									
										
										
										
											2011-05-30 23:26:51 +02:00
										 |  |  |         # makes sure if there are OSErrors (like permission denied) | 
					
						
							| 
									
										
										
										
											2011-10-06 05:15:09 +02:00
										 |  |  |         # remove() stops and displays a clean error | 
					
						
							| 
									
										
										
										
											2011-10-06 04:59:41 +02:00
										 |  |  |         dist, site_packages = self.install_dist('Meh') | 
					
						
							| 
									
										
										
										
											2011-05-30 23:26:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # breaking os.rename | 
					
						
							|  |  |  |         old = os.rename | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def _rename(source, target): | 
					
						
							| 
									
										
										
										
											2011-10-06 05:15:09 +02:00
										 |  |  |             raise OSError(42, 'impossible operation') | 
					
						
							| 
									
										
										
										
											2011-05-30 23:26:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         os.rename = _rename | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2011-10-06 04:59:41 +02:00
										 |  |  |             self.assertFalse(remove('Meh', paths=[site_packages])) | 
					
						
							| 
									
										
										
										
											2011-05-30 23:26:51 +02:00
										 |  |  |         finally: | 
					
						
							|  |  |  |             os.rename = old | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 05:15:09 +02:00
										 |  |  |         logs = [log for log in self.get_logs(logging.INFO) | 
					
						
							|  |  |  |                 if log.startswith('Error:')] | 
					
						
							|  |  |  |         self.assertEqual(logs, ['Error: [Errno 42] impossible operation']) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 04:59:41 +02:00
										 |  |  |         self.assertTrue(remove('Meh', paths=[site_packages])) | 
					
						
							| 
									
										
										
										
											2011-05-30 23:26:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_suite(): | 
					
						
							|  |  |  |     return unittest.makeSuite(UninstallTestCase) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     unittest.main(defaultTest='test_suite') |