Skip to content

Commit

Permalink
fix: bug preventing hyphenated plugin configs from working (#2104)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored May 31, 2024
1 parent 9c8dca1 commit c893851
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/ape/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def validate_model(cls, model):
if fixed_deps:
fixed_model["dependencies"] = fixed_deps

# field: contracs_folder: Handle if given Path object.
# field: contracts_folder: Handle if given Path object.
if "contracts_folder" in fixed_model and isinstance(fixed_model["contracts_folder"], Path):
fixed_model["contracts_folder"] = str(fixed_model["contracts_folder"])

Expand Down Expand Up @@ -330,9 +330,9 @@ def get_plugin_config(self, name: str) -> Optional[PluginConfig]:
# Already decoded.
return cfg

for plugin_name, config_class in self.plugin_manager.config_class:
for plugin_name, config_class in self._get_config_plugin_classes():
cls: type[PluginConfig] = config_class # type: ignore
if plugin_name != name:
if plugin_name.replace("-", "_") != name:
continue

if cls != ConfigDict:
Expand All @@ -347,6 +347,10 @@ def get_plugin_config(self, name: str) -> Optional[PluginConfig]:

return None

def _get_config_plugin_classes(self):
# NOTE: Abstracted for easily mocking in tests.
return self.plugin_manager.config_class

def get_custom_ecosystem_config(self, name: str) -> Optional[PluginConfig]:
name = name.replace("-", "_")
if not (networks := self.get_plugin_config("networks")):
Expand Down
34 changes: 33 additions & 1 deletion tests/functional/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ape.exceptions import ConfigError
from ape.managers.config import CONFIG_FILE_NAME, merge_configs
from ape.types import GasLimit
from ape_ethereum.ecosystem import NetworkConfig
from ape_ethereum.ecosystem import EthereumConfig, NetworkConfig
from ape_networks import CustomNetwork
from tests.functional.conftest import PROJECT_WITH_LONG_CONTRACTS_FOLDER

Expand Down Expand Up @@ -314,3 +314,35 @@ def test_custom_network():
assert network.name == "mytestnet"
assert network.chain_id == chain_id
assert network.ecosystem == "ethereum"


def test_get_config(config):
actual = config.get_config("ethereum")
assert isinstance(actual, EthereumConfig)


def test_get_config_hyphen_in_plugin_name(config):
"""
Tests against a bug noticed with ape-polygon-zkevm installed
on Ape 0.8.0 release where the config no longer worked.
"""

class CustomConfig(PluginConfig):
x: int = 123

mock_cfg_with_hyphens = CustomConfig
original_method = config.local_project.config._get_config_plugin_classes

# Hack in the fake plugin to test the behavior.
def hacked_in_method():
yield from [*list(original_method()), ("mock-plugin", mock_cfg_with_hyphens)]

config.local_project.config._get_config_plugin_classes = hacked_in_method

try:
cfg = config.get_config("mock-plugin")
assert isinstance(cfg, CustomConfig)
assert cfg.x == 123

finally:
config.local_project.config._get_config_plugin_classes = original_method
16 changes: 15 additions & 1 deletion tests/functional/test_ecosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,21 @@ def test_default_network_name_when_not_set_and_no_local_uses_only(

with project.temp_config(networks={"custom": [net]}):
ecosystem = project.network_manager.get_ecosystem(ecosystem_name)
assert ecosystem.default_network_name == only_network
actual = ecosystem.default_network_name

if actual == LOCAL_NETWORK_NAME:
# For some reason, this test is flake-y. Offer more info
# to try and debug when this happens (intermittent CI failure).
all_nets = ", ".join([x.name for x in ecosystem.networks])
pytest.fail(
f"assert '{LOCAL_NETWORK_NAME}' == '{only_network}'. More info below:\n"
f"ecosystem_name={ecosystem.name}\n"
f"networks={all_nets}\n"
f"type={type(ecosystem)}"
)
else:
# This should pass.
assert ecosystem.default_network_name == only_network


def test_decode_custom_error(chain, ethereum):
Expand Down

0 comments on commit c893851

Please sign in to comment.