Skip to content

Commit 7d1ae8f

Browse files
committed
gh-139207: Pass regrtest worker configuration through a file
1 parent 6044c62 commit 7d1ae8f

4 files changed

Lines changed: 39 additions & 10 deletions

File tree

‎Lib/test/libregrtest/run_workers.py‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ def stop(self) -> None:
178178
self._kill()
179179

180180
def _run_process(self, runtests: WorkerRunTests, output_fd: int,
181+
worker_config: StrPath,
181182
tmp_dir: StrPath | None = None) -> int | None:
182-
popen = create_worker_process(runtests, output_fd, tmp_dir)
183+
popen = create_worker_process(runtests, output_fd, worker_config, tmp_dir)
183184
self._popen = popen
184185
self._killed = False
185186

@@ -293,7 +294,8 @@ def create_worker_runtests(self, test_name: TestName,
293294
**kwargs)
294295

295296
def run_tmp_files(self, worker_runtests: WorkerRunTests,
296-
stdout_fd: int) -> tuple[int | None, list[StrPath]]:
297+
stdout_fd: int,
298+
worker_config: StrPath) -> tuple[int | None, list[StrPath]]:
297299
# gh-93353: Check for leaked temporary files in the parent process,
298300
# since the deletion of temporary files can happen late during
299301
# Python finalization: too late for libregrtest.
@@ -305,12 +307,12 @@ def run_tmp_files(self, worker_runtests: WorkerRunTests,
305307
tmp_dir = os.path.abspath(tmp_dir)
306308
try:
307309
retcode = self._run_process(worker_runtests,
308-
stdout_fd, tmp_dir)
310+
stdout_fd, worker_config, tmp_dir)
309311
finally:
310312
tmp_files = os.listdir(tmp_dir)
311313
os_helper.rmtree(tmp_dir)
312314
else:
313-
retcode = self._run_process(worker_runtests, stdout_fd)
315+
retcode = self._run_process(worker_runtests, stdout_fd, worker_config)
314316
tmp_files = []
315317

316318
return (retcode, tmp_files)
@@ -369,9 +371,16 @@ def _runtest(self, test_name: TestName,
369371
worker_runtests = self.create_worker_runtests(
370372
test_name, json_file, module_name=module_name)
371373

374+
worker_config = stack.enter_context(tempfile.NamedTemporaryFile(
375+
'w', encoding='utf-8', delete_on_close=False))
376+
worker_config.write(worker_runtests.as_json())
377+
# Allow the worker to reopen the file on Windows.
378+
worker_config.close()
379+
372380
retcode: str | int | None
373381
retcode, tmp_files = self.run_tmp_files(worker_runtests,
374-
stdout_file.fileno())
382+
stdout_file.fileno(),
383+
worker_config.name)
375384

376385
stdout = self.read_stdout(stdout_file)
377386

‎Lib/test/libregrtest/worker.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121

2222

2323
def create_worker_process(runtests: WorkerRunTests, output_fd: int,
24+
worker_config: StrPath,
2425
tmp_dir: StrPath | None = None) -> subprocess.Popen[str]:
25-
worker_json = runtests.as_json()
26-
2726
cmd = runtests.create_python_cmd()
28-
cmd.extend(['-m', 'test.libregrtest.worker', worker_json])
27+
cmd.extend(['-m', 'test.libregrtest.worker', worker_config])
2928

3029
env = dict(os.environ)
3130
if tmp_dir is not None:
@@ -122,9 +121,10 @@ def worker_process(worker_json: StrJSON) -> NoReturn:
122121

123122
def main() -> NoReturn:
124123
if len(sys.argv) != 2:
125-
print("usage: python -m test.libregrtest.worker JSON")
124+
print("usage: python -m test.libregrtest.worker CONFIG_FILE")
126125
sys.exit(1)
127-
worker_json = sys.argv[1]
126+
with open(sys.argv[1], encoding='utf-8') as file:
127+
worker_json = file.read()
128128

129129
tmp_dir = get_temp_dir()
130130
# get_temp_dir() can be different in the worker and the parent process.

‎Lib/test/test_regrtest.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,24 @@ def test_method4(self):
16091609
subset = ['test_method1', 'test_method3']
16101610
self.assertEqual(methods, subset)
16111611

1612+
def test_large_matchfile(self):
1613+
code = textwrap.dedent("""
1614+
import unittest
1615+
1616+
class Tests(unittest.TestCase):
1617+
def test_selected(self):
1618+
pass
1619+
def test_other(self):
1620+
pass
1621+
""")
1622+
testname = self.create_test(code=code)
1623+
filename = os.path.join(self.tmptestdir, 'matches.txt')
1624+
with open(filename, 'w', encoding='utf-8') as file:
1625+
file.write(('test_missing_' + 'x' * 80 + '\n') * 2000)
1626+
file.write('test_selected\n')
1627+
output = self.run_tests('-j2', '-v', '--matchfile', filename, testname)
1628+
self.assertEqual(self.parse_methods(output), ['test_selected'])
1629+
16121630
def test_env_changed(self):
16131631
code = textwrap.dedent("""
16141632
import unittest
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Pass regrtest worker configuration through a temporary file to avoid operating
2+
system command-line size limits when using large match files.

0 commit comments

Comments
 (0)