我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用charmhelpers.core.hookenv.Hooks()。
def test_config_saved_after_execute(self): config = hookenv.config() config.implicit_save = True foo = MagicMock() hooks = hookenv.Hooks() hooks.register('foo', foo) hooks.execute(['foo', 'some', 'other', 'args']) self.assertTrue(os.path.exists(config.path))
def test_config_not_saved_after_execute(self): config = hookenv.config() config.implicit_save = False foo = MagicMock() hooks = hookenv.Hooks() hooks.register('foo', foo) hooks.execute(['foo', 'some', 'other', 'args']) self.assertFalse(os.path.exists(config.path))
def test_config_save_disabled(self): config = hookenv.config() config.implicit_save = True foo = MagicMock() hooks = hookenv.Hooks(config_save=False) hooks.register('foo', foo) hooks.execute(['foo', 'some', 'other', 'args']) self.assertFalse(os.path.exists(config.path))
def test_runs_a_registered_function(self): foo = MagicMock() hooks = hookenv.Hooks() hooks.register('foo', foo) hooks.execute(['foo', 'some', 'other', 'args']) foo.assert_called_with()
def test_cannot_run_unregistered_function(self): foo = MagicMock() hooks = hookenv.Hooks() hooks.register('foo', foo) self.assertRaises(hookenv.UnregisteredHookError, hooks.execute, ['bar'])
def test_can_run_a_decorated_function_as_itself(self): execs = [] hooks = hookenv.Hooks() @hooks.hook() def func(): execs.append(True) hooks.execute(['func']) self.assertRaises(hookenv.UnregisteredHookError, hooks.execute, ['brew']) self.assertEqual(execs, [True])
def test_magic_underscores(self): # Juju hook names use hypens as separators. Python functions use # underscores. If explicit names have not been provided, hooks # are registered with both the function name and the function # name with underscores replaced with hypens for convenience. execs = [] hooks = hookenv.Hooks() @hooks.hook() def call_me_maybe(): execs.append(True) hooks.execute(['call-me-maybe']) hooks.execute(['call_me_maybe']) self.assertEqual(execs, [True, True])