Skip to content

Commit 2ab8e45

Browse files
committed
Add setup method for plugs
1 parent fc07e53 commit 2ab8e45

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

openhtf/core/base_plugs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ def _asdict(self) -> Dict[Text, Any]:
169169
"""
170170
return {}
171171

172+
def setUp(self) -> None:
173+
"""This method is called automatically at the start of each Test execution."""
174+
172175
def tearDown(self) -> None:
173176
"""This method is called automatically at the end of each Test execution."""
174177

openhtf/plugs/__init__.py

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -258,47 +258,50 @@ def initialize_plugs(
258258
"""
259259
types = plug_types if plug_types is not None else self._plug_types
260260
for plug_type in types:
261+
plug_instance: plug_type
261262
if plug_type in self._unmanaged_plugs:
263+
plug_instance = self._unmanaged_plugs[plug_type]
262264
continue
263-
264-
# Create a logger for this plug. All plug loggers go under the 'plug'
265-
# sub-logger in the logger hierarchy.
266-
plug_logger = self.logger.getChild(plug_type.__name__)
267-
if plug_type in self._plugs_by_type:
268-
continue
269-
try:
270-
if not issubclass(plug_type, base_plugs.BasePlug):
271-
raise base_plugs.InvalidPlugError(
272-
'Plug type "{}" is not an instance of base_plugs.BasePlug'.format(
273-
plug_type))
274-
275-
# The following method of swapping out loggers is not thread-safe, so it is wrapped in a lock.
276-
with plug_type.init_lock:
277-
if plug_type.logger != _BASE_PLUGS_LOG:
278-
# They put a logger attribute on the class itself, overriding ours.
265+
else:
266+
# Create a logger for this plug. All plug loggers go under the 'plug'
267+
# sub-logger in the logger hierarchy.
268+
plug_logger = self.logger.getChild(plug_type.__name__)
269+
if plug_type in self._plugs_by_type:
270+
continue
271+
try:
272+
if not issubclass(plug_type, base_plugs.BasePlug):
273+
raise base_plugs.InvalidPlugError(
274+
'Plug type "{}" is not an instance of base_plugs.BasePlug'.format(
275+
plug_type))
276+
277+
# The following method of swapping out loggers is not thread-safe, so it is wrapped in a lock.
278+
with plug_type.init_lock:
279+
if plug_type.logger != _BASE_PLUGS_LOG:
280+
# They put a logger attribute on the class itself, overriding ours.
281+
raise base_plugs.InvalidPlugError(
282+
'Do not override "logger" in your plugs.', plug_type)
283+
# Override the logger so that __init__'s logging goes into the record.
284+
plug_type.logger = plug_logger
285+
try:
286+
plug_instance = plug_type()
287+
finally:
288+
# Now set it back since we'll give the instance a logger in a moment.
289+
plug_type.logger = _BASE_PLUGS_LOG
290+
# Set the logger attribute directly (rather than in base_plugs.BasePlug)
291+
# so we don't depend on subclasses' implementation of __init__ to have
292+
# it set.
293+
if plug_instance.logger != _BASE_PLUGS_LOG:
279294
raise base_plugs.InvalidPlugError(
280-
'Do not override "logger" in your plugs.', plug_type)
281-
# Override the logger so that __init__'s logging goes into the record.
282-
plug_type.logger = plug_logger
283-
try:
284-
plug_instance = plug_type()
285-
finally:
286-
# Now set it back since we'll give the instance a logger in a moment.
287-
plug_type.logger = _BASE_PLUGS_LOG
288-
# Set the logger attribute directly (rather than in base_plugs.BasePlug)
289-
# so we don't depend on subclasses' implementation of __init__ to have
290-
# it set.
291-
if plug_instance.logger != _BASE_PLUGS_LOG:
292-
raise base_plugs.InvalidPlugError(
293-
'Do not set "self.logger" in __init__ in your plugs', plug_type)
294-
else:
295-
# Now the instance has its own copy of the test logger.
296-
plug_instance.logger = plug_logger
297-
except Exception: # pylint: disable=broad-except
298-
plug_logger.exception('Exception instantiating plug type %s', plug_type)
299-
self.tear_down_plugs()
300-
raise
295+
'Do not set "self.logger" in __init__ in your plugs', plug_type)
296+
else:
297+
# Now the instance has its own copy of the test logger.
298+
plug_instance.logger = plug_logger
299+
except Exception: # pylint: disable=broad-except
300+
plug_logger.exception('Exception instantiating plug type %s', plug_type)
301+
self.tear_down_plugs()
302+
raise
301303
self.update_plug(plug_type, plug_instance)
304+
plug_instance.setUp()
302305

303306
def get_plug_by_class_path(self,
304307
plug_name: Text) -> Optional[base_plugs.BasePlug]:

0 commit comments

Comments
 (0)