Issue #12333: run tests on the new module in a subprocess
It is not possible to unload a module written in C, so use a subprocess to run the tests on the module compiled by test_build_ext(). Using a subprocess, we don't have to unload the module, save/restore sys.path, and the test can be run more than once. This commit fixes also an access error on rmtree() on Windows: because the module was not really unloaded, it was not possible to remove the temporary directory (it is not possible to remove a directory on Windows if it still contains an open file).
This commit is contained in:
parent
0f270b2c37
commit
723993983a
|
@ -8,13 +8,10 @@ from packaging.dist import Distribution
|
|||
from packaging.errors import UnknownFileError, CompileError
|
||||
from packaging.command.build_ext import build_ext
|
||||
from packaging.compiler.extension import Extension
|
||||
from test.script_helper import assert_python_ok
|
||||
|
||||
from packaging.tests import support, unittest, verbose, unload
|
||||
|
||||
# http://bugs.python.org/issue4373
|
||||
# Don't load the xx module more than once.
|
||||
ALREADY_TESTED = False
|
||||
|
||||
|
||||
def _get_source_filename():
|
||||
srcdir = sysconfig.get_config_var('srcdir')
|
||||
|
@ -29,8 +26,6 @@ class BuildExtTestCase(support.TempdirManager,
|
|||
# Note that we're making changes to sys.path
|
||||
super(BuildExtTestCase, self).setUp()
|
||||
self.tmp_dir = self.mkdtemp()
|
||||
self.sys_path = sys.path, sys.path[:]
|
||||
sys.path.append(self.tmp_dir)
|
||||
filename = _get_source_filename()
|
||||
if os.path.exists(filename):
|
||||
shutil.copy(filename, self.tmp_dir)
|
||||
|
@ -58,7 +53,6 @@ class BuildExtTestCase(support.TempdirManager,
|
|||
cmd.library_dirs = value.split(os.pathsep)
|
||||
|
||||
def test_build_ext(self):
|
||||
global ALREADY_TESTED
|
||||
xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
|
||||
if not os.path.exists(xx_c):
|
||||
# skipping if we cannot find it
|
||||
|
@ -86,29 +80,27 @@ class BuildExtTestCase(support.TempdirManager,
|
|||
finally:
|
||||
sys.stdout = old_stdout
|
||||
|
||||
if ALREADY_TESTED:
|
||||
return
|
||||
else:
|
||||
ALREADY_TESTED = True
|
||||
code = """if 1:
|
||||
import sys
|
||||
sys.path.insert(0, %r)
|
||||
|
||||
import xx
|
||||
import xx
|
||||
|
||||
for attr in ('error', 'foo', 'new', 'roj'):
|
||||
self.assertTrue(hasattr(xx, attr))
|
||||
for attr in ('error', 'foo', 'new', 'roj'):
|
||||
assert hasattr(xx, attr)
|
||||
|
||||
self.assertEqual(xx.foo(2, 5), 7)
|
||||
self.assertEqual(xx.foo(13, 15), 28)
|
||||
self.assertEqual(xx.new().demo(), None)
|
||||
doc = 'This is a template module just for instruction.'
|
||||
self.assertEqual(xx.__doc__, doc)
|
||||
self.assertTrue(isinstance(xx.Null(), xx.Null))
|
||||
self.assertTrue(isinstance(xx.Str(), xx.Str))
|
||||
assert xx.foo(2, 5) == 7
|
||||
assert xx.foo(13, 15) == 28
|
||||
assert xx.new().demo() is None
|
||||
doc = 'This is a template module just for instruction.'
|
||||
assert xx.__doc__ == doc
|
||||
assert isinstance(xx.Null(), xx.Null)
|
||||
assert isinstance(xx.Str(), xx.Str)"""
|
||||
code = code % self.tmp_dir
|
||||
assert_python_ok('-c', code)
|
||||
|
||||
def tearDown(self):
|
||||
# Get everything back to normal
|
||||
unload('xx')
|
||||
sys.path = self.sys_path[0]
|
||||
sys.path[:] = self.sys_path[1]
|
||||
if sys.version > "2.6":
|
||||
site.USER_BASE = self.old_user_base
|
||||
build_ext.USER_BASE = self.old_user_base
|
||||
|
|
Loading…
Reference in New Issue