Skip to content

Commit

Permalink
implement fixture isolation
Browse files Browse the repository at this point in the history
isolate state changes inside fixtures
  • Loading branch information
charles-cooper committed Mar 16, 2024
1 parent affa400 commit 0366835
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 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 @@ -50,6 +51,23 @@ def pytest_collection_modifyitems(config, items):
item.add_marker("gas_profile")


_fixture_map = {}


def pytest_fixture_setup(fixturedef, request):
ctx = boa.env.anchor()
assert id(fixturedef) not in _fixture_map, "bad invariant"
_fixture_map[id(fixturedef)] = ctx
ctx.__enter__()


def pytest_fixture_post_finalizer(fixturedef, request):
if (ctx := _fixture_map.pop(id(fixturedef), None)) is not None:
ctx.__exit__(None, None, None)
else:
warnings.warn("possible bug in titanoboa! bad fixture tracking", stacklevel=1)


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item: pytest.Item) -> Generator:
ignore_isolation = item.get_closest_marker("ignore_isolation") is not None
Expand Down
13 changes: 13 additions & 0 deletions tests/unitary/test_isolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,16 @@ def setup_ignore_isolation(boa_contract):
def test_check_ignore_isolation(boa_contract, setup_ignore_isolation):
assert boa_contract.a() == 42069
assert boa_contract.b() == addr_constn


@pytest.fixture(scope="function")
def modify_boa_contract(boa_contract):
# these values spill over from the previous test
assert boa_contract.a() == 42069
assert boa_contract.b() == addr_constn
boa_contract.set_vars(boa_contract.a() + 1, boa.env.generate_address())


@pytest.mark.parametrize("a", range(10))
def test_fixture_isolation(modify_boa_contract, a):
pass

0 comments on commit 0366835

Please sign in to comment.