changeset: 100555:18b28fd8f677 tag: tip user: Victor Stinner date: Tue Mar 15 23:21:00 2016 +0100 files: Lib/test/__init__.py Lib/test/libregrtest/setup.py Lib/test/test_regrtest.py Lib/test/test_zipfile.py description: Issue #26295: test_regrtest now creates tests in a temporary directory. Changes: * libregrtest.setup_tests() now adds the parent directory of testdir to sys.path when the --testdir option is used. * Fix test_zipfile to not use test.__file__ since test will become a package. diff -r 96b73b649b15 -r 18b28fd8f677 Lib/test/__init__.py --- a/Lib/test/__init__.py Tue Mar 15 23:08:44 2016 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -# Dummy file to make this directory a package. diff -r 96b73b649b15 -r 18b28fd8f677 Lib/test/libregrtest/setup.py --- a/Lib/test/libregrtest/setup.py Tue Mar 15 23:08:44 2016 +0100 +++ b/Lib/test/libregrtest/setup.py Tue Mar 15 23:21:00 2016 +0100 @@ -29,6 +29,9 @@ def setup_tests(ns): replace_stdout() support.record_original_stdout(sys.stdout) + if ns.testdir: + sys.path.insert(0, os.path.dirname(ns.testdir)) + # Some times __path__ and __file__ are not absolute (e.g. while running from # Lib/) and, if we change the CWD to run the tests in a temporary dir, some # imports might fail. This affects only the modules imported before os.chdir(). diff -r 96b73b649b15 -r 18b28fd8f677 Lib/test/test_regrtest.py --- a/Lib/test/test_regrtest.py Tue Mar 15 23:08:44 2016 +0100 +++ b/Lib/test/test_regrtest.py Tue Mar 15 23:21:00 2016 +0100 @@ -15,6 +15,7 @@ import re import subprocess import sys import sysconfig +import tempfile import textwrap import unittest from test import libregrtest @@ -306,9 +307,18 @@ class BaseTestCase(unittest.TestCase): TESTNAME_PREFIX = 'test_regrtest_' TESTNAME_REGEX = r'test_[a-z0-9_]+' + def create_testdir(self): + self.tmpdir = tempfile.mkdtemp() + self.addCleanup(support.rmtree, self.tmpdir) + + self.tmptestdir = os.path.join(self.tmpdir, 'test') + os.mkdir(self.tmptestdir) + def setUp(self): self.testdir = os.path.realpath(os.path.dirname(__file__)) + self.create_testdir() + # When test_regrtest is interrupted by CTRL+c, it can leave # temporary test files remove = [entry.path @@ -327,7 +337,7 @@ class BaseTestCase(unittest.TestCase): # test_regrtest cannot be run twice in parallel because # of setUp() and create_test() name = self.TESTNAME_PREFIX + "%s_%s" % (os.getpid(), name) - path = os.path.join(self.testdir, name + '.py') + path = os.path.join(self.tmptestdir, name + '.py') self.addCleanup(support.unlink, path) # Use 'x' mode to ensure that we do not override existing tests @@ -461,7 +471,7 @@ class ProgramsTestCase(BaseTestCase): self.tests = [self.create_test() for index in range(self.NTEST)] self.python_args = ['-Wd', '-E', '-bb'] - self.regrtest_args = ['-uall', '-rwW'] + self.regrtest_args = ['-uall', '-rwW', '--testdir=%s' % self.tmptestdir] if hasattr(faulthandler, 'dump_traceback_later'): self.regrtest_args.extend(('--timeout', '3600', '-j4')) if sys.platform == 'win32': @@ -518,7 +528,8 @@ class ProgramsTestCase(BaseTestCase): def test_tools_script_run_tests(self): # Tools/scripts/run_tests.py script = os.path.join(ROOT_DIR, 'Tools', 'scripts', 'run_tests.py') - self.run_tests([script, *self.tests]) + args = [script, '--testdir=%s' % self.tmptestdir, *self.tests] + self.run_tests(args) def run_batch(self, *args): proc = self.run_command(args) @@ -554,8 +565,9 @@ class ArgsTestCase(BaseTestCase): Test arguments of the Python test suite. """ - def run_tests(self, *args, **kw): - return self.run_python(['-m', 'test', *args], **kw) + def run_tests(self, *testargs, **kw): + cmdargs = ['-m', 'test', '--testdir=%s' % self.tmptestdir, *testargs] + return self.run_python(cmdargs, **kw) def test_failing_test(self): # test a failing test diff -r 96b73b649b15 -r 18b28fd8f677 Lib/test/test_zipfile.py --- a/Lib/test/test_zipfile.py Tue Mar 15 23:08:44 2016 +0100 +++ b/Lib/test/test_zipfile.py Tue Mar 15 23:21:00 2016 +0100 @@ -739,7 +739,7 @@ class PyZipFileTests(unittest.TestCase): def test_write_filtered_python_package(self): import test - packagedir = os.path.dirname(test.__file__) + packagedir = os.path.dirname(__file__) self.requiresWriteAccess(packagedir) with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp: