Skip to content

Commit

Permalink
feat: log snaps
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Sep 30, 2024
1 parent d6b45e0 commit 0a85449
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
12 changes: 12 additions & 0 deletions src/ape/pytest/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ class ConfigWrapper(ManagerAccessMixin):

def __init__(self, pytest_config: PytestConfig):
self.pytest_config = pytest_config
if not self.verbosity:
# Enable verbose output if stdout capture is disabled
self.verbosity = self.pytest_config.getoption("capture") == "no"
# else: user has already changes verbosity to an equal or higher level; avoid downgrading.

@property
def verbosity(self) -> int:
return self.pytest_config.option.verbose

@verbosity.setter
def verbosity(self, value):
self.pytest_config.option.verbose = value

@cached_property
def supports_tracing(self) -> bool:
Expand Down
22 changes: 21 additions & 1 deletion src/ape/pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest
from eth_utils import to_hex
from rich import print as rich_print

from ape.api.accounts import TestAccountAPI
from ape.api.transactions import ReceiptAPI
Expand Down Expand Up @@ -112,7 +113,11 @@ def parameters(self) -> list[str]:

@property
def isolation(self) -> list[str]:
return [n.lstrip("_").split("_")[0] for n in self.names if n.endswith("_isolation") and n.startswith("_")]
return [
n.lstrip("_").split("_")[0]
for n in self.names
if n.endswith("_isolation") and n.startswith("_")
]

@property
def parametrized(self) -> dict[str, list]:
Expand Down Expand Up @@ -354,6 +359,7 @@ class IsolationManager(ManagerAccessMixin):
def __init__(self, config_wrapper: ConfigWrapper, receipt_capture: "ReceiptCapture"):
self.config_wrapper = config_wrapper
self.receipt_capture = receipt_capture
self._records: list[str] = []

@cached_property
def _track_transactions(self) -> bool:
Expand Down Expand Up @@ -404,6 +410,9 @@ def set_snapshot(self, scope: Scope):
if not self.supported:
return

if self.config_wrapper.verbosity:
self._records.append(f"snapshot-taken '{scope.name.upper()}'")

try:
snapshot_id = self.take_snapshot()
except Exception:
Expand Down Expand Up @@ -437,6 +446,9 @@ def restore(self, scope: Scope):
self.snapshots.clear_snapshot_id(scope)
return

if self.config_wrapper.verbosity:
self._records.append(f"restoring '{scope.name.upper()}'")

try:
self.chain_manager.restore(snapshot_id)
except NotImplementedError:
Expand All @@ -449,6 +461,14 @@ def restore(self, scope: Scope):

self.snapshots.clear_snapshot_id(scope)

def show_records(self):
if not self._records:
return

records_str = "\n".join(self._records)
rich_print(f"\n{records_str}")
self._records = []


class ReceiptCapture(ManagerAccessMixin):
receipt_map: dict[str, dict[str, ReceiptAPI]] = {}
Expand Down
5 changes: 0 additions & 5 deletions src/ape/pytest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ def is_module(v):
gas_tracker = GasTracker(config_wrapper)
coverage_tracker = CoverageTracker(config_wrapper)

if not config.option.verbose:
# Enable verbose output if stdout capture is disabled
config.option.verbose = config.getoption("capture") == "no"
# else: user has already changes verbosity to an equal or higher level; avoid downgrading.

# Register the custom Ape test runner
runner = PytestApeRunner(
config_wrapper, isolation_manager, receipt_capture, gas_tracker, coverage_tracker
Expand Down
19 changes: 13 additions & 6 deletions src/ape/pytest/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def pytest_runtest_setup(self, item):
invalid_fixtures[next_snapshot.scope].extend(next_snapshot.fixtures)

# Restore the state now.
invalidated = []
if scope_to_revert is not None:
self.isolation_manager.restore(scope_to_revert)

Expand All @@ -218,13 +219,23 @@ def pytest_runtest_setup(self, item):
info_ls = fixtures.get_info(invalid_fixture)
for info in info_ls:
info.cached_result = None
invalidated.append(info.name)

# Also, invalidate the corresponding isolation fixture.
if invalid_isolation_fixture_ls := fixtures.get_info(
invalid_scope.isolation_fixturename
):
for invalid_isolation_fixture in invalid_isolation_fixture_ls:
invalid_isolation_fixture.cached_result = None
invalidated.append(invalid_isolation_fixture.name)

if invalidated and self.config_wrapper.verbosity:
log = "rebase"
if scope_to_revert is not None:
log = f"{log} scope={scope_to_revert}"

log = f"{log} invalidated-fixtures='{', '.join(invalidated)}'"
self.isolation_manager._records.append(log)

# Append these fixtures so we know when new ones arrive
# and need to trigger the invalidation logic above.
Expand Down Expand Up @@ -276,12 +287,8 @@ def pytest_collection_finish(self, session):
self._provider_is_connected = True

def pytest_runtest_logreport(self, report: TestReport):
breakpoint()
if report.when == "setup":
fixtures = self.fixture_manager.get_fixtures(report.nodeid)
isolation = fixtures.isolation
log = f"Isolation={', '.join(isolation)}"
rich_print(f"\n{log}\n")
if self.config_wrapper.verbosity >= 3:
self.isolation_manager.show_records()

def pytest_terminal_summary(self, terminalreporter):
"""
Expand Down

0 comments on commit 0a85449

Please sign in to comment.