Skip to content

Commit

Permalink
fix: handle project refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed May 28, 2024
1 parent 2cbe3a7 commit 80f1892
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repos:
rev: 0.7.17
hooks:
- id: mdformat
additional_dependencies: [mdformat-gfm, mdformat-frontmatter]
additional_dependencies: [mdformat-gfm, mdformat-frontmatter, mdformat-pyproject]

default_language_version:
python: python3
2 changes: 1 addition & 1 deletion tests/ape-config.yaml → ape-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
contracts_folder: data/contracts/ethereum/local
contracts_folder: tests/data/contracts/ethereum/local

ethereum:
mainnet:
Expand Down
5 changes: 0 additions & 5 deletions ape_foundry/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import shutil
from bisect import bisect_right
from copy import copy
from pathlib import Path
from subprocess import PIPE, call
from typing import Literal, Optional, Union, cast

Expand Down Expand Up @@ -181,10 +180,6 @@ def anvil_bin(self) -> str:

return anvil

@property
def project_folder(self) -> Path:
return self.config_manager.PROJECT_FOLDER

@property
def uri(self) -> str:
if self._host is not None:
Expand Down
42 changes: 6 additions & 36 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import json
import os
import tempfile
import shutil
from contextlib import contextmanager
from pathlib import Path
from tempfile import mkdtemp
from typing import Dict, Optional

import ape
import pytest
import yaml
from _pytest.runner import pytest_runtest_makereport as orig_pytest_runtest_makereport
from ape.contracts import ContractContainer
from ape.exceptions import APINotImplementedError, UnknownSnapshotError
from ape.managers.config import CONFIG_FILE_NAME
from ethpm_types import ContractType

from ape_foundry import FoundryProvider

# NOTE: Ensure that we don't use local paths for the DATA FOLDER
ape.config.DATA_FOLDER = Path(mkdtemp()).resolve()
DATA_FOLDER = Path(mkdtemp()).resolve()
ape.config.DATA_FOLDER = DATA_FOLDER

BASE_CONTRACTS_PATH = Path(__file__).parent / "data" / "contracts"
LOCAL_CONTRACTS_PATH = BASE_CONTRACTS_PATH / "ethereum" / "local"
Expand All @@ -45,9 +42,9 @@ def name():


@pytest.fixture(scope="session", autouse=True)
def in_tests_dir(config):
with config.using_project(Path(__file__).parent):
yield
def clean_datafodler(config):
yield # Run all collected tests.
shutil.rmtree(DATA_FOLDER, ignore_errors=True)


@contextmanager
Expand Down Expand Up @@ -242,33 +239,6 @@ def sepolia_fork_provider(name, ethereum, sepolia_fork_port):
yield provider


@pytest.fixture(scope="session")
def temp_config(config):
@contextmanager
def func(data: Dict, package_json: Optional[Dict] = None):
# TODO: Use `ape.utils.use_tempdir()` (once released)
with tempfile.TemporaryDirectory() as temp_dir_str:
temp_dir = Path(temp_dir_str).resolve()

config._cached_configs = {}
config_file = temp_dir / CONFIG_FILE_NAME
config_file.touch()
config_file.write_text(yaml.dump(data))
config.load(force_reload=True)

if package_json:
package_json_file = temp_dir / "package.json"
package_json_file.write_text(json.dumps(package_json))

with config.using_project(temp_dir):
yield temp_dir

config_file.unlink()
config._cached_configs = {}

return func


@pytest.fixture
def contract_a(owner, connected_provider, get_contract_type):
contract_c = owner.deploy(ContractContainer(get_contract_type("contract_c")))
Expand Down
10 changes: 3 additions & 7 deletions tests/test_fork_provider.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import tempfile
from pathlib import Path

import pytest
Expand Down Expand Up @@ -78,17 +77,14 @@ def test_mainnet_impersonate(accounts, mainnet_fork_provider):


@pytest.mark.fork
def test_request_timeout(networks, config, mainnet_fork_provider):
def test_request_timeout(networks, project, mainnet_fork_provider):
actual = mainnet_fork_provider.web3.provider._request_kwargs["timeout"]
expected = 360 # Value set in `ape-config.yaml`
assert actual == expected

# Test default behavior
# TODO: Use `ape.utils.use_tempdir()` (once released)
with tempfile.TemporaryDirectory() as temp_dir_str:
temp_dir = Path(temp_dir_str).resolve()
with config.using_project(temp_dir):
assert networks.active_provider.timeout == 300
with project.temp_config(foundry={"fork_request_timeout": 300}):
assert networks.active_provider.timeout == 300


@pytest.mark.fork
Expand Down
44 changes: 17 additions & 27 deletions tests/test_provider.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os
import tempfile
from pathlib import Path

