diff --git a/src/ape/api/networks.py b/src/ape/api/networks.py index 798e60e391..4d935d23ef 100644 --- a/src/ape/api/networks.py +++ b/src/ape/api/networks.py @@ -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: """ diff --git a/src/ape/pytest/plugin.py b/src/ape/pytest/plugin.py index 182dda1e9e..d470dbfe44 100644 --- a/src/ape/pytest/plugin.py +++ b/src/ape/pytest/plugin.py @@ -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 @@ -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: @@ -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( diff --git a/tests/functional/test_network_api.py b/tests/functional/test_network_api.py index 62d32e60b1..f7a2f6ea91 100644 --- a/tests/functional/test_network_api.py +++ b/tests/functional/test_network_api.py @@ -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 diff --git a/tests/functional/test_test.py b/tests/functional/test_test.py index 78b2d24930..9666170c56 100644 --- a/tests/functional/test_test.py +++ b/tests/functional/test_test.py @@ -1,3 +1,7 @@ +import pytest + +from ape.exceptions import ConfigError +from ape.pytest.plugin import _get_default_network from ape_test import ApeTestConfig @@ -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)