def test_smoke(self): # Create a collection col = PlugInCollection(self._NAMESPACE) # Ensure that namespace was saved self.assertEqual(col._namespace, self._NAMESPACE) # Ensure that plugins start out empty self.assertEqual(len(col._plugins), 0) # Ensure that 'loaded' flag is false self.assertFalse(col._loaded)
def test_load(self, mock_iter): # Create a collection col = PlugInCollection(self._NAMESPACE) # Create a mocked entry point mock_ep1 = mock.Mock() mock_ep1.name = "zzz" mock_ep1.load.return_value = "two" # Create another mocked entry point mock_ep2 = mock.Mock() mock_ep2.name = "aaa" mock_ep2.load.return_value = "one" # Make the collection load both mocked entry points mock_iter.return_value = [mock_ep1, mock_ep2] # Load plugins col.load() # Ensure that pkg_resources were interrogated mock_iter.assert_calledwith(self._NAMESPACE) # Ensure that both entry points were loaded mock_ep1.load.assert_called_with() mock_ep2.load.assert_called_with()
def test_load_failing(self, mock_iter, mock_logger): # Create a collection col = PlugInCollection(self._NAMESPACE) # Create a mocked entry point mock_ep1 = mock.Mock() mock_ep1.name = "zzz" mock_ep1.load.return_value = "two" # Create another mockeed antry point mock_ep2 = mock.Mock() mock_ep2.name = "aaa" mock_ep2.load.side_effect = ImportError("boom") # Make the collection load both mocked entry points mock_iter.return_value = [mock_ep1, mock_ep2] # Load plugins col.load() # Ensure that pkg_resources were interrogated mock_iter.assert_calledwith(self._NAMESPACE) # Ensure that both entry points were loaded mock_ep1.load.assert_called_with() mock_ep2.load.assert_called_with() # Ensure that an exception was logged mock_logger.exception.assert_called_with( "Unable to import %s", mock_ep2)
def test_aux_methods(self): # Create a collection col = PlugInCollection(self._NAMESPACE) # Create a mocked entry plugin plug1 = PlugIn("ep1", "obj1") plug2 = PlugIn("ep2", "obj2") # With fake plugins with col.fake_plugins([plug1, plug2]): # Check that plugins are correct self.assertIs(col.get_by_name('ep1'), plug1) self.assertIs(col.get_by_name('ep2'), plug2) # Access all plugins self.assertEqual(col.get_all_plugins(), [plug1, plug2]) # Access all plugin names self.assertEqual(col.get_all_names(), ['ep1', 'ep2']) # Access all pairs (name, plugin) self.assertEqual(col.get_all_items(), [('ep1', plug1), ('ep2', plug2)])
def test_default_wrapper(self): # Create a collection col = PlugInCollection(self._NAMESPACE) # Ensure that the wrapper is :class:`PlugIn` self.assertEqual(col._wrapper, PlugIn)
""" Parse the specified text and return a parser-specific native Abstract Syntax Tree that represents the input. Any exception gets logged and causes None to be returned. """ try: return self.parser_fn(text) except Exception: # TODO: portable parser error would be nice, to know where it # fails. This is difficult at this stage. logger.exception("Cannot parse input") return None def _to_json(self, obj): """ Helper method to convert arbitrary objects to their JSON representation. Anything that has a 'as_json' attribute will be converted to the result of calling that method. For all other objects __dict__ is returned. """ if hasattr(obj, "as_json"): return obj.as_json() else: return obj.__dict__ # Collection of all parsers all_parsers = PlugInCollection('plainbox.parsers', wrapper=ParserPlugIn)