diff --git a/src/python/pants/testutil/test_base.py b/src/python/pants/testutil/test_base.py index b2551332960..43ccbadb945 100644 --- a/src/python/pants/testutil/test_base.py +++ b/src/python/pants/testutil/test_base.py @@ -5,7 +5,6 @@ import os import unittest from abc import ABC, ABCMeta, abstractmethod -from collections import defaultdict from contextlib import contextmanager from dataclasses import dataclass from io import StringIO @@ -42,7 +41,6 @@ from pants.init.util import clean_global_runtime_state from pants.option.global_options import ExecutionOptions, GlobalOptions from pants.option.options_bootstrapper import OptionsBootstrapper -from pants.option.scope import GLOBAL_SCOPE from pants.option.subsystem import Subsystem from pants.source import source_root from pants.testutil.engine_util import Params @@ -157,7 +155,7 @@ def run_goal_rule( console.flush() return GoalRuleResult(exit_code, stdout.getvalue(), stderr.getvalue()) - def invalidate_for(self, *relpaths): + def _invalidate_for(self, *relpaths): """Invalidates all files from the relpath, recursively up to the root. Many python operations implicitly create parent directories, so we assume that touching a @@ -177,7 +175,7 @@ def create_dir(self, relpath: str) -> str: """ path = os.path.join(self.build_root, relpath) safe_mkdir(path) - self.invalidate_for(relpath) + self._invalidate_for(relpath) return path def create_file(self, relpath: str, contents: str = "", mode: str = "w") -> str: @@ -192,7 +190,7 @@ def create_file(self, relpath: str, contents: str = "", mode: str = "w") -> str: path = os.path.join(self.build_root, relpath) with safe_open(path, mode=mode) as fp: fp.write(contents) - self.invalidate_for(relpath) + self._invalidate_for(relpath) return path def create_files(self, path: str, files: Iterable[str]) -> None: @@ -259,19 +257,6 @@ def setUp(self): BuildRoot().path = self.build_root self.addCleanup(BuildRoot().reset) - self.subprocess_dir = os.path.join(self.build_root, ".pids") - - self.options = defaultdict(dict) # scope -> key-value mapping. - self.options[GLOBAL_SCOPE] = { - "pants_workdir": self.pants_workdir, - "pants_supportdir": os.path.join(self.build_root, "build-support"), - "pants_distdir": os.path.join(self.build_root, "dist"), - "pants_configdir": os.path.join(self.build_root, "config"), - "pants_subprocessdir": self.subprocess_dir, - } - - self._build_configuration = self.build_config() - def _reset_engine(self): if self._scheduler is not None: self._scheduler.invalidate_all_files() @@ -294,21 +279,17 @@ def isolated_local_store(self): self._scheduler = None safe_rmtree(local_store_dir) + @memoized_method + def _build_root(self) -> str: + return os.path.realpath(mkdtemp(suffix="_BUILD_ROOT")) + @property def build_root(self) -> str: return self._build_root() @property def pants_workdir(self) -> str: - return self._pants_workdir() - - @memoized_method - def _build_root(self) -> str: - return os.path.realpath(mkdtemp(suffix="_BUILD_ROOT")) - - @memoized_method - def _pants_workdir(self) -> str: - return os.path.join(self._build_root(), ".pants.d") + return os.path.join(self.build_root, ".pants.d") def _init_engine(self, local_store_dir: Optional[str] = None) -> None: if self._scheduler is not None: diff --git a/tests/python/pants_test/pantsd/test_process_manager.py b/tests/python/pants_test/pantsd/test_process_manager.py index 77d51212a0f..6621407cd53 100644 --- a/tests/python/pants_test/pantsd/test_process_manager.py +++ b/tests/python/pants_test/pantsd/test_process_manager.py @@ -6,6 +6,7 @@ import os import subprocess import sys +import unittest import unittest.mock from contextlib import contextmanager @@ -18,23 +19,25 @@ ProcessMetadataManager, swallow_psutil_exceptions, ) -from pants.testutil.test_base import TestBase from pants.util.contextutil import temporary_dir -from pants.util.dirutil import safe_file_dump +from pants.util.dirutil import safe_file_dump, safe_mkdtemp PATCH_OPTS = dict(autospec=True, spec_set=True) def fake_process(**kwargs): proc = unittest.mock.create_autospec(psutil.Process, spec_set=True) - [setattr(getattr(proc, k), "return_value", v) for k, v in kwargs.items()] + for k, v in kwargs.items(): + setattr(getattr(proc, k), "return_value", v) return proc -class TestProcessGroup(TestBase): +class TestProcessGroup(unittest.TestCase): + SUBPROCESS_DIR = safe_mkdtemp() + def setUp(self): super().setUp() - self.pg = ProcessGroup("test", metadata_base_dir=self.subprocess_dir) + self.pg = ProcessGroup("test", metadata_base_dir=self.SUBPROCESS_DIR) def test_swallow_psutil_exceptions(self): with swallow_psutil_exceptions(): @@ -100,16 +103,17 @@ def test_iter_instances(self): self.assertTrue("_test" in item.name) -class TestProcessMetadataManager(TestBase): +class TestProcessMetadataManager(unittest.TestCase): NAME = "_test_" TEST_KEY = "TEST" TEST_VALUE = "300" TEST_VALUE_INT = 300 BUILDROOT = "/mock_buildroot/" + SUBPROCESS_DIR = safe_mkdtemp() def setUp(self): super().setUp() - self.pmm = ProcessMetadataManager(metadata_base_dir=self.subprocess_dir) + self.pmm = ProcessMetadataManager(metadata_base_dir=self.SUBPROCESS_DIR) def test_maybe_cast(self): self.assertIsNone(self.pmm._maybe_cast(None, int)) @@ -129,7 +133,7 @@ def test_maybe_init_metadata_dir_by_name(self): ) as mock_mkdir: self.pmm._maybe_init_metadata_dir_by_name(self.NAME) mock_mkdir.assert_called_once_with( - self.pmm._get_metadata_dir_by_name(self.NAME, self.subprocess_dir) + self.pmm._get_metadata_dir_by_name(self.NAME, self.SUBPROCESS_DIR) ) def test_readwrite_metadata_by_name(self): @@ -200,7 +204,9 @@ def test_purge_metadata_error(self): self.assertGreater(mock_rm.call_count, 0) -class TestProcessManager(TestBase): +class TestProcessManager(unittest.TestCase): + SUBPROCESS_DIR = safe_mkdtemp() + def setUp(self): super().setUp() # N.B. We pass in `metadata_base_dir` here because ProcessManager (itself a non-task/non- @@ -209,7 +215,7 @@ def setUp(self): # dependencies in a typical pants run (and integration tests), but not in unit tests. # Thus, passing this parameter here short-circuits the subsystem-reliant path for the # purposes of unit testing without requiring adhoc subsystem initialization. - self.pm = ProcessManager("test", metadata_base_dir=self.subprocess_dir) + self.pm = ProcessManager("test", metadata_base_dir=self.SUBPROCESS_DIR) def test_process_properties(self): with unittest.mock.patch.object(