Skip to content

Commit

Permalink
Remove unused TestBase setup
Browse files Browse the repository at this point in the history
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
  • Loading branch information
Eric-Arellano committed Aug 28, 2020
1 parent fb7cb5d commit 3807816
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
35 changes: 8 additions & 27 deletions src/python/pants/testutil/test_base.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -156,7 +154,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
Expand All @@ -176,7 +174,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:
Expand All @@ -191,7 +189,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:
Expand Down Expand Up @@ -258,19 +256,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()
Expand All @@ -293,21 +278,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:
Expand Down
26 changes: 16 additions & 10 deletions tests/python/pants_test/pantsd/test_process_manager.py
Expand Up @@ -6,6 +6,7 @@
import os
import subprocess
import sys
import unittest
import unittest.mock
from contextlib import contextmanager

Expand All @@ -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():
Expand Down Expand Up @@ -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))
Expand All @@ -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):
Expand Down Expand Up @@ -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-
Expand All @@ -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(
Expand Down

0 comments on commit 3807816

Please sign in to comment.