diff options
author | Victor Stinner <vstinner@python.org> | 2020-12-15 15:08:16 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-15 15:08:16 (GMT) |
commit | f7049b5fb680c774e4950d10be62859a749f4e79 (patch) | |
tree | 8ec3339405abb0b50e4135fee7b93afa8e7c78a6 /Lib/test/support | |
parent | 6a02b384751dbc13979efc1185f0a7c1670dc349 (diff) | |
download | cpython-f7049b5fb680c774e4950d10be62859a749f4e79.zip cpython-f7049b5fb680c774e4950d10be62859a749f4e79.tar.gz cpython-f7049b5fb680c774e4950d10be62859a749f4e79.tar.bz2 |
bpo-42639: Add script_helper.run_test_script() (GH-23777)
* Add run_test_script() function to test.support.script_helper.
* Rename Lib/test/eintrdata/eintr_tester.py to
Lib/test/_test_eintr.py.
* test_eintr.py uses run_test_script().
Diffstat (limited to 'Lib/test/support')
-rw-r--r-- | Lib/test/support/script_helper.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Lib/test/support/script_helper.py b/Lib/test/support/script_helper.py index 09bb586..6d699c8 100644 --- a/Lib/test/support/script_helper.py +++ b/Lib/test/support/script_helper.py @@ -11,12 +11,14 @@ import py_compile import zipfile from importlib.util import source_from_cache +from test import support from test.support.import_helper import make_legacy_pyc # Cached result of the expensive test performed in the function below. __cached_interp_requires_environment = None + def interpreter_requires_environment(): """ Returns True if our sys.executable interpreter requires environment @@ -136,12 +138,14 @@ def run_python_until_end(*args, **env_vars): rc = proc.returncode return _PythonRunResult(rc, out, err), cmd_line + def _assert_python(expected_success, /, *args, **env_vars): res, cmd_line = run_python_until_end(*args, **env_vars) if (res.rc and expected_success) or (not res.rc and not expected_success): res.fail(cmd_line) return res + def assert_python_ok(*args, **env_vars): """ Assert that running the interpreter with `args` and optional environment @@ -155,6 +159,7 @@ def assert_python_ok(*args, **env_vars): """ return _assert_python(True, *args, **env_vars) + def assert_python_failure(*args, **env_vars): """ Assert that running the interpreter with `args` and optional environment @@ -165,6 +170,7 @@ def assert_python_failure(*args, **env_vars): """ return _assert_python(False, *args, **env_vars) + def spawn_python(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw): """Run a Python subprocess with the given arguments. @@ -187,6 +193,7 @@ def spawn_python(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw): stdout=stdout, stderr=stderr, **kw) + def kill_python(p): """Run the given Popen process until completion and return stdout.""" p.stdin.close() @@ -198,6 +205,7 @@ def kill_python(p): subprocess._cleanup() return data + def make_script(script_dir, script_basename, source, omit_suffix=False): script_filename = script_basename if not omit_suffix: @@ -209,6 +217,7 @@ def make_script(script_dir, script_basename, source, omit_suffix=False): importlib.invalidate_caches() return script_name + def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None): zip_filename = zip_basename+os.extsep+'zip' zip_name = os.path.join(zip_dir, zip_filename) @@ -228,10 +237,12 @@ def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None): # zip_file.printdir() return zip_name, os.path.join(zip_name, name_in_zip) + def make_pkg(pkg_dir, init_source=''): os.mkdir(pkg_dir) make_script(pkg_dir, '__init__', init_source) + def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, source, depth=1, compiled=False): unlink = [] @@ -260,3 +271,24 @@ def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, # print 'Contents of %r:' % zip_name # zip_file.printdir() return zip_name, os.path.join(zip_name, script_name_in_zip) + + +def run_test_script(script): + # use -u to try to get the full output if the test hangs or crash + if support.verbose: + def title(text): + return f"===== {text} ======" + + name = f"script {os.path.basename(script)}" + print() + print(title(name), flush=True) + # In verbose mode, the child process inherit stdout and stdout, + # to see output in realtime and reduce the risk of losing output. + args = [sys.executable, "-E", "-X", "faulthandler", "-u", script, "-v"] + proc = subprocess.run(args) + print(title(f"{name} completed: exit code {proc.returncode}"), + flush=True) + if proc.returncode: + raise AssertionError(f"{name} failed") + else: + assert_python_ok("-u", script, "-v") |