2009-06-01 19:22:13 -03:00
|
|
|
# -*- coding: utf8 -*-
|
2008-09-03 08:13:56 -03:00
|
|
|
|
2004-08-03 13:37:40 -03:00
|
|
|
"""Tests for distutils.dist."""
|
|
|
|
import os
|
2005-03-20 18:19:47 -04:00
|
|
|
import StringIO
|
2004-08-03 13:37:40 -03:00
|
|
|
import sys
|
|
|
|
import unittest
|
2008-12-29 18:23:53 -04:00
|
|
|
import warnings
|
2004-08-03 13:37:40 -03:00
|
|
|
|
2009-05-17 07:44:12 -03:00
|
|
|
from distutils.dist import Distribution, fix_help_options
|
|
|
|
from distutils.cmd import Command
|
2009-09-21 10:23:35 -03:00
|
|
|
import distutils.dist
|
2009-05-17 07:44:12 -03:00
|
|
|
|
|
|
|
from test.test_support import TESTFN, captured_stdout
|
2009-02-14 10:10:23 -04:00
|
|
|
from distutils.tests import support
|
2004-08-03 13:37:40 -03:00
|
|
|
|
2009-05-17 07:44:12 -03:00
|
|
|
class test_dist(Command):
|
2004-08-03 13:37:40 -03:00
|
|
|
"""Sample distutils extension command."""
|
|
|
|
|
|
|
|
user_options = [
|
|
|
|
("sample-option=", "S", "help text"),
|
|
|
|
]
|
|
|
|
|
|
|
|
def initialize_options(self):
|
|
|
|
self.sample_option = None
|
|
|
|
|
|
|
|
|
2009-05-17 07:44:12 -03:00
|
|
|
class TestDistribution(Distribution):
|
2004-08-03 13:37:40 -03:00
|
|
|
"""Distribution subclasses that avoids the default search for
|
|
|
|
configuration files.
|
|
|
|
|
|
|
|
The ._config_files attribute must be set before
|
|
|
|
.parse_config_files() is called.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def find_config_files(self):
|
|
|
|
return self._config_files
|
|
|
|
|
|
|
|
|
2009-06-01 19:22:13 -03:00
|
|
|
class DistributionTestCase(support.TempdirManager,
|
|
|
|
support.LoggingSilencer,
|
2009-10-18 06:28:26 -03:00
|
|
|
support.EnvironGuard,
|
2009-06-01 19:22:13 -03:00
|
|
|
unittest.TestCase):
|
2004-08-03 13:37:40 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
2009-05-10 09:17:30 -03:00
|
|
|
super(DistributionTestCase, self).setUp()
|
2009-10-18 06:28:26 -03:00
|
|
|
self.argv = sys.argv, sys.argv[:]
|
2004-08-03 13:37:40 -03:00
|
|
|
del sys.argv[1:]
|
|
|
|
|
|
|
|
def tearDown(self):
|
2009-10-18 06:28:26 -03:00
|
|
|
sys.argv = self.argv[0]
|
|
|
|
sys.argv[:] = self.argv[1]
|
2009-05-10 09:17:30 -03:00
|
|
|
super(DistributionTestCase, self).tearDown()
|
2004-08-03 13:37:40 -03:00
|
|
|
|
|
|
|
def create_distribution(self, configfiles=()):
|
|
|
|
d = TestDistribution()
|
|
|
|
d._config_files = configfiles
|
|
|
|
d.parse_config_files()
|
|
|
|
d.parse_command_line()
|
|
|
|
return d
|
|
|
|
|
2009-09-21 10:23:35 -03:00
|
|
|
def test_debug_mode(self):
|
|
|
|
with open(TESTFN, "w") as f:
|
|
|
|
f.write("[global]")
|
|
|
|
f.write("command_packages = foo.bar, splat")
|
|
|
|
|
|
|
|
files = [TESTFN]
|
|
|
|
sys.argv.append("build")
|
|
|
|
|
|
|
|
with captured_stdout() as stdout:
|
|
|
|
self.create_distribution(files)
|
|
|
|
stdout.seek(0)
|
|
|
|
self.assertEquals(stdout.read(), '')
|
|
|
|
distutils.dist.DEBUG = True
|
|
|
|
try:
|
|
|
|
with captured_stdout() as stdout:
|
|
|
|
self.create_distribution(files)
|
|
|
|
stdout.seek(0)
|
|
|
|
self.assertEquals(stdout.read(), '')
|
|
|
|
finally:
|
|
|
|
distutils.dist.DEBUG = False
|
|
|
|
|
2004-08-03 13:37:40 -03:00
|
|
|
def test_command_packages_unspecified(self):
|
|
|
|
sys.argv.append("build")
|
|
|
|
d = self.create_distribution()
|
|
|
|
self.assertEqual(d.get_command_packages(), ["distutils.command"])
|
|
|
|
|
|
|
|
def test_command_packages_cmdline(self):
|
2008-05-11 11:00:00 -03:00
|
|
|
from distutils.tests.test_dist import test_dist
|
2004-08-03 13:37:40 -03:00
|
|
|
sys.argv.extend(["--command-packages",
|
|
|
|
"foo.bar,distutils.tests",
|
|
|
|
"test_dist",
|
|
|
|
"-Ssometext",
|
|
|
|
])
|
|
|
|
d = self.create_distribution()
|
|
|
|
# let's actually try to load our test command:
|
|
|
|
self.assertEqual(d.get_command_packages(),
|
|
|
|
["distutils.command", "foo.bar", "distutils.tests"])
|
|
|
|
cmd = d.get_command_obj("test_dist")
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(isinstance(cmd, test_dist))
|
2004-08-03 13:37:40 -03:00
|
|
|
self.assertEqual(cmd.sample_option, "sometext")
|
|
|
|
|
|
|
|
def test_command_packages_configfile(self):
|
|
|
|
sys.argv.append("build")
|
|
|
|
f = open(TESTFN, "w")
|
|
|
|
try:
|
|
|
|
print >>f, "[global]"
|
|
|
|
print >>f, "command_packages = foo.bar, splat"
|
|
|
|
f.close()
|
|
|
|
d = self.create_distribution([TESTFN])
|
|
|
|
self.assertEqual(d.get_command_packages(),
|
|
|
|
["distutils.command", "foo.bar", "splat"])
|
|
|
|
|
|
|
|
# ensure command line overrides config:
|
|
|
|
sys.argv[1:] = ["--command-packages", "spork", "build"]
|
|
|
|
d = self.create_distribution([TESTFN])
|
|
|
|
self.assertEqual(d.get_command_packages(),
|
|
|
|
["distutils.command", "spork"])
|
|
|
|
|
|
|
|
# Setting --command-packages to '' should cause the default to
|
|
|
|
# be used even if a config file specified something else:
|
|
|
|
sys.argv[1:] = ["--command-packages", "", "build"]
|
|
|
|
d = self.create_distribution([TESTFN])
|
|
|
|
self.assertEqual(d.get_command_packages(), ["distutils.command"])
|
|
|
|
|
|
|
|
finally:
|
|
|
|
os.unlink(TESTFN)
|
|
|
|
|
2008-09-03 08:13:56 -03:00
|
|
|
def test_write_pkg_file(self):
|
|
|
|
# Check DistributionMetadata handling of Unicode fields
|
2009-02-14 10:10:23 -04:00
|
|
|
tmp_dir = self.mkdtemp()
|
|
|
|
my_file = os.path.join(tmp_dir, 'f')
|
2009-05-17 07:44:12 -03:00
|
|
|
klass = Distribution
|
2008-09-03 08:13:56 -03:00
|
|
|
|
2009-06-01 19:22:13 -03:00
|
|
|
dist = klass(attrs={'author': u'Mister Café',
|
2008-09-03 08:13:56 -03:00
|
|
|
'name': 'my.package',
|
2009-06-01 19:22:13 -03:00
|
|
|
'maintainer': u'Café Junior',
|
|
|
|
'description': u'Café torréfié',
|
|
|
|
'long_description': u'Héhéhé'})
|
2008-09-03 08:13:56 -03:00
|
|
|
|
|
|
|
|
|
|
|
# let's make sure the file can be written
|
|
|
|
# with Unicode fields. they are encoded with
|
|
|
|
# PKG_INFO_ENCODING
|
2009-02-14 10:10:23 -04:00
|
|
|
dist.metadata.write_pkg_file(open(my_file, 'w'))
|
2008-09-03 08:13:56 -03:00
|
|
|
|
|
|
|
# regular ascii is of course always usable
|
|
|
|
dist = klass(attrs={'author': 'Mister Cafe',
|
|
|
|
'name': 'my.package',
|
|
|
|
'maintainer': 'Cafe Junior',
|
|
|
|
'description': 'Cafe torrefie',
|
|
|
|
'long_description': 'Hehehe'})
|
|
|
|
|
2009-02-14 10:10:23 -04:00
|
|
|
my_file2 = os.path.join(tmp_dir, 'f2')
|
|
|
|
dist.metadata.write_pkg_file(open(my_file, 'w'))
|
2004-08-03 13:37:40 -03:00
|
|
|
|
2008-12-29 18:23:53 -04:00
|
|
|
def test_empty_options(self):
|
|
|
|
# an empty options dictionary should not stay in the
|
|
|
|
# list of attributes
|
2009-05-17 07:44:12 -03:00
|
|
|
klass = Distribution
|
2008-12-29 18:23:53 -04:00
|
|
|
|
|
|
|
# catching warnings
|
|
|
|
warns = []
|
|
|
|
def _warn(msg):
|
|
|
|
warns.append(msg)
|
|
|
|
|
|
|
|
old_warn = warnings.warn
|
|
|
|
warnings.warn = _warn
|
|
|
|
try:
|
|
|
|
dist = klass(attrs={'author': 'xxx',
|
|
|
|
'name': 'xxx',
|
|
|
|
'version': 'xxx',
|
|
|
|
'url': 'xxxx',
|
|
|
|
'options': {}})
|
|
|
|
finally:
|
|
|
|
warnings.warn = old_warn
|
|
|
|
|
|
|
|
self.assertEquals(len(warns), 0)
|
|
|
|
|
2009-06-01 19:22:13 -03:00
|
|
|
def test_finalize_options(self):
|
|
|
|
|
|
|
|
attrs = {'keywords': 'one,two',
|
|
|
|
'platforms': 'one,two'}
|
|
|
|
|
|
|
|
dist = Distribution(attrs=attrs)
|
|
|
|
dist.finalize_options()
|
|
|
|
|
|
|
|
# finalize_option splits platforms and keywords
|
|
|
|
self.assertEquals(dist.metadata.platforms, ['one', 'two'])
|
|
|
|
self.assertEquals(dist.metadata.keywords, ['one', 'two'])
|
|
|
|
|
|
|
|
def test_get_command_packages(self):
|
|
|
|
dist = Distribution()
|
|
|
|
self.assertEquals(dist.command_packages, None)
|
|
|
|
cmds = dist.get_command_packages()
|
|
|
|
self.assertEquals(cmds, ['distutils.command'])
|
|
|
|
self.assertEquals(dist.command_packages,
|
|
|
|
['distutils.command'])
|
|
|
|
|
|
|
|
dist.command_packages = 'one,two'
|
|
|
|
cmds = dist.get_command_packages()
|
|
|
|
self.assertEquals(cmds, ['distutils.command', 'one', 'two'])
|
|
|
|
|
|
|
|
|
2009-09-21 10:41:08 -03:00
|
|
|
def test_announce(self):
|
|
|
|
# make sure the level is known
|
|
|
|
dist = Distribution()
|
|
|
|
args = ('ok',)
|
|
|
|
kwargs = {'level': 'ok2'}
|
|
|
|
self.assertRaises(ValueError, dist.announce, args, kwargs)
|
|
|
|
|
2009-05-10 09:17:30 -03:00
|
|
|
class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
|
|
|
|
unittest.TestCase):
|
2005-03-20 18:19:47 -04:00
|
|
|
|
2009-10-18 06:28:26 -03:00
|
|
|
def setUp(self):
|
|
|
|
super(MetadataTestCase, self).setUp()
|
|
|
|
self.argv = sys.argv, sys.argv[:]
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.argv = self.argv[0]
|
|
|
|
sys.argv[:] = self.argv[1]
|
|
|
|
super(MetadataTestCase, self).tearDown()
|
|
|
|
|
2005-03-20 18:19:47 -04:00
|
|
|
def test_simple_metadata(self):
|
|
|
|
attrs = {"name": "package",
|
|
|
|
"version": "1.0"}
|
2009-05-17 07:44:12 -03:00
|
|
|
dist = Distribution(attrs)
|
2005-03-20 18:19:47 -04:00
|
|
|
meta = self.format_metadata(dist)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue("Metadata-Version: 1.0" in meta)
|
|
|
|
self.assertTrue("provides:" not in meta.lower())
|
|
|
|
self.assertTrue("requires:" not in meta.lower())
|
|
|
|
self.assertTrue("obsoletes:" not in meta.lower())
|
2005-03-20 18:19:47 -04:00
|
|
|
|
|
|
|
def test_provides(self):
|
|
|
|
attrs = {"name": "package",
|
|
|
|
"version": "1.0",
|
|
|
|
"provides": ["package", "package.sub"]}
|
2009-05-17 07:44:12 -03:00
|
|
|
dist = Distribution(attrs)
|
2005-03-20 18:19:47 -04:00
|
|
|
self.assertEqual(dist.metadata.get_provides(),
|
|
|
|
["package", "package.sub"])
|
|
|
|
self.assertEqual(dist.get_provides(),
|
|
|
|
["package", "package.sub"])
|
|
|
|
meta = self.format_metadata(dist)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue("Metadata-Version: 1.1" in meta)
|
|
|
|
self.assertTrue("requires:" not in meta.lower())
|
|
|
|
self.assertTrue("obsoletes:" not in meta.lower())
|
2005-03-20 18:19:47 -04:00
|
|
|
|
|
|
|
def test_provides_illegal(self):
|
2009-05-17 07:44:12 -03:00
|
|
|
self.assertRaises(ValueError, Distribution,
|
2005-03-20 18:19:47 -04:00
|
|
|
{"name": "package",
|
|
|
|
"version": "1.0",
|
|
|
|
"provides": ["my.pkg (splat)"]})
|
|
|
|
|
|
|
|
def test_requires(self):
|
|
|
|
attrs = {"name": "package",
|
|
|
|
"version": "1.0",
|
|
|
|
"requires": ["other", "another (==1.0)"]}
|
2009-05-17 07:44:12 -03:00
|
|
|
dist = Distribution(attrs)
|
2005-03-20 18:19:47 -04:00
|
|
|
self.assertEqual(dist.metadata.get_requires(),
|
|
|
|
["other", "another (==1.0)"])
|
|
|
|
self.assertEqual(dist.get_requires(),
|
|
|
|
["other", "another (==1.0)"])
|
|
|
|
meta = self.format_metadata(dist)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue("Metadata-Version: 1.1" in meta)
|
|
|
|
self.assertTrue("provides:" not in meta.lower())
|
|
|
|
self.assertTrue("Requires: other" in meta)
|
|
|
|
self.assertTrue("Requires: another (==1.0)" in meta)
|
|
|
|
self.assertTrue("obsoletes:" not in meta.lower())
|
2005-03-20 18:19:47 -04:00
|
|
|
|
|
|
|
def test_requires_illegal(self):
|
2009-05-17 07:44:12 -03:00
|
|
|
self.assertRaises(ValueError, Distribution,
|
2005-03-20 18:19:47 -04:00
|
|
|
{"name": "package",
|
|
|
|
"version": "1.0",
|
|
|
|
"requires": ["my.pkg (splat)"]})
|
|
|
|
|
|
|
|
def test_obsoletes(self):
|
|
|
|
attrs = {"name": "package",
|
|
|
|
"version": "1.0",
|
|
|
|
"obsoletes": ["other", "another (<1.0)"]}
|
2009-05-17 07:44:12 -03:00
|
|
|
dist = Distribution(attrs)
|
2005-03-20 18:19:47 -04:00
|
|
|
self.assertEqual(dist.metadata.get_obsoletes(),
|
|
|
|
["other", "another (<1.0)"])
|
|
|
|
self.assertEqual(dist.get_obsoletes(),
|
|
|
|
["other", "another (<1.0)"])
|
|
|
|
meta = self.format_metadata(dist)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue("Metadata-Version: 1.1" in meta)
|
|
|
|
self.assertTrue("provides:" not in meta.lower())
|
|
|
|
self.assertTrue("requires:" not in meta.lower())
|
|
|
|
self.assertTrue("Obsoletes: other" in meta)
|
|
|
|
self.assertTrue("Obsoletes: another (<1.0)" in meta)
|
2005-03-20 18:19:47 -04:00
|
|
|
|
|
|
|
def test_obsoletes_illegal(self):
|
2009-05-17 07:44:12 -03:00
|
|
|
self.assertRaises(ValueError, Distribution,
|
2005-03-20 18:19:47 -04:00
|
|
|
{"name": "package",
|
|
|
|
"version": "1.0",
|
|
|
|
"obsoletes": ["my.pkg (splat)"]})
|
2005-03-27 21:08:02 -04:00
|
|
|
|
2005-03-20 18:19:47 -04:00
|
|
|
def format_metadata(self, dist):
|
|
|
|
sio = StringIO.StringIO()
|
|
|
|
dist.metadata.write_pkg_file(sio)
|
|
|
|
return sio.getvalue()
|
|
|
|
|
2008-05-11 11:00:00 -03:00
|
|
|
def test_custom_pydistutils(self):
|
|
|
|
# fixes #2166
|
|
|
|
# make sure pydistutils.cfg is found
|
|
|
|
if os.name == 'posix':
|
|
|
|
user_filename = ".pydistutils.cfg"
|
|
|
|
else:
|
|
|
|
user_filename = "pydistutils.cfg"
|
|
|
|
|
2009-02-14 10:10:23 -04:00
|
|
|
temp_dir = self.mkdtemp()
|
|
|
|
user_filename = os.path.join(temp_dir, user_filename)
|
2008-05-11 11:00:00 -03:00
|
|
|
f = open(user_filename, 'w')
|
|
|
|
f.write('.')
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
try:
|
2009-05-17 07:44:12 -03:00
|
|
|
dist = Distribution()
|
2008-05-11 11:00:00 -03:00
|
|
|
|
|
|
|
# linux-style
|
|
|
|
if sys.platform in ('linux', 'darwin'):
|
2009-10-18 06:28:26 -03:00
|
|
|
os.environ['HOME'] = temp_dir
|
2008-05-11 11:00:00 -03:00
|
|
|
files = dist.find_config_files()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(user_filename in files)
|
2008-05-11 11:00:00 -03:00
|
|
|
|
|
|
|
# win32-style
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
# home drive should be found
|
2009-10-18 06:28:26 -03:00
|
|
|
os.environ['HOME'] = temp_dir
|
2008-05-11 11:00:00 -03:00
|
|
|
files = dist.find_config_files()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(user_filename in files,
|
2008-05-11 16:15:52 -03:00
|
|
|
'%r not found in %r' % (user_filename, files))
|
2008-05-11 11:00:00 -03:00
|
|
|
finally:
|
|
|
|
os.remove(user_filename)
|
2005-03-20 18:19:47 -04:00
|
|
|
|
2009-05-17 07:44:12 -03:00
|
|
|
def test_fix_help_options(self):
|
|
|
|
help_tuples = [('a', 'b', 'c', 'd'), (1, 2, 3, 4)]
|
|
|
|
fancy_options = fix_help_options(help_tuples)
|
|
|
|
self.assertEquals(fancy_options[0], ('a', 'b', 'c'))
|
|
|
|
self.assertEquals(fancy_options[1], (1, 2, 3))
|
|
|
|
|
|
|
|
def test_show_help(self):
|
|
|
|
# smoke test, just makes sure some help is displayed
|
|
|
|
dist = Distribution()
|
|
|
|
sys.argv = []
|
2009-10-18 06:28:26 -03:00
|
|
|
dist.help = 1
|
|
|
|
dist.script_name = 'setup.py'
|
|
|
|
with captured_stdout() as s:
|
|
|
|
dist.parse_command_line()
|
2009-05-17 07:44:12 -03:00
|
|
|
|
|
|
|
output = [line for line in s.getvalue().split('\n')
|
|
|
|
if line.strip() != '']
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(len(output) > 0)
|
2009-05-17 07:44:12 -03:00
|
|
|
|
2004-08-03 13:37:40 -03:00
|
|
|
def test_suite():
|
2005-03-20 18:19:47 -04:00
|
|
|
suite = unittest.TestSuite()
|
|
|
|
suite.addTest(unittest.makeSuite(DistributionTestCase))
|
|
|
|
suite.addTest(unittest.makeSuite(MetadataTestCase))
|
|
|
|
return suite
|
2008-05-11 11:00:00 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main(defaultTest="test_suite")
|