Commit 1bd3b9ce authored by Éric Araujo's avatar Éric Araujo

Branch merge

parents 2a34eb31 cf13c866
...@@ -183,6 +183,7 @@ docs@python.org), and we'll be glad to correct the problem. ...@@ -183,6 +183,7 @@ docs@python.org), and we'll be glad to correct the problem.
* Joakim Sernbrant * Joakim Sernbrant
* Justin Sheehy * Justin Sheehy
* Charlie Shepherd * Charlie Shepherd
* Yue Shuaijie
* Michael Simcich * Michael Simcich
* Ionel Simionescu * Ionel Simionescu
* Michael Sloan * Michael Sloan
......
This diff is collapsed.
"""Support code for distutils test cases.""" """Support code for distutils test cases."""
import os import os
import sys
import shutil import shutil
import tempfile import tempfile
import unittest
import sysconfig
from copy import deepcopy from copy import deepcopy
import warnings import warnings
...@@ -9,6 +12,7 @@ from distutils import log ...@@ -9,6 +12,7 @@ from distutils import log
from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL
from distutils.core import Distribution from distutils.core import Distribution
def capture_warnings(func): def capture_warnings(func):
def _capture_warnings(*args, **kw): def _capture_warnings(*args, **kw):
with warnings.catch_warnings(): with warnings.catch_warnings():
...@@ -16,6 +20,7 @@ def capture_warnings(func): ...@@ -16,6 +20,7 @@ def capture_warnings(func):
return func(*args, **kw) return func(*args, **kw)
return _capture_warnings return _capture_warnings
class LoggingSilencer(object): class LoggingSilencer(object):
def setUp(self): def setUp(self):
...@@ -49,6 +54,7 @@ class LoggingSilencer(object): ...@@ -49,6 +54,7 @@ class LoggingSilencer(object):
def clear_logs(self): def clear_logs(self):
self.logs = [] self.logs = []
class TempdirManager(object): class TempdirManager(object):
"""Mix-in class that handles temporary directories for test cases. """Mix-in class that handles temporary directories for test cases.
...@@ -105,6 +111,7 @@ class TempdirManager(object): ...@@ -105,6 +111,7 @@ class TempdirManager(object):
return pkg_dir, dist return pkg_dir, dist
class DummyCommand: class DummyCommand:
"""Class to store options for retrieval via set_undefined_options().""" """Class to store options for retrieval via set_undefined_options()."""
...@@ -115,6 +122,7 @@ class DummyCommand: ...@@ -115,6 +122,7 @@ class DummyCommand:
def ensure_finalized(self): def ensure_finalized(self):
pass pass
class EnvironGuard(object): class EnvironGuard(object):
def setUp(self): def setUp(self):
...@@ -131,3 +139,71 @@ class EnvironGuard(object): ...@@ -131,3 +139,71 @@ class EnvironGuard(object):
del os.environ[key] del os.environ[key]
super(EnvironGuard, self).tearDown() super(EnvironGuard, self).tearDown()
def copy_xxmodule_c(directory):
"""Helper for tests that need the xxmodule.c source file.
Example use:
def test_compile(self):
copy_xxmodule_c(self.tmpdir)
self.assertIn('xxmodule.c', os.listdir(self.tmpdir))
If the source file can be found, it will be copied to *directory*. If not,
the test will be skipped. Errors during copy are not caught.
"""
filename = _get_xxmodule_path()
if filename is None:
raise unittest.SkipTest('cannot find xxmodule.c (test must run in '
'the python build dir)')
shutil.copy(filename, directory)
def _get_xxmodule_path():
srcdir = sysconfig.get_config_var('srcdir')
candidates = [
# use installed copy if available
os.path.join(os.path.dirname(__file__), 'xxmodule.c'),
# otherwise try using copy from build directory
os.path.join(srcdir, 'Modules', 'xxmodule.c'),
# srcdir mysteriously can be $srcdir/Lib/distutils/tests when
# this file is run from its parent directory, so walk up the
# tree to find the real srcdir
os.path.join(srcdir, '..', '..', '..', 'Modules', 'xxmodule.c'),
]
for path in candidates:
if os.path.exists(path):
return path
def fixup_build_ext(cmd):
"""Function needed to make build_ext tests pass.
When Python was build with --enable-shared on Unix, -L. is not good
enough to find the libpython<blah>.so. This is because regrtest runs
it under a tempdir, not in the top level where the .so lives. By the
time we've gotten here, Python's already been chdir'd to the tempdir.
When Python was built with in debug mode on Windows, build_ext commands
need their debug attribute set, and it is not done automatically for
some reason.
This function handles both of these things. Example use:
cmd = build_ext(dist)
support.fixup_build_ext(cmd)
cmd.ensure_finalized()
"""
if os.name == 'nt':
cmd.debug = sys.executable.endswith('_d.exe')
elif sysconfig.get_config_var('Py_ENABLE_SHARED'):
# To further add to the shared builds fun on Unix, we can't just add
# library_dirs to the Extension() instance because that doesn't get
# plumbed through to the final compiler command.
runshared = sysconfig.get_config_var('RUNSHARED')
if runshared is None:
cmd.library_dirs = ['.']
else:
name, equals, value = runshared.partition('=')
cmd.library_dirs = value.split(os.pathsep)
import sys import sys
import os import os
import tempfile
import shutil
from StringIO import StringIO from StringIO import StringIO
import textwrap import textwrap
...@@ -19,76 +17,40 @@ from test import test_support ...@@ -19,76 +17,40 @@ from test import test_support
# Don't load the xx module more than once. # Don't load the xx module more than once.
ALREADY_TESTED = False ALREADY_TESTED = False
def _get_source_filename():
# use installed copy if available
tests_f = os.path.join(os.path.dirname(__file__), 'xxmodule.c')
if os.path.exists(tests_f):
return tests_f
# otherwise try using copy from build directory
srcdir = sysconfig.get_config_var('srcdir')
if srcdir is None:
return os.path.join(sysconfig.project_base, 'Modules', 'xxmodule.c')
return os.path.join(srcdir, 'Modules', 'xxmodule.c')
_XX_MODULE_PATH = _get_source_filename()
class BuildExtTestCase(support.TempdirManager, class BuildExtTestCase(support.TempdirManager,
support.LoggingSilencer, support.LoggingSilencer,
unittest.TestCase): unittest.TestCase):
def setUp(self): def setUp(self):
# Create a simple test environment
# Note that we're making changes to sys.path
super(BuildExtTestCase, self).setUp() super(BuildExtTestCase, self).setUp()
self.tmp_dir = tempfile.mkdtemp(prefix="pythontest_") self.tmp_dir = self.mkdtemp()
if os.path.exists(_XX_MODULE_PATH): self.xx_created = False
self.sys_path = sys.path[:] sys.path.append(self.tmp_dir)
sys.path.append(self.tmp_dir) self.addCleanup(sys.path.remove, self.tmp_dir)
shutil.copy(_XX_MODULE_PATH, self.tmp_dir) if sys.version > "2.6":
import site
self.old_user_base = site.USER_BASE
site.USER_BASE = self.mkdtemp()
from distutils.command import build_ext
build_ext.USER_BASE = site.USER_BASE
def tearDown(self): def tearDown(self):
# Get everything back to normal if self.xx_created:
if os.path.exists(_XX_MODULE_PATH):
test_support.unload('xx') test_support.unload('xx')
sys.path[:] = self.sys_path
# XXX on Windows the test leaves a directory # XXX on Windows the test leaves a directory
# with xx module in TEMP # with xx module in TEMP
shutil.rmtree(self.tmp_dir, os.name == 'nt' or
sys.platform == 'cygwin')
super(BuildExtTestCase, self).tearDown() super(BuildExtTestCase, self).tearDown()
def _fixup_command(self, cmd):
# When Python was build with --enable-shared, -L. is not good enough
# to find the libpython<blah>.so. This is because regrtest runs it
# under a tempdir, not in the top level where the .so lives. By the
# time we've gotten here, Python's already been chdir'd to the
# tempdir.
#
# To further add to the fun, we can't just add library_dirs to the
# Extension() instance because that doesn't get plumbed through to the
# final compiler command.
if (sysconfig.get_config_var('Py_ENABLE_SHARED') and
not sys.platform.startswith('win')):
runshared = sysconfig.get_config_var('RUNSHARED')
if runshared is None:
cmd.library_dirs = ['.']
else:
name, equals, value = runshared.partition('=')
cmd.library_dirs = value.split(os.pathsep)
@unittest.skipIf(not os.path.exists(_XX_MODULE_PATH),
'xxmodule.c not found')
def test_build_ext(self): def test_build_ext(self):
global ALREADY_TESTED global ALREADY_TESTED
support.copy_xxmodule_c(self.tmp_dir)
self.xx_created = True
xx_c = os.path.join(self.tmp_dir, 'xxmodule.c') xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
xx_ext = Extension('xx', [xx_c]) xx_ext = Extension('xx', [xx_c])
dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]}) dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
dist.package_dir = self.tmp_dir dist.package_dir = self.tmp_dir
cmd = build_ext(dist) cmd = build_ext(dist)
self._fixup_command(cmd) support.fixup_build_ext(cmd)
if os.name == "nt":
# On Windows, we must build a debug version iff running
# a debug build of Python
cmd.debug = sys.executable.endswith("_d.exe")
cmd.build_lib = self.tmp_dir cmd.build_lib = self.tmp_dir
cmd.build_temp = self.tmp_dir cmd.build_temp = self.tmp_dir
...@@ -141,6 +103,36 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -141,6 +103,36 @@ class BuildExtTestCase(support.TempdirManager,
# make sure we get some library dirs under solaris # make sure we get some library dirs under solaris
self.assertTrue(len(cmd.library_dirs) > 0) self.assertTrue(len(cmd.library_dirs) > 0)
def test_user_site(self):
# site.USER_SITE was introduced in 2.6
if sys.version < '2.6':
return
import site
dist = Distribution({'name': 'xx'})
cmd = build_ext(dist)
# making sure the user option is there
options = [name for name, short, label in
cmd.user_options]
self.assertIn('user', options)
# setting a value
cmd.user = 1
# setting user based lib and include
lib = os.path.join(site.USER_BASE, 'lib')
incl = os.path.join(site.USER_BASE, 'include')
os.mkdir(lib)
os.mkdir(incl)
cmd.ensure_finalized()
# see if include_dirs and library_dirs were set
self.assertIn(lib, cmd.library_dirs)
self.assertIn(lib, cmd.rpath)
self.assertIn(incl, cmd.include_dirs)
def test_finalize_options(self): def test_finalize_options(self):
# Make sure Python's include directories (for Python.h, pyconfig.h, # Make sure Python's include directories (for Python.h, pyconfig.h,
# etc.) are in the include search path. # etc.) are in the include search path.
...@@ -149,7 +141,6 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -149,7 +141,6 @@ class BuildExtTestCase(support.TempdirManager,
cmd = build_ext(dist) cmd = build_ext(dist)
cmd.finalize_options() cmd.finalize_options()
from distutils import sysconfig
py_include = sysconfig.get_python_inc() py_include = sysconfig.get_python_inc()
self.assertTrue(py_include in cmd.include_dirs) self.assertTrue(py_include in cmd.include_dirs)
...@@ -277,13 +268,10 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -277,13 +268,10 @@ class BuildExtTestCase(support.TempdirManager,
dist = Distribution({'name': 'xx', dist = Distribution({'name': 'xx',
'ext_modules': [ext]}) 'ext_modules': [ext]})
cmd = build_ext(dist) cmd = build_ext(dist)
self._fixup_command(cmd) support.fixup_build_ext(cmd)
cmd.ensure_finalized() cmd.ensure_finalized()
self.assertEqual(len(cmd.get_outputs()), 1) self.assertEqual(len(cmd.get_outputs()), 1)
if os.name == "nt":
cmd.debug = sys.executable.endswith("_d.exe")
cmd.build_lib = os.path.join(self.tmp_dir, 'build') cmd.build_lib = os.path.join(self.tmp_dir, 'build')
cmd.build_temp = os.path.join(self.tmp_dir, 'tempt') cmd.build_temp = os.path.join(self.tmp_dir, 'tempt')
...@@ -509,10 +497,8 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -509,10 +497,8 @@ class BuildExtTestCase(support.TempdirManager,
cmd.build_temp = self.tmp_dir cmd.build_temp = self.tmp_dir
try: try:
old_stdout = sys.stdout
cmd.ensure_finalized() cmd.ensure_finalized()
cmd.run() cmd.run()
except CompileError: except CompileError:
self.fail("Wrong deployment target during compilation") self.fail("Wrong deployment target during compilation")
......
"""Tests for distutils.command.install.""" """Tests for distutils.command.install."""
import os import os
import sys
import unittest import unittest
import site
from test.test_support import run_unittest from test.test_support import captured_stdout, run_unittest
from distutils import sysconfig
from distutils.command.install import install from distutils.command.install import install
from distutils.command import install as install_module
from distutils.command.build_ext import build_ext
from distutils.command.install import INSTALL_SCHEMES
from distutils.core import Distribution from distutils.core import Distribution
from distutils.errors import DistutilsOptionError
from distutils.extension import Extension
from distutils.tests import support from distutils.tests import support
class InstallTestCase(support.TempdirManager, unittest.TestCase): def _make_ext_name(modname):
if os.name == 'nt' and sys.executable.endswith('_d.exe'):
modname += '_d'
return modname + sysconfig.get_config_var('SO')
class InstallTestCase(support.TempdirManager,
support.LoggingSilencer,
unittest.TestCase):
def test_home_installation_scheme(self): def test_home_installation_scheme(self):
# This ensure two things: # This ensure two things:
...@@ -49,6 +65,181 @@ class InstallTestCase(support.TempdirManager, unittest.TestCase): ...@@ -49,6 +65,181 @@ class InstallTestCase(support.TempdirManager, unittest.TestCase):
check_path(cmd.install_scripts, os.path.join(destination, "bin")) check_path(cmd.install_scripts, os.path.join(destination, "bin"))
check_path(cmd.install_data, destination) check_path(cmd.install_data, destination)
def test_user_site(self):
# site.USER_SITE was introduced in 2.6
if sys.version < '2.6':
return
# preparing the environment for the test
self.old_user_base = site.USER_BASE
self.old_user_site = site.USER_SITE
self.tmpdir = self.mkdtemp()
self.user_base = os.path.join(self.tmpdir, 'B')
self.user_site = os.path.join(self.tmpdir, 'S')
site.USER_BASE = self.user_base
site.USER_SITE = self.user_site
install_module.USER_BASE = self.user_base
install_module.USER_SITE = self.user_site
def _expanduser(path):
return self.tmpdir
self.old_expand = os.path.expanduser
os.path.expanduser = _expanduser
try:
# this is the actual test
self._test_user_site()
finally:
site.USER_BASE = self.old_user_base
site.USER_SITE = self.old_user_site
install_module.USER_BASE = self.old_user_base
install_module.USER_SITE = self.old_user_site
os.path.expanduser = self.old_expand
def _test_user_site(self):
for key in ('nt_user', 'unix_user', 'os2_home'):
self.assertTrue(key in INSTALL_SCHEMES)
dist = Distribution({'name': 'xx'})
cmd = install(dist)
# making sure the user option is there
options = [name for name, short, lable in
cmd.user_options]
self.assertTrue('user' in options)
# setting a value
cmd.user = 1
# user base and site shouldn't be created yet
self.assertTrue(not os.path.exists(self.user_base))
self.assertTrue(not os.path.exists(self.user_site))
# let's run finalize
cmd.ensure_finalized()
# now they should
self.assertTrue(os.path.exists(self.user_base))
self.assertTrue(os.path.exists(self.user_site))
self.assertTrue('userbase' in cmd.config_vars)
self.assertTrue('usersite' in cmd.config_vars)
def test_handle_extra_path(self):
dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'})
cmd = install(dist)
# two elements
cmd.handle_extra_path()
self.assertEqual(cmd.extra_path, ['path', 'dirs'])
self.assertEqual(cmd.extra_dirs, 'dirs')
self.assertEqual(cmd.path_file, 'path')
# one element
cmd.extra_path = ['path']
cmd.handle_extra_path()
self.assertEqual(cmd.extra_path, ['path'])
self.assertEqual(cmd.extra_dirs, 'path')
self.assertEqual(cmd.path_file, 'path')
# none
dist.extra_path = cmd.extra_path = None
cmd.handle_extra_path()
self.assertEqual(cmd.extra_path, None)
self.assertEqual(cmd.extra_dirs, '')
self.assertEqual(cmd.path_file, None)
# three elements (no way !)
cmd.extra_path = 'path,dirs,again'
self.assertRaises(DistutilsOptionError, cmd.handle_extra_path)
def test_finalize_options(self):
dist = Distribution({'name': 'xx'})
cmd = install(dist)
# must supply either prefix/exec-prefix/home or
# install-base/install-platbase -- not both
cmd.prefix = 'prefix'
cmd.install_base = 'base'
self.assertRaises(DistutilsOptionError, cmd.finalize_options)
# must supply either home or prefix/exec-prefix -- not both
cmd.install_base = None
cmd.home = 'home'
self.assertRaises(DistutilsOptionError, cmd.finalize_options)
# can't combine user with with prefix/exec_prefix/home or
# install_(plat)base
cmd.prefix = None
cmd.user = 'user'
self.assertRaises(DistutilsOptionError, cmd.finalize_options)
def test_record(self):
install_dir = self.mkdtemp()
project_dir, dist = self.create_dist(scripts=['hello'])
self.addCleanup(os.chdir, os.getcwd())
os.chdir(project_dir)
self.write_file('hello', "print('o hai')")
cmd = install(dist)
dist.command_obj['install'] = cmd
cmd.root = install_dir
cmd.record = os.path.join(project_dir, 'RECORD')
cmd.ensure_finalized()
cmd.run()
f = open(cmd.record)
try:
content = f.read()
finally:
f.close()
found = [os.path.basename(line) for line in content.splitlines()]
expected = ['hello',
'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
self.assertEqual(found, expected)
def test_record_extensions(self):
install_dir = self.mkdtemp()
project_dir, dist = self.create_dist(ext_modules=[
Extension('xx', ['xxmodule.c'])])
self.addCleanup(os.chdir, os.getcwd())
os.chdir(project_dir)
support.copy_xxmodule_c(project_dir)
buildextcmd = build_ext(dist)
support.fixup_build_ext(buildextcmd)
buildextcmd.ensure_finalized()
cmd = install(dist)
dist.command_obj['install'] = cmd
dist.command_obj['build_ext'] = buildextcmd
cmd.root = install_dir
cmd.record = os.path.join(project_dir, 'RECORD')
cmd.ensure_finalized()
cmd.run()
f = open(cmd.record)
try:
content = f.read()
finally:
f.close()
found = [os.path.basename(line) for line in content.splitlines()]
expected = [_make_ext_name('xx'),
'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
self.assertEqual(found, expected)
def test_debug_mode(self):
# this covers the code called when DEBUG is set
old_logs_len = len(self.logs)
install_module.DEBUG = True
try:
with captured_stdout():
self.test_record()
finally:
install_module.DEBUG = False
self.assertTrue(len(self.logs) > old_logs_len)
def test_suite(): def test_suite():
return unittest.makeSuite(InstallTestCase) return unittest.makeSuite(InstallTestCase)
......
...@@ -423,6 +423,7 @@ class SDistTestCase(PyPIRCCommandTestCase): ...@@ -423,6 +423,7 @@ class SDistTestCase(PyPIRCCommandTestCase):
def test_manual_manifest(self): def test_manual_manifest(self):
# check that a MANIFEST without a marker is left alone # check that a MANIFEST without a marker is left alone
dist, cmd = self.get_cmd() dist, cmd = self.get_cmd()
cmd.formats = ['gztar']
cmd.ensure_finalized() cmd.ensure_finalized()
self.write_file((self.tmp_dir, cmd.manifest), 'README.manual') self.write_file((self.tmp_dir, cmd.manifest), 'README.manual')
self.write_file((self.tmp_dir, 'README.manual'), self.write_file((self.tmp_dir, 'README.manual'),
......
...@@ -25,7 +25,8 @@ except ImportError: ...@@ -25,7 +25,8 @@ except ImportError:
__all__ = ["copyfileobj", "copyfile", "copymode", "copystat", "copy", "copy2", __all__ = ["copyfileobj", "copyfile", "copymode", "copystat", "copy", "copy2",
"copytree", "move", "rmtree", "Error", "SpecialFileError", "copytree", "move", "rmtree", "Error", "SpecialFileError",
"ExecError", "make_archive", "get_archive_formats", "ExecError", "make_archive", "get_archive_formats",
"register_archive_format", "unregister_archive_format"] "register_archive_format", "unregister_archive_format",
"ignore_patterns"]
class Error(EnvironmentError): class Error(EnvironmentError):
pass pass
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment