Skip to content

Commit

Permalink
fix: issue running tests when mainnet is default network and specif…
Browse files Browse the repository at this point in the history
…ying a different network (#2343)
  • Loading branch information
antazoey authored Oct 26, 2024
1 parent a24374b commit fee84bb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
9 changes: 0 additions & 9 deletions src/ape/pytest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ def _get_default_network(ecosystem: Optional[EcosystemAPI] = None) -> str:
if ecosystem is None:
ecosystem = ManagerAccessMixin.network_manager.default_ecosystem

if ecosystem.default_network.is_mainnet:
# Don't use mainnet for tests, even if it configured as
# the default.
raise ConfigError(
"Default network is mainnet; unable to run tests on mainnet. "
"Please specify the network using the `--network` flag or "
"configure a different default network."
)

return ecosystem.name


Expand Down
21 changes: 19 additions & 2 deletions src/ape/pytest/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rich import print as rich_print

from ape.api.networks import ProviderContextManager
from ape.exceptions import ConfigError
from ape.logging import LogLevel
from ape.pytest.config import ConfigWrapper
from ape.pytest.coverage import CoverageTracker
Expand Down Expand Up @@ -206,8 +207,24 @@ def pytest_collection_finish(self, session):

# Only start provider if collected tests.
if not outcome.get_result() and session.items:
self._provider_context.push_provider()
self._provider_is_connected = True
self._connect()

def _connect(self):
if self._provider_context._provider.network.is_mainnet:
# Ensure is not only running on tests on mainnet because
# was configured as the default.
is_from_command_line = (
"--network" in self.config_wrapper.pytest_config.invocation_params.args
)
if not is_from_command_line:
raise ConfigError(
"Default network is mainnet; unable to run tests on mainnet. "
"Please specify the network using the `--network` flag or "
"configure a different default network."
)

self._provider_context.push_provider()
self._provider_is_connected = True

def pytest_terminal_summary(self, terminalreporter):
"""
Expand Down
22 changes: 13 additions & 9 deletions tests/functional/test_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from ape.exceptions import ConfigError
from ape.pytest.plugin import _get_default_network
from ape.pytest.runners import PytestApeRunner
from ape_test import ApeTestConfig


Expand All @@ -15,17 +15,21 @@ def test_balance_set_from_currency_str(self):
assert actual == expected


def test_get_default_network(mocker):
# NOTE: Using this weird test to avoid actually
# using mainnet in any test, even accidentally.
mock_ecosystem = mocker.MagicMock()
mock_mainnet = mocker.MagicMock()
mock_mainnet.name = "mainnet"
mock_ecosystem.default_network = mock_mainnet
def test_connect_to_mainnet_by_default(mocker):
"""
Tests the condition where mainnet is configured as the default network
and no --network option is passed. It should avoid running the tests
to be safe.
"""

cfg = mocker.MagicMock()
cfg.network = "ethereum:mainnet:node"
runner = PytestApeRunner(cfg, mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock())

expected = (
"Default network is mainnet; unable to run tests on mainnet. "
"Please specify the network using the `--network` flag or "
"configure a different default network."
)
with pytest.raises(ConfigError, match=expected):
_get_default_network(mock_mainnet)
runner._connect()

0 comments on commit fee84bb

Please sign in to comment.