Debian Codegraph @

Lib/test/libregrtest/worker.py

raw
1 import subprocess
2 import sys
3 import os
4 from _colorize import can_colorize # type: ignore[import-not-found]
5 from typing import Any, NoReturn
6
7 from test.support import os_helper, Py_DEBUG
8
9 from .setup import setup_process, setup_test_dir
10 from .runtests import WorkerRunTests, JsonFile, JsonFileType
11 from .single import run_single_test
12 from .utils import (
13 StrPath, StrJSON, TestFilter,
14 get_temp_dir, get_work_dir, exit_timeout)
15
16
17 USE_PROCESS_GROUP = (hasattr(os, "setsid") and hasattr(os, "killpg"))
18 NEED_TTY = {
19 'test_ioctl',
20 }
21
22
23 def create_worker_process(runtests: WorkerRunTests, output_fd: int,
24 tmp_dir: StrPath | None = None) -> subprocess.Popen[str]:
25 worker_json = runtests.as_json()
26
27 cmd = runtests.create_python_cmd()
28 cmd.extend(['-m', 'test.libregrtest.worker', worker_json])
29
30 env = dict(os.environ)
31 if tmp_dir is not None:
32 env['TMPDIR'] = tmp_dir
33 env['TEMP'] = tmp_dir
34 env['TMP'] = tmp_dir
35
36 # The subcommand is run with a temporary output which means it is not a TTY
37 # and won't auto-color. The test results are printed to stdout so if we can
38 # color that have the subprocess use color.
39 if can_colorize(file=sys.stdout):
40 env['FORCE_COLOR'] = '1'
41
42 # Running the child from the same working directory as regrtest's original
43 # invocation ensures that TEMPDIR for the child is the same when
44 # sysconfig.is_python_build() is true. See issue 15300.
45 #
46 # Emscripten and WASI Python must start in the Python source code directory
47 # to get 'python.js' or 'python.wasm' file. Then worker_process() changes
48 # to a temporary directory created to run tests.
49 work_dir = os_helper.SAVEDCWD
50
51 kwargs: dict[str, Any] = dict(
52 env=env,
53 stdout=output_fd,
54 # bpo-45410: Write stderr into stdout to keep messages order
55 stderr=output_fd,
56 text=True,
57 close_fds=True,
58 cwd=work_dir,
59 )
60
61 # Don't use setsid() in tests using TTY
62 test_name = runtests.tests[0]
63 if USE_PROCESS_GROUP and test_name not in NEED_TTY:
64 kwargs['start_new_session'] = True
65
66 # Include the test name in the TSAN log file name
67 if 'TSAN_OPTIONS' in env:
68 parts = env['TSAN_OPTIONS'].split(' ')
69 for i, part in enumerate(parts):
70 if part.startswith('log_path='):
71 parts[i] = f'{part}.{test_name}'
72 break
73 env['TSAN_OPTIONS'] = ' '.join(parts)
74
75 # Pass json_file to the worker process
76 json_file = runtests.json_file
77 json_file.configure_subprocess(kwargs)
78
79 with json_file.inherit_subprocess():
80 return subprocess.Popen(cmd, **kwargs)
81
82
83 def worker_process(worker_json: StrJSON) -> NoReturn:
84 runtests = WorkerRunTests.from_json(worker_json)
85 test_name = runtests.tests[0]
86 match_tests: TestFilter = runtests.match_tests
87 json_file: JsonFile = runtests.json_file
88
89 setup_test_dir(runtests.test_dir)
90 setup_process()
91
92 if runtests.rerun:
93 if match_tests:
94 matching = "matching: " + ", ".join(pattern for pattern, result in match_tests if result)
95 print(f"Re-running {test_name} in verbose mode ({matching})", flush=True)
96 else:
97 print(f"Re-running {test_name} in verbose mode", flush=True)
98
99 result = run_single_test(test_name, runtests)
100 if runtests.coverage:
101 if "test.cov" in sys.modules: # imported by -Xpresite=
102 result.covered_lines = list(sys.modules["test.cov"].coverage)
103 elif not Py_DEBUG:
104 print(
105 "Gathering coverage in worker processes requires --with-pydebug",
106 flush=True,
107 )
108 else:
109 raise LookupError(
110 "`test.cov` not found in sys.modules but coverage wanted"
111 )
112
113 if json_file.file_type == JsonFileType.STDOUT:
114 print()
115 result.write_json_into(sys.stdout)
116 else:
117 with json_file.open('w', encoding='utf-8') as json_fp:
118 result.write_json_into(json_fp)
119
120 sys.exit(0)
121
122
123 def main() -> NoReturn:
124 if len(sys.argv) != 2:
125 print("usage: python -m test.libregrtest.worker JSON")
126 sys.exit(1)
127 worker_json = sys.argv[1]
128
129 tmp_dir = get_temp_dir()
130 # get_temp_dir() can be different in the worker and the parent process.
131 # For example, if --tempdir option is used.
132 os.makedirs(tmp_dir, exist_ok=True)
133 work_dir = get_work_dir(tmp_dir, worker=True)
134
135 with exit_timeout():
136 with os_helper.temp_cwd(work_dir, quiet=True):
137 worker_process(worker_json)
138
139
140 if __name__ == "__main__":
141 main()