Skip to content

Commit

Permalink
Add the class name and scope name to the uninitialized subsystem erro…
Browse files Browse the repository at this point in the history
…r message [IMPROVEMENT]

Add the class name and scope name to the uninitialized subsystem error message

Testing Done:
CI running at https://travis-ci.org/pantsbuild/pants/builds/77148097

Added new unit test

Bugs closed: 2064

Reviewed at https://rbcommons.com/s/twitter/r/2698/
  • Loading branch information
ericzundel committed Sep 1, 2015
1 parent 883fb3d commit 160cfe9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/python/pants/subsystem/subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class Subsystem(SubsystemClientMixin, Optionable):
"""
options_scope_category = ScopeInfo.SUBSYSTEM

class UninitializedSubsystemError(SubsystemError):
def __init__(self, class_name, scope):
super(Subsystem.UninitializedSubsystemError, self).__init__(
'Subsystem "{}" not initialized for scope "{}". '
'Is subsystem missing from subsystem_dependencies() in a task? '.format(class_name, scope))

class CycleException(Exception):
"""Thrown when a circular dependency is detected."""
def __init__(self, cycle):
Expand Down Expand Up @@ -128,7 +134,7 @@ def scoped_instance(cls, optionable):
@classmethod
def _instance_for_scope(cls, scope):
if cls._options is None:
raise SubsystemError('Subsystem not initialized yet.')
raise cls.UninitializedSubsystemError(cls.__name__, scope)
key = (cls, scope)
if key not in cls._scoped_instances:
cls._scoped_instances[key] = cls(scope, cls._options.for_scope(scope))
Expand Down
19 changes: 19 additions & 0 deletions tests/python/pants_test/subsystem/test_subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class DummyOptionable(Optionable):
options_scope = 'foo'


class UninitializedSubsystem(Subsystem):
options_scope = 'uninitialized-scope'


class SubsystemTest(unittest.TestCase):
def setUp(self):
DummySubsystem._options = DummyOptions()
Expand Down Expand Up @@ -114,3 +118,18 @@ def subsystem_dependencies(cls):
for root in SubsystemA, SubsystemB, SubsystemC:
with self.assertRaises(Subsystem.CycleException):
Subsystem.closure((root,))

def test_uninitialized_global(self):

with self.assertRaisesRegexp(Subsystem.UninitializedSubsystemError,
r'UninitializedSubsystem.*uninitialized-scope'):
UninitializedSubsystem.global_instance()

def test_uninitialized_scoped_instance(self):
class UninitializedOptional(Optionable):
options_scope = 'optional'

optional = UninitializedOptional()
with self.assertRaisesRegexp(Subsystem.UninitializedSubsystemError,
r'UninitializedSubsystem.*uninitialized-scope'):
UninitializedSubsystem.scoped_instance(optional)

0 comments on commit 160cfe9

Please sign in to comment.