Skip to content

Commit

Permalink
test: use ape test to test ape (#2327)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Oct 21, 2024
1 parent c695988 commit d43ea77
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 202 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ jobs:
pip install .[test]
- name: Run Functional Tests
run: pytest tests/functional -m "not fuzzing" -s --cov=src --cov-append -n auto --dist loadgroup
run: ape test tests/functional -m "not fuzzing" -s --cov=src --cov-append -n auto --dist loadgroup

- name: Run Integration Tests
run: pytest tests/integration -m "not fuzzing" -s --cov=src --cov-append -n auto --dist loadgroup
run: ape test tests/integration -m "not fuzzing" -s --cov=src --cov-append -n auto --dist loadgroup

- name: Run Performance Tests
run: pytest tests/performance -s
run: ape test tests/performance -s

fuzzing:
runs-on: ubuntu-latest
Expand All @@ -123,4 +123,4 @@ jobs:
pip install .[test]
- name: Run Tests
run: pytest -m "fuzzing" --no-cov -s
run: ape test -m "fuzzing" --no-cov -s
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ norecursedirs = "projects"
# NOTE: 'no:ape_test' Prevents the ape plugin from activating on our tests
# And 'pytest_ethereum' is not used and causes issues in some environments.
addopts = """
-p no:ape_test
-p no:pytest_ethereum
"""

Expand Down
107 changes: 30 additions & 77 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import ape
from ape.contracts import ContractContainer
from ape.exceptions import APINotImplementedError, ProviderNotConnectedError, UnknownSnapshotError
from ape.logging import LogLevel, logger
from ape.managers.project import Project
from ape.pytest.config import ConfigWrapper
Expand Down Expand Up @@ -91,7 +90,7 @@ def validate_cwd(start_dir):


@pytest.fixture
def project():
def example_project():
path = "tests/functional/data/contracts/ethereum/local"
with ape.project.temp_config(contracts_folder=path):
ape.project.clean()
Expand Down Expand Up @@ -135,7 +134,8 @@ def plugin_manager():


@pytest.fixture(scope="session")
def accounts():
def account_manager():
# NOTE: `accounts` fixture comes with ape_test as the test-accounts.
return ape.accounts


Expand All @@ -145,43 +145,28 @@ def compilers():


@pytest.fixture(scope="session")
def networks():
return ape.networks
def owner(accounts):
return accounts[0]


@pytest.fixture(scope="session")
def chain():
return ape.chain
def sender(accounts):
return accounts[1]


@pytest.fixture(scope="session")
def test_accounts(accounts):
return accounts.test_accounts
def receiver(accounts):
return accounts[2]


@pytest.fixture(scope="session")
def owner(test_accounts):
return test_accounts[0]
def not_owner(accounts):
return accounts[3]


@pytest.fixture(scope="session")
def sender(test_accounts):
return test_accounts[1]


@pytest.fixture(scope="session")
def receiver(test_accounts):
return test_accounts[2]


@pytest.fixture(scope="session")
def not_owner(test_accounts):
return test_accounts[3]


@pytest.fixture(scope="session")
def helper(test_accounts):
return test_accounts[4]
def helper(accounts):
return accounts[4]


@pytest.fixture(scope="session")
Expand All @@ -190,18 +175,18 @@ def mystruct_c():


@pytest.fixture
def signer(test_accounts):
return test_accounts[5]
def signer(accounts):
return accounts[5]


@pytest.fixture
def geth_account(test_accounts):
return test_accounts[6]
def geth_account(accounts):
return accounts[6]


@pytest.fixture
def geth_second_account(test_accounts):
return test_accounts[7]
def geth_second_account(accounts):
return accounts[7]


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -289,44 +274,6 @@ def geth_provider(networks):
yield networks.provider


@contextmanager
def _isolation():
if ape.networks.active_provider is None:
raise AssertionError("Isolation should only be used with a connected provider.")

init_network_name = ape.chain.provider.network.name
init_provider_name = ape.chain.provider.name

try:
snapshot = ape.chain.snapshot()
except (APINotImplementedError, ProviderNotConnectedError):
# Provider not used or connected in test.
snapshot = None

yield

if (
snapshot is None
or ape.networks.active_provider is None
or ape.chain.provider.network.name != init_network_name
or ape.chain.provider.name != init_provider_name
):
return

try:
ape.chain.restore(snapshot)
except (UnknownSnapshotError, ProviderNotConnectedError):
# Assume snapshot removed for testing reasons
# or the provider was not needed to be connected for the test.
pass


@pytest.fixture(autouse=True)
def eth_tester_isolation(eth_tester_provider):
with _isolation():
yield


@pytest.fixture
def empty_data_folder():
# Avoid user's global ape-config data.
Expand Down Expand Up @@ -527,11 +474,17 @@ def __init__(
self.root_cmd = root_cmd or []
self.data_folder = data_folder

def invoke(self, *subcommand: str, input=None, timeout: int = 40):
def invoke(
self,
*subcommand: str,
input=None,
timeout: int = 40,
env: Optional[dict] = None,
):
subcommand = subcommand or ()
cmd_ls = [*self.root_cmd, *subcommand]

env = dict(os.environ)
env = {**dict(os.environ), **(env or {})}
if self.data_folder:
env["APE_DATA_FOLDER"] = str(self.data_folder)

Expand Down Expand Up @@ -562,7 +515,7 @@ def __init__(
super().__init__([str(ape_path), *root], data_folder=data_folder)
self.project = None

def invoke(self, *subcommand: str, input=None, timeout: int = 40):
def invoke(self, *subcommand: str, input=None, timeout: int = 40, env: Optional[dict] = None):
if self.project:
try:
here = os.getcwd()
Expand All @@ -574,7 +527,7 @@ def invoke(self, *subcommand: str, input=None, timeout: int = 40):
else:
here = None

result = super().invoke(*subcommand, input=input, timeout=timeout)
result = super().invoke(*subcommand, input=input, timeout=timeout, env=env)
if here:
os.chdir(here)

Expand All @@ -591,7 +544,7 @@ def exit_code(self) -> int:

@property
def output(self) -> str:
return self._completed_process.stdout
return self._completed_process.stdout or self._completed_process.stderr


CUSTOM_NETWORK_0 = "apenet"
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,9 @@ def struct_input_for_call(owner):


@pytest.fixture(scope="session")
def output_from_struct_input_call(test_accounts):
def output_from_struct_input_call(accounts):
# Expected when using `struct_input_for_call`.
addr = test_accounts[0].address.replace("0x", "")
addr = accounts[0].address.replace("0x", "")
return HexBytes(
f"0x26e0a196000000000000000000000000{addr}000000000000000000000000000000000"
f"0000000000000000000000000000080000000000000000000000000000000000000000000"
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/geth/test_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def test_revert_out_of_gas_error_allow(geth_account, geth_second_account, geth_p


@geth_process_test
def test_revert_allow(test_accounts, geth_contract):
not_owner = test_accounts[0]
def test_revert_allow(accounts, geth_contract):
not_owner = accounts[0]

# 'sender' is not the owner so it will revert (with a message)
receipt = geth_contract.setNumber(100199, sender=not_owner, raise_on_revert=False)
Expand Down
Loading

0 comments on commit d43ea77

Please sign in to comment.