import pytest
from ape.api import TraceAPI
Expand Down Expand Up @@ -154,18 +152,15 @@ def test_get_transaction_trace_call_tree(connected_provider, sender, receiver):
assert repr(trace) == "__ETH_transfer__.0x() 1"


def test_request_timeout(connected_provider, config):
def test_request_timeout(connected_provider, project):
# Test value set in `ape-config.yaml`
expected = 29
actual = connected_provider.web3.provider._request_kwargs["timeout"]
assert actual == expected

# Test default behavior
# TODO: Use `ape.utils.use_tempdir()` (once released)
with tempfile.TemporaryDirectory() as temp_dir_str:
temp_dir = Path(temp_dir_str).resolve()
with config.using_project(temp_dir):
assert connected_provider.timeout == 30
with project.temp_config(foundry={"timeout": 30}):
assert connected_provider.timeout == 30


def test_contract_interaction(connected_provider, owner, contract_instance, mocker):
Expand Down Expand Up @@ -274,23 +269,22 @@ def test_revert_error_using_impersonated_account(error_contract, accounts, conne


@pytest.mark.parametrize("host", ("https://example.com", "example.com"))
def test_host(temp_config, local_network, host):
data = {"foundry": {"host": host}}
with temp_config(data):
def test_host(project, local_network, host):
with project.temp_config(foundry={"host": host}):
provider = local_network.get_provider("foundry")
assert provider.uri == "https://example.com"


def test_base_fee(connected_provider, temp_config, networks, accounts):
def test_base_fee(connected_provider, project, networks, accounts):
assert connected_provider.base_fee == 0

acct1 = accounts[-1]
acct2 = accounts[-2]

# Show we can se the base-fee.
new_base_fee = 1_000_000
data = {"foundry": {"base_fee": new_base_fee, "host": "http://127.0.0.1:8555"}}
with temp_config(data):
data = {"base_fee": new_base_fee, "host": "http://127.0.0.1:8555"}
with project.temp_config(foundry=data):
with networks.ethereum.local.use_provider("foundry") as provider:
# Verify the block has the right base fee
block_one = provider.get_block("latest")
Expand Down Expand Up @@ -338,28 +332,25 @@ def test_get_virtual_machine_error_from_contract_logic_message_includes_base_err
assert actual.base_err == exception


def test_no_mining(temp_config, local_network, connected_provider):
def test_no_mining(project, local_network, connected_provider):
assert "--no-mining" not in connected_provider.build_command()
data = {"foundry": {"auto_mine": "false"}}
with temp_config(data):
with project.temp_config(foundry={"auto_mine": "false"}):
provider = local_network.get_provider("foundry")
cmd = provider.build_command()
assert "--no-mining" in cmd


def test_block_time(temp_config, local_network, connected_provider):
def test_block_time(project, local_network, connected_provider):
assert "--block-time" not in connected_provider.build_command()
data = {"foundry": {"block_time": 10}}
with temp_config(data):
with project.temp_config(foundry={"block_time": 10}):
provider = local_network.get_provider("foundry")
cmd = provider.build_command()
assert "--block-time" in cmd
assert "10" in cmd


def test_remote_host(temp_config, local_network, no_anvil_bin):
data = {"foundry": {"host": "https://example.com"}}
with temp_config(data):
def test_remote_host(project, local_network, no_anvil_bin):
with project.temp_config(foundry={"host": "https://example.com"}):
with pytest.raises(
FoundryProviderError,
match=r"Failed to connect to remote Anvil node at 'https://example.com'\.",
Expand All @@ -368,7 +359,7 @@ def test_remote_host(temp_config, local_network, no_anvil_bin):
assert True


def test_remote_host_using_env_var(temp_config, local_network, no_anvil_bin):
def test_remote_host_using_env_var(local_network, no_anvil_bin):
original = os.environ.get("APE_FOUNDRY_HOST")
os.environ["APE_FOUNDRY_HOST"] = "https://example2.com"

Expand Down Expand Up @@ -443,14 +434,13 @@ def test_prepare_tx_with_max_gas(tx_type, connected_provider, ethereum, owner):
assert actual.gas_limit == connected_provider.max_gas


def test_disable_block_gas_limit(temp_config, disconnected_provider):
def test_disable_block_gas_limit(project, disconnected_provider):
# Ensure it is disabled by default.
cmd = disconnected_provider.build_command()
assert "--disable-block-gas-limit" not in cmd

# Show we can enable it.
data = {"foundry": {"disable_block_gas_limit": True}}
with temp_config(data):
with project.temp_config(foundry={"disable_block_gas_limit": True}):
cmd = disconnected_provider.build_command()
assert "--disable-block-gas-limit" in cmd

Expand Down

0 comments on commit 80f1892

Please sign in to comment.