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: fixture teardown #219

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 22 additions & 7 deletions boa/test/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import warnings
from typing import Generator

import hypothesis
Expand Down Expand Up @@ -52,6 +53,7 @@ def pytest_collection_modifyitems(config, items):

_task_list = set()
_stack = {} # preserve the original order of tasks
_fixture_map = {}
_id = 0


Expand All @@ -61,23 +63,36 @@ def pytest_fixture_setup(fixturedef, request):
_id += 1

ctx = boa.env.anchor()
_stack[task_id] = ctx
_stack[task_id] = (fixturedef, ctx)
_fixture_map.setdefault(fixturedef, []).append(task_id)

ctx.__enter__()

def finalize():
_task_list.add(task_id)
_work_task_list()

fixturedef.addfinalizer(finalize)
def pytest_fixture_post_finalizer(fixturedef, request):
# there are outstanding bugs in pytest where the finalizers get
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wow.. it would be nice to have link(s) so we check when it's fixed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a long story too, you can check the git log for this file

# run out of order, so we need to maintain a task list and defer
# execution of out-of-order finalizers until all the finalizers
# that should have come before them get run.
todo = _fixture_map.get(fixturedef, [])
_task_list.update(todo)

_work_task_list()


def _work_task_list():
while (task_id := next(reversed(_stack.keys()), None)) in _task_list:
ctx = _stack.pop(task_id)
_task_list.remove(task_id)
fixturedef, ctx = _stack.pop(task_id)
ctx.__exit__(None, None, None)

tid = _fixture_map[fixturedef].pop()
assert tid == task_id # sanity check

if len(_fixture_map[fixturedef]) == 0:
del _fixture_map[fixturedef]

_task_list.remove(task_id)


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item: pytest.Item) -> Generator:
Expand Down
Loading