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().
This commit is contained in:
Victor Stinner 2020-12-15 16:08:16 +01:00 committed by GitHub
parent 6a02b38475
commit f7049b5fb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 16 deletions

View File

@ -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")

View File

@ -15,22 +15,8 @@ class EINTRTests(unittest.TestCase):
def test_all(self):
# Run the tester in a sub-process, to make sure there is only one
# thread (for reliable signal delivery).
tester = support.findfile("eintr_tester.py", subdir="eintrdata")
# use -u to try to get the full output if the test hangs or crash
args = ["-u", tester, "-v"]
if support.verbose:
print()
print("--- run eintr_tester.py ---", 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", *args]
proc = subprocess.run(args)
print(f"--- eintr_tester.py completed: "
f"exit code {proc.returncode} ---", flush=True)
if proc.returncode:
self.fail("eintr_tester.py failed")
else:
script_helper.assert_python_ok("-u", tester, "-v")
script = support.findfile("_test_eintr.py")
script_helper.run_test_script(script)
if __name__ == "__main__":