Skip to content

Commit

Permalink
Merge 9ea3542 into e61a033
Browse files Browse the repository at this point in the history
  • Loading branch information
benjyw committed May 13, 2015
2 parents e61a033 + 9ea3542 commit 6fe5773
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 61 deletions.
3 changes: 2 additions & 1 deletion src/python/pants/subsystem/subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ def global_instance(cls):

@classmethod
def reset(cls):
"""Forget all cached subsystem instances.
"""Forget all option values and cached subsystem instances.
Used for test isolation.
"""
cls._options = None
cls._scoped_instances = {}

@classmethod
Expand Down
5 changes: 3 additions & 2 deletions tests/python/pants_test/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def alias_groups(self):
def setUp(self):
super(BaseTest, self).setUp()
Goal.clear()
Subsystem.reset()

self.real_build_root = BuildRoot().path

self.build_root = os.path.realpath(mkdtemp(suffix='_BUILD_ROOT'))
Expand All @@ -137,8 +139,7 @@ def setUp(self):
BuildRoot().path = self.build_root
self.addCleanup(BuildRoot().reset)

Subsystem.reset()

# We need a pants.ini, even if empty. get_buildroot() uses its presence.
self.create_file('pants.ini')
self._build_configuration = BuildConfiguration()
self._build_configuration.register_aliases(self.alias_groups)
Expand Down
26 changes: 13 additions & 13 deletions tests/python/pants_test/jvm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,42 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_tests(
name = 'artifact',
sources = ['test_artifact.py'],
dependencies = [
name='artifact',
sources=['test_artifact.py'],
dependencies=[
'src/python/pants/backend/jvm:artifact',
'src/python/pants/backend/jvm:repository',
'tests/python/pants_test:base_test',
]
)

python_library(
name = 'jvm_tool_task_test_base',
sources = ['jvm_tool_task_test_base.py'],
dependencies = [
name='jvm_tool_task_test_base',
sources=['jvm_tool_task_test_base.py'],
dependencies=[
'src/python/pants/backend/jvm/subsystems:jvm_tool_mixin',
'src/python/pants/backend/jvm/targets:jvm',
'src/python/pants/backend/jvm/tasks:bootstrap_jvm_tools',
'src/python/pants/base:config',
'src/python/pants/base:build_file_aliases',
'src/python/pants/ivy',
'src/python/pants/util:dirutil',
'tests/python/pants_test/tasks:task_test_base',
]
)

python_library(
name = 'nailgun_task_test_base',
sources = ['nailgun_task_test_base.py'],
dependencies = [
name='nailgun_task_test_base',
sources=['nailgun_task_test_base.py'],
dependencies=[
':jvm_tool_task_test_base',
'src/python/pants/backend/jvm/tasks:nailgun_task',
]
)

python_library(
name = 'jar_task_test_base',
sources = ['jar_task_test_base.py'],
dependencies = [
name='jar_task_test_base',
sources=['jar_task_test_base.py'],
dependencies=[
':nailgun_task_test_base',
]
)
50 changes: 5 additions & 45 deletions tests/python/pants_test/jvm/jvm_tool_task_test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

import errno
import os
import shutil

Expand All @@ -14,9 +13,8 @@
from pants.backend.jvm.targets.jar_library import JarLibrary
from pants.backend.jvm.tasks.bootstrap_jvm_tools import BootstrapJvmTools
from pants.base.build_file_aliases import BuildFileAliases
from pants.base.config import Config
from pants.ivy.bootstrapper import Bootstrapper
from pants.util.dirutil import safe_mkdir, safe_mkdtemp, safe_walk
from pants.util.dirutil import safe_mkdtemp
from pants_test.tasks.task_test_base import TaskTestBase


Expand All @@ -36,11 +34,6 @@ def alias_groups(self):
)

def setUp(self):
# TODO(Eric Ayers): this is the old way
# Ensure we get a read of the real pants.ini config
Config.reset_default_bootstrap_option_values()
real_config = Config.from_cache()

super(JvmToolTaskTestBase, self).setUp()

# Use a synthetic subclass for bootstrapping within the test, to isolate this from
Expand All @@ -53,43 +46,10 @@ def setUp(self):
# tool, constructing a fat jar and then shading that fat jar.
self.set_options_for_scope(bootstrap_scope, jvm_options=['-Xmx128m'])

def link_or_copy(src, dest):
try:
os.link(src, dest)
except OSError as e:
if e.errno == errno.EXDEV:
shutil.copy(src, dest)
else:
raise e

def link(path, optional=False, force=False):
src = os.path.join(self.real_build_root, path)
if not optional or os.path.exists(src):
dest = os.path.join(self.build_root, path)
safe_mkdir(os.path.dirname(dest))
try:
link_or_copy(src, dest)
except OSError as e:
if force and e.errno == errno.EEXIST:
os.unlink(dest)
link_or_copy(src, dest)
else:
raise e
return dest

def link_tree(path, optional=False, force=False):
src = os.path.join(self.real_build_root, path)
if not optional or os.path.exists(src):
for abspath, dirs, files in safe_walk(src):
for f in files:
link(os.path.relpath(os.path.join(abspath, f), self.real_build_root), force=force)

# TODO(John Sirois): Find a way to do this cleanly
link('pants.ini', force=True)
link('BUILD.tools', force=True)

support_dir = real_config.getdefault('pants_supportdir')
link_tree(os.path.relpath(os.path.join(support_dir, 'ivy'), self.real_build_root), force=True)
# Tool option defaults currently point to targets in the real BUILD.tools, so we copy it
# into our test workspace.
shutil.copy(os.path.join(self.real_build_root, 'BUILD.tools'), self.build_root)

Bootstrapper.reset_instance()

def context(self, for_task_types=None, options=None, target_roots=None,
Expand Down
8 changes: 8 additions & 0 deletions tests/python/pants_test/tasks/test_jar_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ def get_source_root_fs_path(path):
SourceRoot.register(get_source_root_fs_path('src/scala'), ScalaLibrary)
SourceRoot.register(get_source_root_fs_path('src/thrift'), JavaThriftLibrary)

# This is hacky: Creating a scala_library below requires Subsystem initialization,
# which we must force by this call to the superclass context() (note that can't call our own
# context() because it expects the scala_library to already exist). The test methods themselves
# then call self.context() again to get hold of a context, which will superfluously (but
# harmlessly) re-initialize Subsystem.
# TODO: Fix this hack by separating option setup from context setup in the test sequence.
super(JarCreateExecuteTest, self).context()

self.res = self.create_resources(test_path('src/resources/com/twitter'), 'spam', 'r.txt')
self.jl = self.java_library(test_path('src/java/com/twitter'), 'foo', ['a.java'],
resources=test_path('src/resources/com/twitter:spam'))
Expand Down

0 comments on commit 6fe5773

Please sign in to comment.