Skip to content

Commit 4f85cec

Browse files
authored
gh-93353: regrtest supports checking tmp files with -j2 (#93909)
regrtest now also implements checking for leaked temporary files and directories when using -jN for N >= 2. Use tempfile.mkdtemp() to create the temporary directory. Skip this check on WASI.
1 parent 138db8e commit 4f85cec

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

Lib/test/libregrtest/runtest_mp.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import signal
77
import subprocess
88
import sys
9+
import tempfile
910
import threading
1011
import time
1112
import traceback
@@ -273,14 +274,16 @@ def _run_process(self, test_name: str, tmp_dir: str) -> tuple[int, str, str]:
273274
self.current_test_name = None
274275

275276
def _runtest(self, test_name: str) -> MultiprocessResult:
276-
if self.ns.use_mp == 1:
277+
# Don't check for leaked temporary files and directories if Python is
278+
# run on WASI. WASI don't pass environment variables like TMPDIR to
279+
# worker processes.
280+
if not support.is_wasi:
277281
# gh-93353: Check for leaked temporary files in the parent process,
278282
# since the deletion of temporary files can happen late during
279283
# Python finalization: too late for libregrtest.
280-
tmp_dir = os.getcwd() + '_tmpdir'
284+
tmp_dir = tempfile.mkdtemp(prefix="test_python_")
281285
tmp_dir = os.path.abspath(tmp_dir)
282286
try:
283-
os.mkdir(tmp_dir)
284287
retcode, stdout = self._run_process(test_name, tmp_dir)
285288
finally:
286289
tmp_files = os.listdir(tmp_dir)

Lib/test/test_regrtest.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,8 @@ def test_cleanup(self):
13571357
for name in names:
13581358
self.assertFalse(os.path.exists(name), name)
13591359

1360+
@unittest.skipIf(support.is_wasi,
1361+
'checking temp files is not implemented on WASI')
13601362
def test_leak_tmp_file(self):
13611363
code = textwrap.dedent(r"""
13621364
import os.path
@@ -1369,15 +1371,17 @@ def test_leak_tmp_file(self):
13691371
with open(filename, "wb") as fp:
13701372
fp.write(b'content')
13711373
""")
1372-
testname = self.create_test(code=code)
1374+
testnames = [self.create_test(code=code) for _ in range(3)]
13731375

1374-
output = self.run_tests("--fail-env-changed", "-v", "-j1", testname, exitcode=3)
1375-
self.check_executed_tests(output, [testname],
1376-
env_changed=[testname],
1377-
fail_env_changed=True)
1378-
self.assertIn(f"Warning -- {testname} leaked temporary "
1379-
f"files (1): mytmpfile",
1380-
output)
1376+
output = self.run_tests("--fail-env-changed", "-v", "-j2", *testnames, exitcode=3)
1377+
self.check_executed_tests(output, testnames,
1378+
env_changed=testnames,
1379+
fail_env_changed=True,
1380+
randomize=True)
1381+
for testname in testnames:
1382+
self.assertIn(f"Warning -- {testname} leaked temporary "
1383+
f"files (1): mytmpfile",
1384+
output)
13811385

13821386

13831387
class TestUtils(unittest.TestCase):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
regrtest now checks if a test leaks temporary files or directories if run
2+
with -jN option. Patch by Victor Stinner.

0 commit comments

Comments
 (0)