Commit d2d11c0f authored by Éric Araujo's avatar Éric Araujo

Refactor the copying of xxmodule.c in distutils tests (#12141).

I need to copy this file in another test too, so I moved the support
code to distutils.tests.support and improved it:

- don’t skip when run from the Lib/distutils/tests directory
- use proper skip machinery instead of custom print/return/test suite
  fiddling.
parent 2659dbff
...@@ -2,12 +2,15 @@ ...@@ -2,12 +2,15 @@
import os import os
import shutil import shutil
import tempfile import tempfile
import unittest
import sysconfig
from copy import deepcopy from copy import deepcopy
from distutils import log 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
class LoggingSilencer(object): class LoggingSilencer(object):
def setUp(self): def setUp(self):
...@@ -41,6 +44,7 @@ class LoggingSilencer(object): ...@@ -41,6 +44,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.
...@@ -97,6 +101,7 @@ class TempdirManager(object): ...@@ -97,6 +101,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()."""
...@@ -107,6 +112,7 @@ class DummyCommand: ...@@ -107,6 +112,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):
...@@ -123,3 +129,39 @@ class EnvironGuard(object): ...@@ -123,3 +129,39 @@ 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
import sys import sys
import os import os
import shutil
from io import StringIO from io import StringIO
import textwrap import textwrap
from distutils.core import Distribution from distutils.core import Distribution
from distutils.command.build_ext import build_ext from distutils.command.build_ext import build_ext
from distutils import sysconfig from distutils import sysconfig
from distutils.tests.support import TempdirManager from distutils.tests.support import (TempdirManager, LoggingSilencer,
from distutils.tests.support import LoggingSilencer copy_xxmodule_c)
from distutils.extension import Extension from distutils.extension import Extension
from distutils.errors import ( from distutils.errors import (
CompileError, DistutilsPlatformError, DistutilsSetupError, CompileError, DistutilsPlatformError, DistutilsSetupError,
...@@ -16,20 +15,11 @@ from distutils.errors import ( ...@@ -16,20 +15,11 @@ from distutils.errors import (
import unittest import unittest
from test import support from test import support
from test.support import run_unittest
# http://bugs.python.org/issue4373 # http://bugs.python.org/issue4373
# 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')
return os.path.join(srcdir, 'Modules', 'xxmodule.c')
class BuildExtTestCase(TempdirManager, class BuildExtTestCase(TempdirManager,
LoggingSilencer, LoggingSilencer,
...@@ -41,9 +31,6 @@ class BuildExtTestCase(TempdirManager, ...@@ -41,9 +31,6 @@ class BuildExtTestCase(TempdirManager,
self.tmp_dir = self.mkdtemp() self.tmp_dir = self.mkdtemp()
self.sys_path = sys.path, sys.path[:] self.sys_path = sys.path, sys.path[:]
sys.path.append(self.tmp_dir) sys.path.append(self.tmp_dir)
filename = _get_source_filename()
if os.path.exists(filename):
shutil.copy(filename, self.tmp_dir)
if sys.version > "2.6": if sys.version > "2.6":
import site import site
self.old_user_base = site.USER_BASE self.old_user_base = site.USER_BASE
...@@ -72,9 +59,8 @@ class BuildExtTestCase(TempdirManager, ...@@ -72,9 +59,8 @@ class BuildExtTestCase(TempdirManager,
def test_build_ext(self): def test_build_ext(self):
global ALREADY_TESTED global ALREADY_TESTED
copy_xxmodule_c(self.tmp_dir)
xx_c = os.path.join(self.tmp_dir, 'xxmodule.c') xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
if not os.path.exists(xx_c):
return
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
...@@ -518,13 +504,7 @@ class BuildExtTestCase(TempdirManager, ...@@ -518,13 +504,7 @@ class BuildExtTestCase(TempdirManager,
def test_suite(): def test_suite():
src = _get_source_filename() return unittest.makeSuite(BuildExtTestCase)
if not os.path.exists(src):
if support.verbose:
print('test_build_ext: Cannot find source code (test'
' must run in python build dir)')
return unittest.TestSuite()
else: return unittest.makeSuite(BuildExtTestCase)
if __name__ == '__main__': if __name__ == '__main__':
support.run_unittest(test_suite()) support.run_unittest(test_suite())
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