diff --git a/Doc/library/test.rst b/Doc/library/test.rst --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -605,3 +605,55 @@ Class used to record warnings for unit tests. See documentation of :func:`check_warnings` above for more details. + + +The :mod:`test.support.script_helper` module - common utilities for various tests +================================================================================= + +The :mod:`test.support.script_helper` defines the following functions: + +.. function:: test.support.script_helper._assert_python(expected_success, *args, **env_vars) + + Execute the interpreter in a subprocess + +.. function:: test.support.script_helper.assert_python_ok(*args, **env_vars) + + Assert that running the interpreter with `args` and optional environment + variables `env_vars` succeeds (rc == 0) and return a (return code, stdout, + stderr) tuple. + + If the __cleanenv keyword is set, env_vars is used a fresh environment. + + Python is started in isolated mode (command line option -I), + except if the __isolated keyword is set to False. + +.. function:: test.support.script_helper.assert_python_failure(*args, **env_vars) + + Assert that running the interpreter with `args` and optional environment + variables `env_vars` fails (rc != 0) and return a (return code, stdout, + stderr) tuple. + +.. function:: test.support.script_helper.spawn_python(*args, **kw) + + Run a Python subprocess with the given arguments. + +.. function:: test.support.script_helper.kill_python(p) + + Run the given Popen process until completion and return stdout. + +.. function:: test.support.script_helper.make_script(script_dir, script_basename, source) + + Create a new script based on the given parameters. + +.. function:: test.support.script_helper.make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None) + + Create a new zipped script, based on the given parameters. + +.. function:: test.support.script_helper.make_pkg(pkg_dir, init_source='') + + Create a new Python package, based on given paratemeters. + +.. function:: test.support.script_helper.make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, source, depth=1, compiled=False) + + Create a new zipped package, based on given parameters. + diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -21,7 +21,7 @@ import struct import operator import test.support -import test.script_helper +import test.support.script_helper # Skip tests if _multiprocessing wasn't built. @@ -3396,11 +3396,11 @@ sm = multiprocessing.get_start_method() name = os.path.join(os.path.dirname(__file__), 'mp_fork_bomb.py') if sm != 'fork': - rc, out, err = test.script_helper.assert_python_failure(name, sm) + rc, out, err = test.support.script_helper.assert_python_failure(name, sm) self.assertEqual('', out.decode('ascii')) self.assertIn('RuntimeError', err.decode('ascii')) else: - rc, out, err = test.script_helper.assert_python_ok(name, sm) + rc, out, err = test.support.script_helper.assert_python_ok(name, sm) self.assertEqual('123', out.decode('ascii').rstrip()) self.assertEqual('', err.decode('ascii')) diff --git a/Lib/test/script_helper.py b/Lib/test/support/script_helper.py rename from Lib/test/script_helper.py rename to Lib/test/support/script_helper.py diff --git a/Lib/test/test_calendar.py b/Lib/test/test_calendar.py --- a/Lib/test/test_calendar.py +++ b/Lib/test/test_calendar.py @@ -2,7 +2,7 @@ import unittest from test import support -from test.script_helper import assert_python_ok +from test.support.script_helper import assert_python_ok import time import locale import sys diff --git a/Lib/test/test_cgitb.py b/Lib/test/test_cgitb.py --- a/Lib/test/test_cgitb.py +++ b/Lib/test/test_cgitb.py @@ -1,5 +1,5 @@ from test.support import run_unittest -from test.script_helper import assert_python_failure, temp_dir +from test.support.script_helper import assert_python_failure, temp_dir import unittest import sys import cgitb diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -8,7 +8,7 @@ import sys import subprocess import tempfile -from test.script_helper import (spawn_python, kill_python, assert_python_ok, +from test.support.script_helper import (spawn_python, kill_python, assert_python_ok, assert_python_failure) diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py --- a/Lib/test/test_cmd_line_script.py +++ b/Lib/test/test_cmd_line_script.py @@ -11,7 +11,7 @@ import textwrap from test import support -from test.script_helper import ( +from test.support.script_helper import ( make_pkg, make_script, make_zip_pkg, make_zip_script, assert_python_ok, assert_python_failure, temp_dir, spawn_python, kill_python) diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py --- a/Lib/test/test_compileall.py +++ b/Lib/test/test_compileall.py @@ -12,7 +12,8 @@ import unittest import io -from test import support, script_helper +from test import support +from test.support import script_helper class CompileallTests(unittest.TestCase): diff --git a/Lib/test/test_concurrent_futures.py b/Lib/test/test_concurrent_futures.py --- a/Lib/test/test_concurrent_futures.py +++ b/Lib/test/test_concurrent_futures.py @@ -9,7 +9,7 @@ # without thread support. test.support.import_module('threading') -from test.script_helper import assert_python_ok +from test.support.script_helper import assert_python_ok import sys import threading diff --git a/Lib/test/test_crashers.py b/Lib/test/test_crashers.py --- a/Lib/test/test_crashers.py +++ b/Lib/test/test_crashers.py @@ -8,7 +8,7 @@ import glob import os.path import test.support -from test.script_helper import assert_python_failure +from test.support.script_helper import assert_python_failure CRASHER_DIR = os.path.join(os.path.dirname(__file__), "crashers") CRASHER_FILES = os.path.join(CRASHER_DIR, "*.py") diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -2679,7 +2679,7 @@ simple tests and no errors. We'll run both the unadorned doctest command, and the verbose version, and then check the output: - >>> from test import script_helper + >>> from test.support import script_helper >>> with script_helper.temp_dir() as tmpdir: ... fn = os.path.join(tmpdir, 'myfile.doc') ... with open(fn, 'w') as f: @@ -2730,7 +2730,7 @@ file ends in '.py', its handling of python module files (as opposed to straight text files). - >>> from test import script_helper + >>> from test.support import script_helper >>> with script_helper.temp_dir() as tmpdir: ... fn = os.path.join(tmpdir, 'myfile.doc') ... with open(fn, 'w') as f: diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py --- a/Lib/test/test_faulthandler.py +++ b/Lib/test/test_faulthandler.py @@ -6,8 +6,9 @@ import signal import subprocess import sys -from test import support, script_helper -from test.script_helper import assert_python_ok +from test import support +from test.support import script_helper +from test.support.script_helper import assert_python_ok import tempfile import unittest diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py --- a/Lib/test/test_gc.py +++ b/Lib/test/test_gc.py @@ -2,7 +2,7 @@ import unittest from test.support import (verbose, refcount_test, run_unittest, strip_python_stderr) -from test.script_helper import assert_python_ok, make_script, temp_dir +from test.support.script_helper import assert_python_ok, make_script, temp_dir import sys import time diff --git a/Lib/test/test_hash.py b/Lib/test/test_hash.py --- a/Lib/test/test_hash.py +++ b/Lib/test/test_hash.py @@ -7,7 +7,7 @@ import os import sys import unittest -from test.script_helper import assert_python_ok +from test.support.script_helper import assert_python_ok from collections import Hashable IS_64BIT = sys.maxsize > 2**32 diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -22,7 +22,7 @@ EnvironmentVarGuard, TESTFN, check_warnings, forget, is_jython, make_legacy_pyc, rmtree, run_unittest, swap_attr, swap_item, temp_umask, unlink, unload, create_empty_file, cpython_only, TESTFN_UNENCODABLE) -from test import script_helper +from test.support import script_helper skip_if_dont_write_bytecode = unittest.skipIf( diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -18,7 +18,7 @@ from test.support import run_unittest, TESTFN, DirsOnSysPath from test.support import MISSING_C_DOCSTRINGS -from test.script_helper import assert_python_ok, assert_python_failure +from test.support.script_helper import assert_python_ok, assert_python_failure from test import inspect_fodder as mod from test import inspect_fodder2 as mod2 diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -4,7 +4,7 @@ import unittest import subprocess from test import support -from test.script_helper import assert_python_ok +from test.support.script_helper import assert_python_ok class TestTool(unittest.TestCase): data = """ diff --git a/Lib/test/test_module.py b/Lib/test/test_module.py --- a/Lib/test/test_module.py +++ b/Lib/test/test_module.py @@ -2,7 +2,7 @@ import unittest import weakref from test.support import run_unittest, gc_collect -from test.script_helper import assert_python_ok +from test.support.script_helper import assert_python_ok import sys ModuleType = type(sys) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -40,7 +40,7 @@ except ImportError: fcntl = None -from test.script_helper import assert_python_ok +from test.support.script_helper import assert_python_ok with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) diff --git a/Lib/test/test_parser.py b/Lib/test/test_parser.py --- a/Lib/test/test_parser.py +++ b/Lib/test/test_parser.py @@ -4,7 +4,7 @@ import operator import struct from test import support -from test.script_helper import assert_python_failure +from test.support.script_helper import assert_python_failure # # First, we test that we can generate trees from valid source fragments, diff --git a/Lib/test/test_py_compile.py b/Lib/test/test_py_compile.py --- a/Lib/test/test_py_compile.py +++ b/Lib/test/test_py_compile.py @@ -7,7 +7,8 @@ import tempfile import unittest -from test import support, script_helper +from test import support +from test.support import script_helper class PyCompileTests(unittest.TestCase): diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -17,7 +17,7 @@ import textwrap from io import StringIO from collections import namedtuple -from test.script_helper import assert_python_ok +from test.support.script_helper import assert_python_ok from test.support import ( TESTFN, rmtree, reap_children, reap_threads, captured_output, captured_stdout, diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -10,7 +10,7 @@ from test.support import ( forget, make_legacy_pyc, run_unittest, unload, verbose, no_tracing, create_empty_file) -from test.script_helper import ( +from test.support.script_helper import ( make_pkg, make_script, make_zip_pkg, make_zip_script, temp_dir) diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -9,7 +9,7 @@ import subprocess import traceback import sys, os, time, errno -from test.script_helper import assert_python_ok, spawn_python +from test.support.script_helper import assert_python_ok, spawn_python try: import threading except ImportError: diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1,5 +1,5 @@ import unittest -from test import script_helper +from test.support import script_helper from test import support import subprocess import sys diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -664,7 +664,7 @@ def test_debugmallocstats(self): # Test sys._debugmallocstats() - from test.script_helper import assert_python_ok + from test.support.script_helper import assert_python_ok args = ['-c', 'import sys; sys._debugmallocstats()'] ret, out, err = assert_python_ok(*args) self.assertIn(b"free PyDictObjects", err) diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -7,7 +7,8 @@ import unittest import tarfile -from test import support, script_helper +from test import support +from test.support import script_helper # Check for our compression modules. try: diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -4,7 +4,7 @@ import test.support from test.support import verbose, strip_python_stderr, import_module -from test.script_helper import assert_python_ok +from test.support.script_helper import assert_python_ok import random import re @@ -16,7 +16,7 @@ import unittest import weakref import os -from test.script_helper import assert_python_ok, assert_python_failure +from test.support.script_helper import assert_python_ok, assert_python_failure import subprocess from test import lock_tests diff --git a/Lib/test/test_tools.py b/Lib/test/test_tools.py --- a/Lib/test/test_tools.py +++ b/Lib/test/test_tools.py @@ -15,7 +15,7 @@ import tempfile import textwrap from test import support -from test.script_helper import assert_python_ok, temp_dir +from test.support.script_helper import assert_python_ok, temp_dir if not sysconfig.is_python_build(): # XXX some installers do contain the tools, should we detect that diff --git a/Lib/test/test_tracemalloc.py b/Lib/test/test_tracemalloc.py --- a/Lib/test/test_tracemalloc.py +++ b/Lib/test/test_tracemalloc.py @@ -4,7 +4,7 @@ import tracemalloc import unittest from unittest.mock import patch -from test.script_helper import assert_python_ok, assert_python_failure +from test.support.script_helper import assert_python_ok, assert_python_failure from test import support try: import threading diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -6,7 +6,7 @@ import unittest import subprocess from test import support -from test.script_helper import assert_python_ok +from test.support.script_helper import assert_python_ok from test import warning_tests diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -7,7 +7,8 @@ import contextlib import copy -from test import support, script_helper +from test import support +from test.support import script_helper # Used in ReferencesTestCase.test_ref_created_during_del() . ref_from_del = None diff --git a/Lib/test/test_zipimport_support.py b/Lib/test/test_zipimport_support.py --- a/Lib/test/test_zipimport_support.py +++ b/Lib/test/test_zipimport_support.py @@ -14,8 +14,9 @@ import linecache import pdb import unittest -from test.script_helper import (spawn_python, kill_python, assert_python_ok, - temp_dir, make_script, make_zip_script) +from test.support.script_helper import (spawn_python, kill_python, + assert_python_ok,temp_dir, make_script, + make_zip_script) verbose = test.support.verbose