mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
gh-109162: libregrtest: add worker.py (#109229)
Add new worker.py file: * Move create_worker_process() and worker_process() to this file. * Add main() function to worker.py. create_worker_process() now runs the command: "python -m test.libregrtest.worker JSON". * create_worker_process() now starts the worker process in the current working directory. Regrtest now gets the absolute path of the reflog.txt filename: -R command line option filename. * Remove --worker-json command line option. Remove test_regrtest.test_worker_json(). Related changes: * Add write_json() and from_json() methods to TestResult. * Rename select_temp_dir() to get_temp_dir() and move it to utils. * Rename make_temp_dir() to get_work_dir() and move it to utils. It no longer calls os.makedirs(): Regrtest.main() now calls it. * Move fix_umask() to utils. The function is now called by setup_tests(). * Move StrPath to utils. * Add exit_timeout() context manager to utils. * RunTests: Replace junit_filename (StrPath) with use_junit (bool).
This commit is contained in:
parent
e55aab9578
commit
a939b65aa6
10 changed files with 238 additions and 213 deletions
93
Lib/test/libregrtest/worker.py
Normal file
93
Lib/test/libregrtest/worker.py
Normal file
|
@ -0,0 +1,93 @@
|
|||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
from typing import TextIO, NoReturn
|
||||
|
||||
from test import support
|
||||
from test.support import os_helper
|
||||
|
||||
from test.libregrtest.setup import setup_tests, setup_test_dir
|
||||
from test.libregrtest.runtest import (
|
||||
run_single_test, StrJSON, FilterTuple, RunTests)
|
||||
from test.libregrtest.utils import get_work_dir, exit_timeout, StrPath
|
||||
|
||||
|
||||
USE_PROCESS_GROUP = (hasattr(os, "setsid") and hasattr(os, "killpg"))
|
||||
|
||||
|
||||
def create_worker_process(runtests: RunTests,
|
||||
output_file: TextIO,
|
||||
tmp_dir: StrPath | None = None) -> subprocess.Popen:
|
||||
python_cmd = runtests.python_cmd
|
||||
worker_json = runtests.as_json()
|
||||
|
||||
if python_cmd is not None:
|
||||
executable = python_cmd
|
||||
else:
|
||||
executable = [sys.executable]
|
||||
cmd = [*executable, *support.args_from_interpreter_flags(),
|
||||
'-u', # Unbuffered stdout and stderr
|
||||
'-m', 'test.libregrtest.worker',
|
||||
worker_json]
|
||||
|
||||
env = dict(os.environ)
|
||||
if tmp_dir is not None:
|
||||
env['TMPDIR'] = tmp_dir
|
||||
env['TEMP'] = tmp_dir
|
||||
env['TMP'] = tmp_dir
|
||||
|
||||
# Running the child from the same working directory as regrtest's original
|
||||
# invocation ensures that TEMPDIR for the child is the same when
|
||||
# sysconfig.is_python_build() is true. See issue 15300.
|
||||
kw = dict(
|
||||
env=env,
|
||||
stdout=output_file,
|
||||
# bpo-45410: Write stderr into stdout to keep messages order
|
||||
stderr=output_file,
|
||||
text=True,
|
||||
close_fds=(os.name != 'nt'),
|
||||
)
|
||||
if USE_PROCESS_GROUP:
|
||||
kw['start_new_session'] = True
|
||||
return subprocess.Popen(cmd, **kw)
|
||||
|
||||
|
||||
def worker_process(worker_json: StrJSON) -> NoReturn:
|
||||
runtests = RunTests.from_json(worker_json)
|
||||
test_name = runtests.tests[0]
|
||||
match_tests: FilterTuple | None = runtests.match_tests
|
||||
|
||||
setup_test_dir(runtests.test_dir)
|
||||
setup_tests(runtests)
|
||||
|
||||
if runtests.rerun:
|
||||
if match_tests:
|
||||
matching = "matching: " + ", ".join(match_tests)
|
||||
print(f"Re-running {test_name} in verbose mode ({matching})", flush=True)
|
||||
else:
|
||||
print(f"Re-running {test_name} in verbose mode", flush=True)
|
||||
|
||||
result = run_single_test(test_name, runtests)
|
||||
print() # Force a newline (just in case)
|
||||
|
||||
# Serialize TestResult as dict in JSON
|
||||
result.write_json(sys.stdout)
|
||||
sys.stdout.flush()
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 2:
|
||||
print("usage: python -m test.libregrtest.worker JSON")
|
||||
sys.exit(1)
|
||||
worker_json = sys.argv[1]
|
||||
|
||||
work_dir = get_work_dir(worker=True)
|
||||
|
||||
with exit_timeout():
|
||||
with os_helper.temp_cwd(work_dir, quiet=True):
|
||||
worker_process(worker_json)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue