Skip to content

Commit

Permalink
fix: prevent running tests on mainnet if configured as the default ne…
Browse files Browse the repository at this point in the history
…twork (#2311)
  • Loading branch information
antazoey authored Oct 14, 2024
1 parent 6271dc5 commit 3f35535
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,17 @@ def explorer(self) -> Optional["ExplorerAPI"]:

return None # May not have an block explorer

@property
def is_mainnet(self) -> bool:
"""
True when the network is the mainnet network for the ecosystem.
"""
cfg_is_mainnet: Optional[bool] = self.config.get("is_mainnet")
if cfg_is_mainnet is not None:
return cfg_is_mainnet

return self.name == "mainnet"

@property
def is_fork(self) -> bool:
"""
Expand Down
20 changes: 19 additions & 1 deletion src/ape/pytest/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys
from pathlib import Path
from typing import Optional

from ape.api import EcosystemAPI
from ape.exceptions import ConfigError
from ape.pytest.config import ConfigWrapper
from ape.pytest.coverage import CoverageTracker
Expand All @@ -10,6 +12,22 @@
from ape.utils.basemodel import ManagerAccessMixin


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


def pytest_addoption(parser):
def add_option(*names, **kwargs):
try:
Expand All @@ -29,7 +47,7 @@ def add_option(*names, **kwargs):
add_option(
"--network",
action="store",
default=ManagerAccessMixin.network_manager.default_ecosystem.name,
default=_get_default_network(),
help="Override the default network and provider (see ``ape networks list`` for options).",
)
add_option(
Expand Down
24 changes: 24 additions & 0 deletions tests/functional/test_network_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,27 @@ class MyForkProvider:
finally:
network._get_plugin_providers = orig
network.__dict__.pop("providers", None) # de-cache


def test_is_local(ethereum):
assert ethereum.local.is_local
assert not ethereum.mainnet.is_local
assert not ethereum.mainnet_fork.is_local


def test_is_fork(ethereum):
assert not ethereum.local.is_fork
assert not ethereum.mainnet.is_fork
assert ethereum.mainnet_fork.is_fork


def test_is_dev(ethereum):
assert ethereum.local.is_dev
assert not ethereum.mainnet.is_dev
assert ethereum.mainnet_fork.is_dev


def test_is_mainnet(ethereum):
assert not ethereum.local.is_mainnet
assert ethereum.mainnet.is_mainnet
assert not ethereum.mainnet_fork.is_mainnet
20 changes: 20 additions & 0 deletions tests/functional/test_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import pytest

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


Expand All @@ -9,3 +13,19 @@ def test_balance_set_from_currency_str(self):
actual = cfg.balance
expected = 10_000_000_000_000_000_000 # 10 ETH in WEI
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
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)

0 comments on commit 3f35535

Please sign in to comment.