summaryrefslogtreecommitdiffstats
path: root/Lib/packaging/tests/support.py
diff options
context:
space:
mode:
authorÉric Araujo <merwok@netwok.org>2011-08-20 05:27:47 (GMT)
committerÉric Araujo <merwok@netwok.org>2011-08-20 05:27:47 (GMT)
commit60b0d31e35238275ef9f72ca1be71e4efae88fc2 (patch)
tree4357a67281363b37145b429cbf9bc136c4e3170f /Lib/packaging/tests/support.py
parentc9322aabaa32df09af0bfcd317d1a860fb47a675 (diff)
downloadcpython-60b0d31e35238275ef9f72ca1be71e4efae88fc2.zip
cpython-60b0d31e35238275ef9f72ca1be71e4efae88fc2.tar.gz
cpython-60b0d31e35238275ef9f72ca1be71e4efae88fc2.tar.bz2
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.
Diffstat (limited to 'Lib/packaging/tests/support.py')
-rw-r--r--Lib/packaging/tests/support.py35
1 files changed, 34 insertions, 1 deletions
diff --git a/Lib/packaging/tests/support.py b/Lib/packaging/tests/support.py
index c6e3f72..a9535ab 100644
--- a/Lib/packaging/tests/support.py
+++ b/Lib/packaging/tests/support.py
@@ -32,6 +32,7 @@ import shutil
import logging
import weakref
import tempfile
+import sysconfig
from packaging.dist import Distribution
from packaging.tests import unittest
@@ -39,7 +40,7 @@ from test.support import requires_zlib, unlink
__all__ = ['LoggingCatcher', 'TempdirManager', 'EnvironRestorer',
'DummyCommand', 'unittest', 'create_distribution',
- 'skip_unless_symlink', 'requires_zlib']
+ 'skip_unless_symlink', 'requires_zlib', 'copy_xxmodule_c']
logger = logging.getLogger('packaging')
@@ -271,6 +272,38 @@ def fake_dec(*args, **kw):
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:
from test.support import skip_unless_symlink
except ImportError: