Skip to content

Commit

Permalink
Make TestBase less stateful.
Browse files Browse the repository at this point in the history
This fixes zipimporter issues noted in pantsbuild#6282 and is generally desirable
in the absence of severe performance degradation.

Fixes pantsbuild#6282
  • Loading branch information
jsirois committed May 22, 2019
1 parent 805ef8a commit 273118b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@
from pants.build_graph.build_file_aliases import BuildFileAliases
from pants.build_graph.target import Target
from pants.java.jar.jar_dependency import JarDependency
from pants.util.dirutil import safe_mkdir, safe_mkdtemp, safe_open
from pants.util.memo import memoized_classproperty
from pants_test.task_test_base import ConsoleTaskTestBase


class CheckPublishedDepsTest(ConsoleTaskTestBase):

@memoized_classproperty
def push_db_basedir(cls):
return safe_mkdtemp()

@classmethod
def alias_groups(cls):
return BuildFileAliases(
Expand All @@ -35,10 +41,13 @@ def alias_groups(cls):
'jar': JarDependency,
'scala_artifact': ScalaArtifact,
'scala_jar': ScalaJarDependency,
'repo': Repository(name='repo',
url='http://www.www.com',
push_db_basedir=os.path.join(cls._build_root(), 'repo')),
}

},
context_aware_object_factories={
'repo': lambda _: Repository(name='repo',
url='http://www.www.com',
push_db_basedir=cls.push_db_basedir),
},
)

@classmethod
Expand All @@ -53,13 +62,19 @@ def assert_console_output(self, *args, **kwargs):
def setUp(self):
super(CheckPublishedDepsTest, self).setUp()

self.create_file('repo/org.name/lib1/publish.properties', dedent("""
safe_mkdir(self.push_db_basedir, clean=True)

def write_db_file(relpath, contents):
with safe_open(os.path.join(self.push_db_basedir, relpath), 'w') as fp:
fp.write(contents)

write_db_file('org.name/lib1/publish.properties', dedent("""
revision.major.org.name%lib1=2
revision.minor.org.name%lib1=0
revision.patch.org.name%lib1=0
revision.sha.org.name%lib1=12345
"""))
self.create_file('repo/org.name/lib2/publish.properties', dedent("""
write_db_file('org.name/lib2/publish.properties', dedent("""
revision.major.org.name%lib2=2
revision.minor.org.name%lib2=0
revision.patch.org.name%lib2=0
Expand Down
22 changes: 15 additions & 7 deletions tests/python/pants_test/backend/jvm/tasks/test_jar_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import absolute_import, division, print_function, unicode_literals

import os
import unittest
from builtins import object, str

Expand All @@ -21,7 +20,8 @@
from pants.build_graph.target import Target
from pants.scm.scm import Scm
from pants.util.contextutil import temporary_dir
from pants.util.dirutil import safe_mkdir, safe_walk
from pants.util.dirutil import safe_mkdir, safe_mkdtemp, safe_walk
from pants.util.memo import memoized_classproperty
from pants_test.task_test_base import TaskTestBase


Expand All @@ -37,11 +37,12 @@ def test_smoke_publish(self):
task = self.create_task(self.context())
task.execute()

@memoized_classproperty
def push_db_basedir(cls):
return safe_mkdtemp()

@classmethod
def alias_groups(cls):
cls.push_db_basedir = os.path.join(cls._build_root(), "pushdb")
safe_mkdir(cls.push_db_basedir)

return BuildFileAliases(
targets={
'jar_library': JarLibrary,
Expand All @@ -51,11 +52,18 @@ def alias_groups(cls):
objects={
'artifact': Artifact,
'scala_artifact': ScalaArtifact,
'internal': Repository(name='internal', url='http://example.com',
push_db_basedir=cls.push_db_basedir),
},
context_aware_object_factories={
'internal': lambda _: Repository(name='internal',
url='http://example.com',
push_db_basedir=cls.push_db_basedir),
},
)

def setUp(self):
super(JarPublishTest, self).setUp()
safe_mkdir(self.push_db_basedir, clean=True)

def _prepare_for_publishing(self, with_alias=False):
targets = []
nail_target = self._create_nail_target()
Expand Down
51 changes: 23 additions & 28 deletions tests/python/pants_test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,21 +346,19 @@ def _reset_engine(self):
self._build_graph.reset()
self._scheduler.invalidate_all_files()

@classmethod
def aggressively_reset_scheduler(cls):
cls._scheduler = None
if cls._local_store_dir is not None:
safe_rmtree(cls._local_store_dir)
def aggressively_reset_scheduler(self):
self._scheduler = None
if self._local_store_dir is not None:
safe_rmtree(self._local_store_dir)

@classmethod
@contextmanager
def isolated_local_store(cls):
cls.aggressively_reset_scheduler()
cls._init_engine()
def isolated_local_store(self):
self.aggressively_reset_scheduler()
self._init_engine()
try:
yield
finally:
cls.aggressively_reset_scheduler()
self.aggressively_reset_scheduler()

@property
def build_root(self):
Expand All @@ -370,41 +368,38 @@ def build_root(self):
def pants_workdir(self):
return self._pants_workdir()

@classmethod
@memoized_method
def _build_root(cls):
cls.real_build_root = BuildRoot().path
def _build_root(self):
self.real_build_root = BuildRoot().path
return os.path.realpath(mkdtemp(suffix='_BUILD_ROOT'))

@classmethod
@memoized_method
def _pants_workdir(cls):
return os.path.join(cls._build_root(), '.pants.d')
def _pants_workdir(self):
return os.path.join(self._build_root(), '.pants.d')

@classmethod
def _init_engine(cls):
if cls._scheduler is not None:
def _init_engine(self):
if self._scheduler is not None:
return

cls._local_store_dir = os.path.realpath(safe_mkdtemp())
safe_mkdir(cls._local_store_dir)
self._local_store_dir = os.path.realpath(safe_mkdtemp())
safe_mkdir(self._local_store_dir)

# NB: This uses the long form of initialization because it needs to directly specify
# `cls.alias_groups` rather than having them be provided by bootstrap options.
graph_session = EngineInitializer.setup_legacy_graph_extended(
pants_ignore_patterns=None,
workdir=cls._pants_workdir(),
local_store_dir=cls._local_store_dir,
workdir=self._pants_workdir(),
local_store_dir=self._local_store_dir,
build_file_imports_behavior='allow',
native=init_native(),
options_bootstrapper=OptionsBootstrapper.create(args=['--pants-config-files=[]']),
build_configuration=cls.build_config(),
build_configuration=self.build_config(),
build_ignore_patterns=None,
).new_session()
cls._scheduler = graph_session.scheduler_session
cls._build_graph, cls._address_mapper = graph_session.create_build_graph(
TargetRoots([]), cls._build_root()
)
self._scheduler = graph_session.scheduler_session
self._build_graph, self._address_mapper = graph_session.create_build_graph(
TargetRoots([]), self._build_root()
)

@property
def scheduler(self):
Expand Down

0 comments on commit 273118b

Please sign in to comment.