Skip to content

Commit a694e09

Browse files
bpo-30207: Rename test.test_support to test.support. (python#1353)
To simplify backports from Python 3, the test.test_support module was converted into a package and renamed to test.support. The test.script_helper module was moved into the test.support package. Names test.test_support and test.script_helper are left as aliases to test.support and test.support.script_helper.
1 parent 4734c2d commit a694e09

8 files changed

Lines changed: 1962 additions & 1945 deletions

File tree

Doc/library/test.rst

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616

1717
The :mod:`test` package contains all regression tests for Python as well as the
18-
modules :mod:`test.test_support` and :mod:`test.regrtest`.
19-
:mod:`test.test_support` is used to enhance your tests while
18+
modules :mod:`test.support` and :mod:`test.regrtest`.
19+
:mod:`test.support` is used to enhance your tests while
2020
:mod:`test.regrtest` drives the testing suite.
2121

2222
Each module in the :mod:`test` package whose name starts with ``test_`` is a
@@ -54,7 +54,7 @@ stated.
5454
A basic boilerplate is often used::
5555

5656
import unittest
57-
from test import test_support
57+
from test import support
5858

5959
class MyTestCase1(unittest.TestCase):
6060

@@ -82,10 +82,10 @@ A basic boilerplate is often used::
8282
... more test classes ...
8383

8484
def test_main():
85-
test_support.run_unittest(MyTestCase1,
86-
MyTestCase2,
87-
... list other tests ...
88-
)
85+
support.run_unittest(MyTestCase1,
86+
MyTestCase2,
87+
... list other tests ...
88+
)
8989

9090
if __name__ == '__main__':
9191
test_main()
@@ -186,18 +186,19 @@ top-level directory where Python was built. On Windows, executing
186186
tests.
187187

188188

189-
:mod:`test.test_support` --- Utility functions for tests
190-
========================================================
189+
:mod:`test.support` --- Utility functions for tests
190+
===================================================
191191

192-
.. module:: test.test_support
192+
.. module:: test.support
193193
:synopsis: Support for Python regression tests.
194194

195195
.. note::
196196

197197
The :mod:`test.test_support` module has been renamed to :mod:`test.support`
198-
in Python 3.x.
198+
in Python 3.x and 2.7.13. The name ``test.test_support`` has been retained
199+
as an alias in 2.7.
199200

200-
The :mod:`test.test_support` module provides support for Python's regression
201+
The :mod:`test.support` module provides support for Python's regression
201202
tests.
202203

203204
This module defines the following exceptions:
@@ -216,7 +217,7 @@ This module defines the following exceptions:
216217
network connection) is not available. Raised by the :func:`requires`
217218
function.
218219

219-
The :mod:`test.test_support` module defines the following constants:
220+
The :mod:`test.support` module defines the following constants:
220221

221222

222223
.. data:: verbose
@@ -241,7 +242,7 @@ The :mod:`test.test_support` module defines the following constants:
241242
Set to a name that is safe to use as the name of a temporary file. Any
242243
temporary file that is created should be closed and unlinked (removed).
243244

244-
The :mod:`test.test_support` module defines the following functions:
245+
The :mod:`test.support` module defines the following functions:
245246

246247

247248
.. function:: forget(module_name)
@@ -284,7 +285,7 @@ The :mod:`test.test_support` module defines the following functions:
284285
following :func:`test_main` function::
285286

286287
def test_main():
287-
test_support.run_unittest(__name__)
288+
support.run_unittest(__name__)
288289

289290
This will run all tests defined in the named module.
290291

@@ -433,7 +434,7 @@ The :mod:`test.test_support` module defines the following functions:
433434
.. versionadded:: 2.7
434435

435436

436-
The :mod:`test.test_support` module defines the following classes:
437+
The :mod:`test.support` module defines the following classes:
437438

438439
.. class:: TransientResource(exc[, **kwargs])
439440

Lib/test/script_helper.py

