| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | """Tests for packaging.dist.""" | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import textwrap | 
					
						
							| 
									
										
										
										
											2011-09-18 20:11:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | import packaging.dist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from packaging.dist import Distribution | 
					
						
							|  |  |  | from packaging.command.cmd import Command | 
					
						
							|  |  |  | from packaging.errors import PackagingModuleError, PackagingOptionError | 
					
						
							|  |  |  | from packaging.tests import support, unittest | 
					
						
							| 
									
										
										
										
											2011-11-06 07:01:18 +01:00
										 |  |  | from packaging.tests.support import create_distribution, use_command | 
					
						
							| 
									
										
										
										
											2011-05-24 14:01:39 +02:00
										 |  |  | from test.support import unload | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class test_dist(Command): | 
					
						
							| 
									
										
										
										
											2011-11-06 07:01:18 +01:00
										 |  |  |     """Custom command used for testing.""" | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     user_options = [ | 
					
						
							| 
									
										
										
										
											2011-11-06 07:01:18 +01:00
										 |  |  |         ('sample-option=', 'S', | 
					
						
							|  |  |  |          "help text"), | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |         ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def initialize_options(self): | 
					
						
							|  |  |  |         self.sample_option = None | 
					
						
							| 
									
										
										
										
											2011-11-06 07:01:18 +01:00
										 |  |  |         self._record = [] | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def finalize_options(self): | 
					
						
							| 
									
										
										
										
											2011-11-06 07:01:18 +01:00
										 |  |  |         if self.sample_option is None: | 
					
						
							|  |  |  |             self.sample_option = 'default value' | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-19 08:49:20 +02:00
										 |  |  |     def run(self): | 
					
						
							| 
									
										
										
										
											2011-11-06 07:01:18 +01:00
										 |  |  |         self._record.append('test_dist has run') | 
					
						
							| 
									
										
										
										
											2011-10-19 08:49:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | class DistributionTestCase(support.TempdirManager, | 
					
						
							|  |  |  |                            support.LoggingCatcher, | 
					
						
							|  |  |  |                            support.EnvironRestorer, | 
					
						
							|  |  |  |                            unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-10 04:29:43 +02:00
										 |  |  |     restore_environ = ['HOME', 'PLAT'] | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         super(DistributionTestCase, self).setUp() | 
					
						
							| 
									
										
										
										
											2011-10-19 08:49:20 +02:00
										 |  |  |         # XXX this is ugly, we should fix the functions to accept args | 
					
						
							|  |  |  |         # (defaulting to sys.argv) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |         self.argv = sys.argv, sys.argv[:] | 
					
						
							|  |  |  |         del sys.argv[1:] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         sys.argv = self.argv[0] | 
					
						
							|  |  |  |         sys.argv[:] = self.argv[1] | 
					
						
							|  |  |  |         super(DistributionTestCase, self).tearDown() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-10 05:18:20 +02:00
										 |  |  |     @unittest.skip('needs to be updated') | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |     def test_debug_mode(self): | 
					
						
							| 
									
										
										
										
											2011-09-18 20:11:48 +02:00
										 |  |  |         tmpdir = self.mkdtemp() | 
					
						
							|  |  |  |         setupcfg = os.path.join(tmpdir, 'setup.cfg') | 
					
						
							|  |  |  |         with open(setupcfg, "w") as f: | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |             f.write("[global]\n") | 
					
						
							|  |  |  |             f.write("command_packages = foo.bar, splat") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-18 20:11:48 +02:00
										 |  |  |         files = [setupcfg] | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |         sys.argv.append("build") | 
					
						
							|  |  |  |         __, stdout = captured_stdout(create_distribution, files) | 
					
						
							|  |  |  |         self.assertEqual(stdout, '') | 
					
						
							| 
									
										
										
										
											2011-09-10 05:18:20 +02:00
										 |  |  |         # XXX debug mode does not exist anymore, test logging levels in this | 
					
						
							|  |  |  |         # test instead | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |         packaging.dist.DEBUG = True | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             __, stdout = captured_stdout(create_distribution, files) | 
					
						
							|  |  |  |             self.assertEqual(stdout, '') | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             packaging.dist.DEBUG = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_bad_attr(self): | 
					
						
							|  |  |  |         Distribution(attrs={'author': 'xxx', | 
					
						
							|  |  |  |                             'name': 'xxx', | 
					
						
							|  |  |  |                             'version': '1.2', | 
					
						
							| 
									
										
										
										
											2011-09-19 15:12:23 +02:00
										 |  |  |                             'home_page': 'xxxx', | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |                             'badoptname': 'xxx'}) | 
					
						
							| 
									
										
										
										
											2011-10-19 08:37:22 +02:00
										 |  |  |         logs = self.get_logs() | 
					
						
							| 
									
										
										
										
											2011-09-10 05:18:20 +02:00
										 |  |  |         self.assertEqual(len(logs), 1) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |         self.assertIn('unknown argument', logs[0]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_empty_options(self): | 
					
						
							|  |  |  |         # an empty options dictionary should not stay in the | 
					
						
							|  |  |  |         # list of attributes | 
					
						
							| 
									
										
										
										
											2011-09-10 05:18:20 +02:00
										 |  |  |         dist = Distribution(attrs={'author': 'xxx', 'name': 'xxx', | 
					
						
							| 
									
										
										
										
											2011-09-19 15:12:23 +02:00
										 |  |  |                                    'version': '1.2', 'home_page': 'xxxx', | 
					
						
							| 
									
										
										
										
											2011-09-10 05:18:20 +02:00
										 |  |  |                                    'options': {}}) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-19 08:37:22 +02:00
										 |  |  |         self.assertEqual(self.get_logs(), []) | 
					
						
							| 
									
										
										
										
											2011-09-10 05:18:20 +02:00
										 |  |  |         self.assertNotIn('options', dir(dist)) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_non_empty_options(self): | 
					
						
							|  |  |  |         # TODO: how to actually use options is not documented except | 
					
						
							|  |  |  |         # for a few cryptic comments in dist.py.  If this is to stay | 
					
						
							|  |  |  |         # in the public API, it deserves some better documentation. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Here is an example of how it's used out there: | 
					
						
							|  |  |  |         # http://svn.pythonmac.org/py2app/py2app/trunk/doc/ | 
					
						
							|  |  |  |         # index.html#specifying-customizations | 
					
						
							|  |  |  |         dist = Distribution(attrs={'author': 'xxx', | 
					
						
							|  |  |  |                                    'name': 'xxx', | 
					
						
							|  |  |  |                                    'version': 'xxx', | 
					
						
							| 
									
										
										
										
											2011-09-19 15:12:23 +02:00
										 |  |  |                                    'home_page': 'xxxx', | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |                                    'options': {'sdist': {'owner': 'root'}}}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertIn('owner', dist.get_option_dict('sdist')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_finalize_options(self): | 
					
						
							|  |  |  |         attrs = {'keywords': 'one,two', | 
					
						
							|  |  |  |                  'platform': 'one,two'} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dist = Distribution(attrs=attrs) | 
					
						
							|  |  |  |         dist.finalize_options() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # finalize_option splits platforms and keywords | 
					
						
							|  |  |  |         self.assertEqual(dist.metadata['platform'], ['one', 'two']) | 
					
						
							|  |  |  |         self.assertEqual(dist.metadata['keywords'], ['one', 'two']) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-10 05:18:20 +02:00
										 |  |  |     def test_custom_pydistutils(self): | 
					
						
							|  |  |  |         # Bug #2166:  make sure pydistutils.cfg is found | 
					
						
							|  |  |  |         if os.name == 'posix': | 
					
						
							|  |  |  |             user_filename = ".pydistutils.cfg" | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             user_filename = "pydistutils.cfg" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         temp_dir = self.mkdtemp() | 
					
						
							|  |  |  |         user_filename = os.path.join(temp_dir, user_filename) | 
					
						
							|  |  |  |         with open(user_filename, 'w') as f: | 
					
						
							|  |  |  |             f.write('.') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dist = Distribution() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         os.environ['HOME'] = temp_dir | 
					
						
							|  |  |  |         files = dist.find_config_files() | 
					
						
							|  |  |  |         self.assertIn(user_filename, files) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |     def test_find_config_files_disable(self): | 
					
						
							|  |  |  |         # Bug #1180: Allow users to disable their own config file. | 
					
						
							|  |  |  |         temp_home = self.mkdtemp() | 
					
						
							|  |  |  |         if os.name == 'posix': | 
					
						
							|  |  |  |             user_filename = os.path.join(temp_home, ".pydistutils.cfg") | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             user_filename = os.path.join(temp_home, "pydistutils.cfg") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with open(user_filename, 'w') as f: | 
					
						
							|  |  |  |             f.write('[distutils2]\n') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def _expander(path): | 
					
						
							|  |  |  |             return temp_home | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         old_expander = os.path.expanduser | 
					
						
							|  |  |  |         os.path.expanduser = _expander | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             d = packaging.dist.Distribution() | 
					
						
							|  |  |  |             all_files = d.find_config_files() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             d = packaging.dist.Distribution(attrs={'script_args': | 
					
						
							|  |  |  |                                                    ['--no-user-cfg']}) | 
					
						
							|  |  |  |             files = d.find_config_files() | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             os.path.expanduser = old_expander | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # make sure --no-user-cfg disables the user cfg file | 
					
						
							|  |  |  |         self.assertEqual((len(all_files) - 1), len(files)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_special_hooks_parsing(self): | 
					
						
							|  |  |  |         temp_home = self.mkdtemp() | 
					
						
							|  |  |  |         config_files = [os.path.join(temp_home, "config1.cfg"), | 
					
						
							|  |  |  |                         os.path.join(temp_home, "config2.cfg")] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Store two aliased hooks in config files | 
					
						
							|  |  |  |         self.write_file((temp_home, "config1.cfg"), | 
					
						
							|  |  |  |                         '[test_dist]\npre-hook.a = type') | 
					
						
							|  |  |  |         self.write_file((temp_home, "config2.cfg"), | 
					
						
							|  |  |  |                         '[test_dist]\npre-hook.b = type') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-06 07:01:18 +01:00
										 |  |  |         use_command(self, 'packaging.tests.test_dist.test_dist') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |         dist = create_distribution(config_files) | 
					
						
							|  |  |  |         cmd = dist.get_command_obj("test_dist") | 
					
						
							|  |  |  |         self.assertEqual(cmd.pre_hook, {"a": 'type', "b": 'type'}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_hooks_get_run(self): | 
					
						
							|  |  |  |         temp_home = self.mkdtemp() | 
					
						
							| 
									
										
										
										
											2011-05-19 14:46:10 +02:00
										 |  |  |         module_name = os.path.split(temp_home)[-1] | 
					
						
							|  |  |  |         pyname = '%s.py' % module_name | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |         config_file = os.path.join(temp_home, "config1.cfg") | 
					
						
							| 
									
										
										
										
											2011-05-19 14:46:10 +02:00
										 |  |  |         hooks_module = os.path.join(temp_home, pyname) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-17 13:24:33 +02:00
										 |  |  |         self.write_file(config_file, textwrap.dedent('''\
 | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |             [test_dist] | 
					
						
							| 
									
										
										
										
											2011-05-19 14:46:10 +02:00
										 |  |  |             pre-hook.test = %(modname)s.log_pre_call | 
					
						
							|  |  |  |             post-hook.test = %(modname)s.log_post_call'''
 | 
					
						
							|  |  |  |             % {'modname': module_name})) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-17 13:24:33 +02:00
										 |  |  |         self.write_file(hooks_module, textwrap.dedent('''\
 | 
					
						
							|  |  |  |             record = [] | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-17 13:24:33 +02:00
										 |  |  |             def log_pre_call(cmd): | 
					
						
							|  |  |  |                 record.append('pre-%s' % cmd.get_command_name()) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-17 13:24:33 +02:00
										 |  |  |             def log_post_call(cmd): | 
					
						
							|  |  |  |                 record.append('post-%s' % cmd.get_command_name()) | 
					
						
							|  |  |  |             '''))
 | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-06 07:01:18 +01:00
										 |  |  |         use_command(self, 'packaging.tests.test_dist.test_dist') | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |         d = create_distribution([config_file]) | 
					
						
							|  |  |  |         cmd = d.get_command_obj("test_dist") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # prepare the call recorders | 
					
						
							|  |  |  |         sys.path.append(temp_home) | 
					
						
							|  |  |  |         self.addCleanup(sys.path.remove, temp_home) | 
					
						
							| 
									
										
										
										
											2011-05-24 14:01:39 +02:00
										 |  |  |         self.addCleanup(unload, module_name) | 
					
						
							| 
									
										
										
										
											2011-05-19 14:46:10 +02:00
										 |  |  |         record = __import__(module_name).record | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         cmd.run = lambda: record.append('run') | 
					
						
							|  |  |  |         cmd.finalize_options = lambda: record.append('finalize') | 
					
						
							| 
									
										
										
										
											2011-06-17 13:24:33 +02:00
										 |  |  |         d.run_command('test_dist') | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(record, ['finalize', | 
					
						
							|  |  |  |                                   'pre-test_dist', | 
					
						
							|  |  |  |                                   'run', | 
					
						
							|  |  |  |                                   'post-test_dist']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_hooks_importable(self): | 
					
						
							|  |  |  |         temp_home = self.mkdtemp() | 
					
						
							|  |  |  |         config_file = os.path.join(temp_home, "config1.cfg") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-17 13:24:33 +02:00
										 |  |  |         self.write_file(config_file, textwrap.dedent('''\
 | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |             [test_dist] | 
					
						
							|  |  |  |             pre-hook.test = nonexistent.dotted.name'''))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-06 07:01:18 +01:00
										 |  |  |         use_command(self, 'packaging.tests.test_dist.test_dist') | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |         d = create_distribution([config_file]) | 
					
						
							|  |  |  |         cmd = d.get_command_obj("test_dist") | 
					
						
							|  |  |  |         cmd.ensure_finalized() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertRaises(PackagingModuleError, d.run_command, 'test_dist') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_hooks_callable(self): | 
					
						
							|  |  |  |         temp_home = self.mkdtemp() | 
					
						
							|  |  |  |         config_file = os.path.join(temp_home, "config1.cfg") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-17 13:24:33 +02:00
										 |  |  |         self.write_file(config_file, textwrap.dedent('''\
 | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |             [test_dist] | 
					
						
							|  |  |  |             pre-hook.test = packaging.tests.test_dist.__doc__'''))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-06 07:01:18 +01:00
										 |  |  |         use_command(self, 'packaging.tests.test_dist.test_dist') | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |         d = create_distribution([config_file]) | 
					
						
							|  |  |  |         cmd = d.get_command_obj("test_dist") | 
					
						
							|  |  |  |         cmd.ensure_finalized() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertRaises(PackagingOptionError, d.run_command, 'test_dist') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_suite(): | 
					
						
							| 
									
										
										
										
											2011-09-10 05:18:20 +02:00
										 |  |  |     return unittest.makeSuite(DistributionTestCase) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     unittest.main(defaultTest="test_suite") |