Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple hook execution #123

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mamba/example_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def execute_hook(self, hook, execution_context):
if hook.endswith('_all') and not self.hooks.get(hook):
return

if self.parent is not None:
if not hook.endswith('_all') and self.parent is not None:
self.parent.execute_hook(hook, execution_context)

for registered in self.hooks.get(hook, []):
Expand Down
16 changes: 12 additions & 4 deletions spec/hooks_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,43 @@
expect(self.execution_context.calls).to(equal(['example_1', 'example_2', 'after_all']))

with context('when having nested contexts'):
with it('executes only once the before_all hooks'):
with it('executes each before_all hook only once'):
self.parent.hooks['before_all'] = [lambda ctx: ctx.calls.append('before_all_parent')]

child = an_example_group()
child.append(Example(lambda ctx: ctx.calls.append('example')))
child.hooks['before_all'] = [lambda ctx: ctx.calls.append('before_all_child_1')]
self.parent.append(child)

child = an_example_group()
child.append(Example(lambda ctx: ctx.calls.append('example_2')))
child.hooks['before_all'] = [lambda ctx: ctx.calls.append('before_all_child_2')]
self.parent.append(child)

self.parent.execute(self.reporter, self.execution_context)

expect(self.execution_context.calls).to(equal(['before_all_parent', 'example', 'example_2']))
expect(self.execution_context.calls).to(equal(
['before_all_parent', 'before_all_child_1', 'example', 'before_all_child_2', 'example_2']
))

with it('executes only once the after_all hooks'):
with it('executes each after_all hook only once (in opposite nesting order)'):
self.parent.hooks['after_all'] = [lambda ctx: ctx.calls.append('after_all_parent')]

child = an_example_group()
child.append(Example(lambda ctx: ctx.calls.append('example')))
child.hooks['after_all'] = [lambda ctx: ctx.calls.append('after_all_child_1')]
self.parent.append(child)

child = an_example_group()
child.append(Example(lambda ctx: ctx.calls.append('example_2')))
child.hooks['after_all'] = [lambda ctx: ctx.calls.append('after_all_child_2')]
self.parent.append(child)

self.parent.execute(self.reporter, self.execution_context)

expect(self.execution_context.calls).to(equal(['example', 'example_2', 'after_all_parent']))
expect(self.execution_context.calls).to(equal(
['example', 'after_all_child_1', 'example_2', 'after_all_child_2', 'after_all_parent']
))

with it('executes first the before_all and later the before_each for every parent, in declaration order'):
self.parent.hooks['before_all'] = [lambda ctx: ctx.calls.append('before_all_parent')]
Expand Down