diff --git a/test/common.py b/test/common.py index a73287f8589d3..2fc1eacbd9523 100644 --- a/test/common.py +++ b/test/common.py @@ -699,6 +699,13 @@ def set_temp_dir(self, temp_dir): # Explicitly set dedicated temporary directory for parallel tests os.environ['EMCC_TEMP_DIR'] = self.temp_dir + def reset_temp_dir(self): + # Deleting these instance fields will mean that the class field is used + # instead. + del self.temp_dir + del self.canonical_temp_dir + del os.environ['EMCC_TEMP_DIR'] + def parse_wasm(self, filename): wat = self.get_wasm_text(filename) imports = [] @@ -797,7 +804,8 @@ def setUp(self): self.temp_files_before_run.append(os.path.normpath(os.path.join(root, filename))) if self.runningInParallel(): - self.working_dir = tempfile.mkdtemp(prefix='emscripten_test_' + self.__class__.__name__ + '_', dir=self.temp_dir) + # When running tests in parallel each test runs it its own new temp directory + self.working_dir = tempfile.mkdtemp(prefix='emtest_' + self.__class__.__name__ + '_', dir=self.temp_dir) else: self.working_dir = path_from_root('out/test') if os.path.exists(self.working_dir): diff --git a/test/parallel_testsuite.py b/test/parallel_testsuite.py index 389f2db2eed0c..5a1cd0b374c3a 100644 --- a/test/parallel_testsuite.py +++ b/test/parallel_testsuite.py @@ -53,10 +53,7 @@ def test_failed(): with lock: allowed_failures_counter.value -= 1 - olddir = os.getcwd() result = BufferedParallelTestResult(lock, progress_counter, num_tests) - temp_dir = tempfile.mkdtemp(prefix='emtest_') - test.set_temp_dir(temp_dir) try: if test.__class__ not in seen_class: seen_class.add(test.__class__) @@ -71,10 +68,6 @@ def test_failed(): except Exception as e: result.addError(test, e) test_failed() - # Before attempting to delete the tmp dir make sure the current - # working directory is not within it. - os.chdir(olddir) - common.force_delete_dir(temp_dir) return result diff --git a/test/test_other.py b/test/test_other.py index 0ae20c4824dd9..4598914ed45ac 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -19,6 +19,7 @@ import subprocess import sys import tarfile +import tempfile import time from datetime import datetime from functools import wraps @@ -142,6 +143,13 @@ def uses_canonical_tmp(func): """ @wraps(func) def decorated(self, *args, **kwargs): + if self.runningInParallel(): + # Becuase the canical temp directory is global/central location when + # tests use it in parallel they need to temporarily override the + # temp directory to ovoid clobbering each each. + new_temp = tempfile.mkdtemp(prefix='emtest_tmpdir_', dir=self.temp_dir) + self.set_temp_dir(new_temp) + # Before running the test completely remove the canonical_tmp if os.path.exists(self.canonical_temp_dir): shutil.rmtree(self.canonical_temp_dir) @@ -155,6 +163,8 @@ def decorated(self, *args, **kwargs): # test fails we would not clean it up, and if leak detection # is set we will show that error instead of the actual one. shutil.rmtree(self.canonical_temp_dir) + if self.runningInParallel(): + self.reset_temp_dir() return decorated