[Python-checkins] gh-93353: regrtest supports checking tmp files with -j2 (#93909)

vstinner webhook-mailer at python.org
Thu Jun 16 15:48:35 EDT 2022


https://github.com/python/cpython/commit/4f85cec9e2077681b3dacc3108e646d509b720bf
commit: 4f85cec9e2077681b3dacc3108e646d509b720bf
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-06-16T21:48:26+02:00
summary:

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.

files:
A Misc/NEWS.d/next/Tests/2022-06-16-17-50-58.gh-issue-93353.JdpATx.rst
M Lib/test/libregrtest/runtest_mp.py
M Lib/test/test_regrtest.py

diff --git a/Lib/test/libregrtest/runtest_mp.py b/Lib/test/libregrtest/runtest_mp.py
index a901b582f27da..71ababd19b876 100644
--- a/Lib/test/libregrtest/runtest_mp.py
+++ b/Lib/test/libregrtest/runtest_mp.py
@@ -6,6 +6,7 @@
 import signal
 import subprocess
 import sys
+import tempfile
 import threading
 import time
 import traceback
@@ -273,14 +274,16 @@ def _run_process(self, test_name: str, tmp_dir: str) -> tuple[int, str, str]:
             self.current_test_name = None
 
     def _runtest(self, test_name: str) -> MultiprocessResult:
-        if self.ns.use_mp == 1:
+        # Don't check for leaked temporary files and directories if Python is
+        # run on WASI. WASI don't pass environment variables like TMPDIR to
+        # worker processes.
+        if not support.is_wasi:
             # gh-93353: Check for leaked temporary files in the parent process,
             # since the deletion of temporary files can happen late during
             # Python finalization: too late for libregrtest.
-            tmp_dir = os.getcwd() + '_tmpdir'
+            tmp_dir = tempfile.mkdtemp(prefix="test_python_")
             tmp_dir = os.path.abspath(tmp_dir)
             try:
-                os.mkdir(tmp_dir)
                 retcode, stdout = self._run_process(test_name, tmp_dir)
             finally:
                 tmp_files = os.listdir(tmp_dir)
diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py
index 93c0cae1473a0..a36d18488a5ef 100644
--- a/Lib/test/test_regrtest.py
+++ b/Lib/test/test_regrtest.py
@@ -1357,6 +1357,8 @@ def test_cleanup(self):
         for name in names:
             self.assertFalse(os.path.exists(name), name)
 
+    @unittest.skipIf(support.is_wasi,
+                     'checking temp files is not implemented on WASI')
     def test_leak_tmp_file(self):
         code = textwrap.dedent(r"""
             import os.path
@@ -1369,15 +1371,17 @@ def test_leak_tmp_file(self):
                     with open(filename, "wb") as fp:
                         fp.write(b'content')
         """)
-        testname = self.create_test(code=code)
+        testnames = [self.create_test(code=code) for _ in range(3)]
 
-        output = self.run_tests("--fail-env-changed", "-v", "-j1", testname, exitcode=3)
-        self.check_executed_tests(output, [testname],
-                                  env_changed=[testname],
-                                  fail_env_changed=True)
-        self.assertIn(f"Warning -- {testname} leaked temporary "
-                      f"files (1): mytmpfile",
-                      output)
+        output = self.run_tests("--fail-env-changed", "-v", "-j2", *testnames, exitcode=3)
+        self.check_executed_tests(output, testnames,
+                                  env_changed=testnames,
+                                  fail_env_changed=True,
+                                  randomize=True)
+        for testname in testnames:
+            self.assertIn(f"Warning -- {testname} leaked temporary "
+                          f"files (1): mytmpfile",
+                          output)
 
 
 class TestUtils(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Tests/2022-06-16-17-50-58.gh-issue-93353.JdpATx.rst b/Misc/NEWS.d/next/Tests/2022-06-16-17-50-58.gh-issue-93353.JdpATx.rst
new file mode 100644
index 0000000000000..4e232948f49ee
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2022-06-16-17-50-58.gh-issue-93353.JdpATx.rst
@@ -0,0 +1,2 @@
+regrtest now checks if a test leaks temporary files or directories if run
+with -jN option. Patch by Victor Stinner.



More information about the Python-checkins mailing list