Lines changed: 1 addition & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1,170 +1 @@
1-
# Common utility functions used by various script execution tests
2-
# e.g. test_cmd_line, test_cmd_line_script and test_runpy
3-
4-
import sys
5-
import os
6-
import re
7-
import os.path
8-
import tempfile
9-
import subprocess
10-
import py_compile
11-
import contextlib
12-
import shutil
13-
try:
14-
import zipfile
15-
except ImportError:
16-
# If Python is build without Unicode support, importing _io will
17-
# fail, which, in turn, means that zipfile cannot be imported
18-
# Most of this module can then still be used.
19-
pass
20-
21-
from test.test_support import strip_python_stderr
22-
23-
# Executing the interpreter in a subprocess
24-
def _assert_python(expected_success, *args, **env_vars):
25-
cmd_line = [sys.executable]
26-
if not env_vars:
27-
cmd_line.append('-E')
28-
cmd_line.extend(args)
29-
# Need to preserve the original environment, for in-place testing of
30-
# shared library builds.
31-
env = os.environ.copy()
32-
env.update(env_vars)
33-
p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
34-
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
35-
env=env)
36-
try:
37-
out, err = p.communicate()
38-
finally:
39-
subprocess._cleanup()
40-
p.stdout.close()
41-
p.stderr.close()
42-
rc = p.returncode
43-
err = strip_python_stderr(err)
44-
if (rc and expected_success) or (not rc and not expected_success):
45-
raise AssertionError(
46-
"Process return code is %d, "
47-
"stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
48-
return rc, out, err
49-
50-
def assert_python_ok(*args, **env_vars):
51-
"""
52-
Assert that running the interpreter with `args` and optional environment
53-
variables `env_vars` is ok and return a (return code, stdout, stderr) tuple.
54-
"""
55-
return _assert_python(True, *args, **env_vars)
56-
57-
def assert_python_failure(*args, **env_vars):
58-
"""
59-
Assert that running the interpreter with `args` and optional environment
60-
variables `env_vars` fails and return a (return code, stdout, stderr) tuple.
61-
"""
62-
return _assert_python(False, *args, **env_vars)
63-
64-
def python_exit_code(*args):
65-
cmd_line = [sys.executable, '-E']
66-
cmd_line.extend(args)
67-
with open(os.devnull, 'w') as devnull:
68-
return subprocess.call(cmd_line, stdout=devnull,
69-
stderr=subprocess.STDOUT)
70-
71-
def spawn_python(*args, **kwargs):
72-
cmd_line = [sys.executable, '-E']
73-
cmd_line.extend(args)
74-
return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
75-
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
76-
**kwargs)
77-
78-
def kill_python(p):
79-
p.stdin.close()
80-
data = p.stdout.read()
81-
p.stdout.close()
82-
# try to cleanup the child so we don't appear to leak when running
83-
# with regrtest -R.
84-
p.wait()
85-
subprocess._cleanup()
86-
return data
87-
88-
def run_python(*args, **kwargs):
89-
if __debug__:
90-
p = spawn_python(*args, **kwargs)
91-
else:
92-
p = spawn_python('-O', *args, **kwargs)
93-
stdout_data = kill_python(p)
94-
return p.wait(), stdout_data
95-
96-
# Script creation utilities
97-
@contextlib.contextmanager
98-
def temp_dir():
99-
dirname = tempfile.mkdtemp()
100-
dirname = os.path.realpath(dirname)
101-
try:
102-
yield dirname
103-
finally:
104-
shutil.rmtree(dirname)
105-
106-
def make_script(script_dir, script_basename, source):
107-
script_filename = script_basename+os.extsep+'py'
108-
script_name = os.path.join(script_dir, script_filename)
109-
script_file = open(script_name, 'w')
110-
script_file.write(source)
111-
script_file.close()
112-
return script_name
113-
114-
def compile_script(script_name):
115-
py_compile.compile(script_name, doraise=True)
116-
if __debug__:
117-
compiled_name = script_name + 'c'
118-
else:
119-
compiled_name = script_name + 'o'
120-
return compiled_name
121-
122-
def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
123-
zip_filename = zip_basename+os.extsep+'zip'
124-
zip_name = os.path.join(zip_dir, zip_filename)
125-
zip_file = zipfile.ZipFile(zip_name, 'w')
126-
if name_in_zip is None:
127-
name_in_zip = os.path.basename(script_name)
128-
zip_file.write(script_name, name_in_zip)
129-
zip_file.close()
130-
#if test.test_support.verbose:
131-
# zip_file = zipfile.ZipFile(zip_name, 'r')
132-
# print 'Contents of %r:' % zip_name
133-
# zip_file.printdir()
134-
# zip_file.close()
135-
return zip_name, os.path.join(zip_name, name_in_zip)
136-
137-
def make_pkg(pkg_dir, init_source=''):
138-
os.mkdir(pkg_dir)
139-
make_script(pkg_dir, '__init__', init_source)
140-
141-
def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
142-
source, depth=1, compiled=False):
143-
unlink = []
144-
init_name = make_script(zip_dir, '__init__', '')
145-
unlink.append(init_name)
146-
init_basename = os.path.basename(init_name)
147-
script_name = make_script(zip_dir, script_basename, source)
148-
unlink.append(script_name)
149-
if compiled:
150-
init_name = compile_script(init_name)
151-
script_name = compile_script(script_name)
152-
unlink.extend((init_name, script_name))
153-
pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
154-
script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
155-
zip_filename = zip_basename+os.extsep+'zip'
156-
zip_name = os.path.join(zip_dir, zip_filename)
157-
zip_file = zipfile.ZipFile(zip_name, 'w')
158-
for name in pkg_names:
159-
init_name_in_zip = os.path.join(name, init_basename)
160-
zip_file.write(init_name, init_name_in_zip)
161-
zip_file.write(script_name, script_name_in_zip)
162-
zip_file.close()
163-
for name in unlink:
164-
os.unlink(name)
165-
#if test.test_support.verbose:
166-
# zip_file = zipfile.ZipFile(zip_name, 'r')
167-
# print 'Contents of %r:' % zip_name
168-
# zip_file.printdir()
169-
# zip_file.close()
170-
return zip_name, os.path.join(zip_name, script_name_in_zip)
1+
from test.support.script_helper import *

0 commit comments

Comments
 (0)