2011-05-19 08:07:25 -03:00
|
|
|
"""Tests for packaging.dist."""
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import textwrap
|
2011-09-18 15:11:48 -03:00
|
|
|
|
2011-05-19 08:07:25 -03:00
|
|
|
import packaging.dist
|
|
|
|
|
|
|
|
from packaging.dist import Distribution
|
2011-10-19 03:49:20 -03:00
|
|
|
from packaging.command import set_command, _COMMANDS
|
2011-05-19 08:07:25 -03:00
|
|
|
from packaging.command.cmd import Command
|
|
|
|
from packaging.errors import PackagingModuleError, PackagingOptionError
|
2011-09-18 15:11:48 -03:00
|
|
|
from packaging.tests import captured_stdout
|
2011-05-19 08:07:25 -03:00
|
|
|
from packaging.tests import support, unittest
|
|
|
|
from packaging.tests.support import create_distribution
|
2011-05-24 09:01:39 -03:00
|
|
|
from test.support import unload
|
2011-05-19 08:07:25 -03:00
|
|
|
|
|
|
|
|
|
|
|
class test_dist(Command):
|
|
|
|
"""Sample packaging extension command."""
|
|
|
|
|
|
|
|
user_options = [
|
|
|
|
("sample-option=", "S", "help text"),
|
|
|
|
]
|
|
|
|
|
|
|
|
def initialize_options(self):
|
|
|
|
self.sample_option = None
|
|
|
|
|
|
|
|
def finalize_options(self):
|
|
|
|
pass
|
|
|
|
|
2011-10-19 03:49:20 -03:00
|
|
|
def run(self):
|
|
|
|
pass
|
|
|
|
|
2011-05-19 08:07:25 -03:00
|
|
|
|
|
|
|
class DistributionTestCase(support.TempdirManager,
|
|
|
|
support.LoggingCatcher,
|
|
|
|
support.EnvironRestorer,
|
|
|
|
unittest.TestCase):
|
|
|
|
|
2011-06-09 23:29:43 -03:00
|
|
|
restore_environ = ['HOME', 'PLAT']
|
2011-05-19 08:07:25 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(DistributionTestCase, self).setUp()
|
2011-10-19 03:49:20 -03:00
|
|
|
# XXX this is ugly, we should fix the functions to accept args
|
|
|
|
# (defaulting to sys.argv)
|
2011-05-19 08:07:25 -03:00
|
|
|
self.argv = sys.argv, sys.argv[:]
|
|
|
|
del sys.argv[1:]
|
2011-10-19 03:49:20 -03:00
|
|
|
self._commands = _COMMANDS.copy()
|
2011-05-19 08:07:25 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.argv = self.argv[0]
|
|
|
|
sys.argv[:] = self.argv[1]
|
2011-10-19 03:49:20 -03:00
|
|
|
# XXX maybe we need a public API to remove commands
|
|
|
|
_COMMANDS.clear()
|
|
|
|
_COMMANDS.update(self._commands)
|
2011-05-19 08:07:25 -03:00
|
|
|
super(DistributionTestCase, self).tearDown()
|
|
|
|
|
2011-09-10 00:18:20 -03:00
|
|
|
@unittest.skip('needs to be updated')
|
2011-05-19 08:07:25 -03:00
|
|
|
def test_debug_mode(self):
|
2011-09-18 15:11:48 -03:00
|
|
|
tmpdir = self.mkdtemp()
|
|
|
|
setupcfg = os.path.join(tmpdir, 'setup.cfg')
|
|
|
|
with open(setupcfg, "w") as f:
|
2011-05-19 08:07:25 -03:00
|
|
|
f.write("[global]\n")
|
|
|
|
f.write("command_packages = foo.bar, splat")
|
|
|
|
|
2011-09-18 15:11:48 -03:00
|
|
|
files = [setupcfg]
|
2011-05-19 08:07:25 -03:00
|
|
|
sys.argv.append("build")
|
|
|
|
__, stdout = captured_stdout(create_distribution, files)
|
|
|
|
self.assertEqual(stdout, '')
|
2011-09-10 00:18:20 -03:00
|
|
|
# XXX debug mode does not exist anymore, test logging levels in this
|
|
|
|
# test instead
|
2011-05-19 08:07:25 -03: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 10:12:23 -03:00
|
|
|
'home_page': 'xxxx',
|
2011-05-19 08:07:25 -03:00
|
|
|
'badoptname': 'xxx'})
|
2011-10-19 03:37:22 -03:00
|
|
|
logs = self.get_logs()
|
2011-09-10 00:18:20 -03:00
|
|
|
self.assertEqual(len(logs), 1)
|
2011-05-19 08:07:25 -03: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 00:18:20 -03:00
|
|
|
dist = Distribution(attrs={'author': 'xxx', 'name': 'xxx',
|
2011-09-19 10:12:23 -03:00
|
|
|
'version': '1.2', 'home_page': 'xxxx',
|
2011-09-10 00:18:20 -03:00
|
|
|
'options': {}})
|
2011-05-19 08:07:25 -03:00
|
|
|
|
2011-10-19 03:37:22 -03:00
|
|
|
self.assertEqual(self.get_logs(), [])
|
2011-09-10 00:18:20 -03:00
|
|
|
self.assertNotIn('options', dir(dist))
|
2011-05-19 08:07:25 -03: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 10:12:23 -03:00
|
|
|
'home_page': 'xxxx',
|
2011-05-19 08:07:25 -03: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 00:18:20 -03: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 08:07:25 -03: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')
|
|
|
|
|
|
|
|
set_command('packaging.tests.test_dist.test_dist')
|
|
|
|
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 09:46:10 -03:00
|
|
|
module_name = os.path.split(temp_home)[-1]
|
|
|
|
pyname = '%s.py' % module_name
|
2011-05-19 08:07:25 -03:00
|
|
|
config_file = os.path.join(temp_home, "config1.cfg")
|
2011-05-19 09:46:10 -03:00
|
|
|
hooks_module = os.path.join(temp_home, pyname)
|
2011-05-19 08:07:25 -03:00
|
|
|
|
2011-06-17 08:24:33 -03:00
|
|
|
self.write_file(config_file, textwrap.dedent('''\
|
2011-05-19 08:07:25 -03:00
|
|
|
[test_dist]
|
2011-05-19 09:46:10 -03:00
|
|
|
pre-hook.test = %(modname)s.log_pre_call
|
|
|
|
post-hook.test = %(modname)s.log_post_call'''
|
|
|
|
% {'modname': module_name}))
|
2011-05-19 08:07:25 -03:00
|
|
|
|
2011-06-17 08:24:33 -03:00
|
|
|
self.write_file(hooks_module, textwrap.dedent('''\
|
|
|
|
record = []
|
2011-05-19 08:07:25 -03:00
|
|
|
|
2011-06-17 08:24:33 -03:00
|
|
|
def log_pre_call(cmd):
|
|
|
|
record.append('pre-%s' % cmd.get_command_name())
|
2011-05-19 08:07:25 -03:00
|
|
|
|
2011-06-17 08:24:33 -03:00
|
|
|
def log_post_call(cmd):
|
|
|
|
record.append('post-%s' % cmd.get_command_name())
|
|
|
|
'''))
|
2011-05-19 08:07:25 -03:00
|
|
|
|
|
|
|
set_command('packaging.tests.test_dist.test_dist')
|
|
|
|
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 09:01:39 -03:00
|
|
|
self.addCleanup(unload, module_name)
|
2011-05-19 09:46:10 -03:00
|
|
|
record = __import__(module_name).record
|
2011-05-19 08:07:25 -03:00
|
|
|
|
|
|
|
cmd.run = lambda: record.append('run')
|
|
|
|
cmd.finalize_options = lambda: record.append('finalize')
|
2011-06-17 08:24:33 -03:00
|
|
|
d.run_command('test_dist')
|
2011-05-19 08:07:25 -03: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 08:24:33 -03:00
|
|
|
self.write_file(config_file, textwrap.dedent('''\
|
2011-05-19 08:07:25 -03:00
|
|
|
[test_dist]
|
|
|
|
pre-hook.test = nonexistent.dotted.name'''))
|
|
|
|
|
|
|
|
set_command('packaging.tests.test_dist.test_dist')
|
|
|
|
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 08:24:33 -03:00
|
|
|
self.write_file(config_file, textwrap.dedent('''\
|
2011-05-19 08:07:25 -03:00
|
|
|
[test_dist]
|
|
|
|
pre-hook.test = packaging.tests.test_dist.__doc__'''))
|
|
|
|
|
|
|
|
set_command('packaging.tests.test_dist.test_dist')
|
|
|
|
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 00:18:20 -03:00
|
|
|
return unittest.makeSuite(DistributionTestCase)
|
2011-05-19 08:07:25 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main(defaultTest="test_suite")
|