Kaydet (Commit) 60b0d31e authored tarafından Éric Araujo's avatar Éric Araujo

Refactor the copying of xxmodule.c in packaging 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 to use proper skip
machinery instead of custom print/return/test suite fiddling.

Contrary to my similar change in distutils tests, I did not add support
for finding xxmodule.c when running a test from the tests directory,
because in that case my compiler didn’t find Python.h, so I figured it’s
better to skip than to fail.
üst c9322aab
...@@ -32,6 +32,7 @@ import shutil ...@@ -32,6 +32,7 @@ import shutil
import logging import logging
import weakref import weakref
import tempfile import tempfile
import sysconfig
from packaging.dist import Distribution from packaging.dist import Distribution
from packaging.tests import unittest from packaging.tests import unittest
...@@ -39,7 +40,7 @@ from test.support import requires_zlib, unlink ...@@ -39,7 +40,7 @@ from test.support import requires_zlib, unlink
__all__ = ['LoggingCatcher', 'TempdirManager', 'EnvironRestorer', __all__ = ['LoggingCatcher', 'TempdirManager', 'EnvironRestorer',
'DummyCommand', 'unittest', 'create_distribution', 'DummyCommand', 'unittest', 'create_distribution',
'skip_unless_symlink', 'requires_zlib'] 'skip_unless_symlink', 'requires_zlib', 'copy_xxmodule_c']
logger = logging.getLogger('packaging') logger = logging.getLogger('packaging')
...@@ -271,6 +272,38 @@ def fake_dec(*args, **kw): ...@@ -271,6 +272,38 @@ def fake_dec(*args, **kw):
return _wrap return _wrap
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'),
]
for path in candidates:
if os.path.exists(path):
return path
try: try:
from test.support import skip_unless_symlink from test.support import skip_unless_symlink
except ImportError: except ImportError:
......
import os import os
import sys import sys
import site import site
import shutil
import sysconfig import sysconfig
import textwrap import textwrap
from io import StringIO from io import StringIO
...@@ -12,17 +11,7 @@ from packaging.command.build_ext import build_ext ...@@ -12,17 +11,7 @@ from packaging.command.build_ext import build_ext
from packaging.compiler.extension import Extension from packaging.compiler.extension import Extension
from test.script_helper import assert_python_ok from test.script_helper import assert_python_ok
from packaging.tests import support, unittest, verbose, unload from packaging.tests import support, unittest, verbose
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(support.TempdirManager, class BuildExtTestCase(support.TempdirManager,
...@@ -33,9 +22,6 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -33,9 +22,6 @@ class BuildExtTestCase(support.TempdirManager,
# Note that we're making changes to sys.path # Note that we're making changes to sys.path
super(BuildExtTestCase, self).setUp() super(BuildExtTestCase, self).setUp()
self.tmp_dir = self.mkdtemp() self.tmp_dir = self.mkdtemp()
filename = _get_source_filename()
if os.path.exists(filename):
shutil.copy(filename, self.tmp_dir)
self.old_user_base = site.USER_BASE self.old_user_base = site.USER_BASE
site.USER_BASE = self.mkdtemp() site.USER_BASE = self.mkdtemp()
build_ext.USER_BASE = site.USER_BASE build_ext.USER_BASE = site.USER_BASE
...@@ -68,10 +54,8 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -68,10 +54,8 @@ class BuildExtTestCase(support.TempdirManager,
cmd.library_dirs = value.split(os.pathsep) cmd.library_dirs = value.split(os.pathsep)
def test_build_ext(self): def test_build_ext(self):
support.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):
# skipping if we cannot find it
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
...@@ -455,14 +439,7 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -455,14 +439,7 @@ class BuildExtTestCase(support.TempdirManager,
def test_suite(): def test_suite():
src = _get_source_filename() return unittest.makeSuite(BuildExtTestCase)
if not os.path.exists(src):
if verbose:
print('test_command_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__':
unittest.main(defaultTest='test_suite') unittest.main(defaultTest='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