Skip to content

bpo-37421: Fix test_distutils.test_build_ext() #14564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/distutils/tests/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import unittest
import sysconfig
from copy import deepcopy
import test.support

from distutils import log
from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL
Expand Down Expand Up @@ -64,8 +65,8 @@ def tearDown(self):
os.chdir(self.old_cwd)
super().tearDown()
while self.tempdirs:
d = self.tempdirs.pop()
shutil.rmtree(d, os.name in ('nt', 'cygwin'))
tmpdir = self.tempdirs.pop()
test.support.rmtree(tmpdir)

def mkdtemp(self):
"""Create a temporary directory that will be cleaned up.
Expand Down
51 changes: 30 additions & 21 deletions Lib/distutils/tests/test_build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import unittest
from test import support
from test.support.script_helper import assert_python_ok

# http://bugs.python.org/issue4373
# Don't load the xx module more than once.
Expand All @@ -26,11 +27,8 @@ class BuildExtTestCase(TempdirManager,
unittest.TestCase):
def setUp(self):
# Create a simple test environment
# 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)
import site
self.old_user_base = site.USER_BASE
site.USER_BASE = self.mkdtemp()
Expand All @@ -40,15 +38,11 @@ def setUp(self):
# bpo-30132: On Windows, a .pdb file may be created in the current
# working directory. Create a temporary working directory to cleanup
# everything at the end of the test.
self.temp_cwd = support.temp_cwd()
self.temp_cwd.__enter__()
self.addCleanup(self.temp_cwd.__exit__, None, None, None)
change_cwd = support.change_cwd(self.tmp_dir)
change_cwd.__enter__()
self.addCleanup(change_cwd.__exit__, None, None, None)

def tearDown(self):
# Get everything back to normal
support.unload('xx')
sys.path = self.sys_path[0]
sys.path[:] = self.sys_path[1]
import site
site.USER_BASE = self.old_user_base
from distutils.command import build_ext
Expand Down Expand Up @@ -88,19 +82,34 @@ def test_build_ext(self):
else:
ALREADY_TESTED = type(self).__name__

import xx
code = textwrap.dedent(f"""
tmp_dir = {self.tmp_dir!r}

for attr in ('error', 'foo', 'new', 'roj'):
self.assertTrue(hasattr(xx, attr))
import sys
import unittest
from test import support

self.assertEqual(xx.foo(2, 5), 7)
self.assertEqual(xx.foo(13,15), 28)
self.assertEqual(xx.new().demo(), None)
if support.HAVE_DOCSTRINGS:
doc = 'This is a template module just for instruction.'
self.assertEqual(xx.__doc__, doc)
self.assertIsInstance(xx.Null(), xx.Null)
self.assertIsInstance(xx.Str(), xx.Str)
sys.path.insert(0, tmp_dir)
import xx

class Tests(unittest.TestCase):
def test_xx(self):
for attr in ('error', 'foo', 'new', 'roj'):
self.assertTrue(hasattr(xx, attr))

self.assertEqual(xx.foo(2, 5), 7)
self.assertEqual(xx.foo(13,15), 28)
self.assertEqual(xx.new().demo(), None)
if support.HAVE_DOCSTRINGS:
doc = 'This is a template module just for instruction.'
self.assertEqual(xx.__doc__, doc)
self.assertIsInstance(xx.Null(), xx.Null)
self.assertIsInstance(xx.Str(), xx.Str)


unittest.main()
""")
assert_python_ok('-c', code)

def test_solaris_enable_shared(self):
dist = Distribution({'name': 'xx'})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test_distutils.test_build_ext() is now able to remove the temporary
directory on Windows: don't import the newly built C extension ("xx") in the
current process, but test it in a separated process.