From 6f6177211916c2ca88b4d33881d3b5e3e555a0b6 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Thu, 17 Oct 2024 16:07:44 -0500 Subject: [PATCH] test: use ape test to test ape --- .github/workflows/test.yaml | 8 +-- pyproject.toml | 1 - tests/conftest.py | 52 ++++++--------- tests/functional/geth/test_contract.py | 4 +- tests/functional/test_accounts.py | 90 ++++++++++++-------------- tests/functional/test_cli.py | 18 +++--- tests/integration/cli/test_accounts.py | 4 +- tests/integration/cli/test_console.py | 5 +- 8 files changed, 82 insertions(+), 100 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 4128112885..236f005ecd 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -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 @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 338e5675f4..46ced0756d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 """ diff --git a/tests/conftest.py b/tests/conftest.py index 9c02bad3cd..32ec8cd9d5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -91,7 +91,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() @@ -135,7 +135,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 @@ -145,43 +146,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") @@ -190,18 +176,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") diff --git a/tests/functional/geth/test_contract.py b/tests/functional/geth/test_contract.py index b40cb53b43..0c4d6fba25 100644 --- a/tests/functional/geth/test_contract.py +++ b/tests/functional/geth/test_contract.py @@ -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) diff --git a/tests/functional/test_accounts.py b/tests/functional/test_accounts.py index b1db310813..e7a0a50534 100644 --- a/tests/functional/test_accounts.py +++ b/tests/functional/test_accounts.py @@ -20,7 +20,6 @@ ) from ape.types import AutoGasLimit from ape.types.signatures import recover_signer -from ape.utils.testing import DEFAULT_NUMBER_OF_TEST_ACCOUNTS from ape_accounts import ( KeyfileAccount, generate_account, @@ -362,7 +361,7 @@ def test_send_transaction_without_enough_funds_impersonated_account( ): address = "0x4838B106FCe9647Bdf1E7877BF73cE8B0BAD5f97" # Not a test account! impersonated_account = ImpersonatedAccount(raw_address=address) - accounts.test_accounts._impersonated_accounts[address] = impersonated_account + accounts._impersonated_accounts[address] = impersonated_account # Basically, it failed anywhere else besides the AccountsError you get from not # enough balance. @@ -376,23 +375,21 @@ def test_send_transaction_sets_defaults(sender, receiver): assert receipt.required_confirmations == 0 -def test_accounts_splice_access(test_accounts): - a, b = test_accounts[:2] - assert a == test_accounts[0] - assert b == test_accounts[1] - c = test_accounts[-1] - assert c == test_accounts[len(test_accounts) - 1] - expected = ( - (len(test_accounts) // 2) if len(test_accounts) % 2 == 0 else (len(test_accounts) // 2 + 1) - ) - assert len(test_accounts[::2]) == expected +def test_accounts_splice_access(accounts): + a, b = accounts[:2] + assert a == accounts[0] + assert b == accounts[1] + c = accounts[-1] + assert c == accounts[len(accounts) - 1] + expected = (len(accounts) // 2) if len(accounts) % 2 == 0 else (len(accounts) // 2 + 1) + assert len(accounts[::2]) == expected def test_accounts_address_access(owner, accounts): assert accounts[owner.address] == owner -def test_accounts_address_access_conversion_fail(accounts): +def test_accounts_address_access_conversion_fail(account_manager): with pytest.raises( KeyError, match=( @@ -400,7 +397,7 @@ def test_accounts_address_access_conversion_fail(accounts): r"Do you have the necessary conversion plugins installed?" ), ): - _ = accounts["FAILS"] + _ = account_manager["FAILS"] def test_accounts_address_access_not_found(accounts): @@ -409,15 +406,15 @@ def test_accounts_address_access_not_found(accounts): _ = accounts[address] -def test_test_accounts_address_access_conversion_fail(test_accounts): +def test_test_accounts_address_access_conversion_fail(accounts): with pytest.raises(KeyError, match=r"No account with ID 'FAILS'"): - _ = test_accounts["FAILS"] + _ = accounts["FAILS"] -def test_test_accounts_address_access_not_found(test_accounts): +def test_test_accounts_address_access_not_found(accounts): address = "0x1222262222222922222222222222222222222222" with pytest.raises(KeyError, match=rf"No account with address '{address}'\."): - _ = test_accounts[address] + _ = accounts[address] def test_accounts_contains(accounts, owner): @@ -459,9 +456,9 @@ def test_impersonated_account_ignores_signature_check_on_txn(accounts, address): account = ImpersonatedAccount(raw_address=address) # Impersonate hack, since no providers in core actually support it. - accounts.test_accounts._impersonated_accounts[address] = account - other_0 = accounts.test_accounts[8] - other_1 = accounts.test_accounts[9] + accounts._impersonated_accounts[address] = account + other_0 = accounts[8] + other_1 = accounts[9] txn = other_0.transfer(other_1, "1 gwei").transaction # Hack in fake sender. @@ -553,13 +550,13 @@ def test_unlock_with_wrong_passphrase_from_env(keyfile_account): assert keyfile_account.locked -def test_unlock_and_reload(runner, accounts, keyfile_account, message): +def test_unlock_and_reload(runner, account_manager, keyfile_account, message): """ Tests against a condition where reloading after unlocking would not honor unlocked state. """ keyfile_account.unlock(passphrase=PASSPHRASE) - reloaded_account = accounts.load(keyfile_account.alias) + reloaded_account = account_manager.load(keyfile_account.alias) # y: yes, sign (note: unlocking makes the key available but is not the same as autosign). with runner.isolation(input="y\n"): @@ -567,23 +564,20 @@ def test_unlock_and_reload(runner, accounts, keyfile_account, message): assert keyfile_account.check_signature(message, signature) -def test_custom_num_of_test_accounts_config(test_accounts, project): - custom_number_of_test_accounts = 20 +def test_custom_num_of_test_accounts_config(accounts, project): + custom_number_of_test_accounts = 25 test_config = { "test": { "number_of_accounts": custom_number_of_test_accounts, } } - - assert len(test_accounts) == DEFAULT_NUMBER_OF_TEST_ACCOUNTS - with project.temp_config(**test_config): - assert len(test_accounts) == custom_number_of_test_accounts + assert len(accounts) == custom_number_of_test_accounts -def test_test_accounts_repr(test_accounts): - actual = repr(test_accounts) - assert all(a.address in actual for a in test_accounts) +def test_test_accounts_repr(accounts): + actual = repr(accounts) + assert all(a.address in actual for a in accounts) def test_account_comparison_to_non_account(core_account): @@ -591,14 +585,14 @@ def test_account_comparison_to_non_account(core_account): assert core_account != "foo" -def test_create_account(test_accounts): - length_at_start = len(test_accounts) - created_account = test_accounts.generate_test_account() +def test_create_account(accounts): + length_at_start = len(accounts) + created_account = accounts.generate_test_account() assert isinstance(created_account, TestAccount) assert created_account.index == length_at_start - second_created_account = test_accounts.generate_test_account() + second_created_account = accounts.generate_test_account() assert created_account.address != second_created_account.address assert second_created_account.index == created_account.index + 1 @@ -627,42 +621,42 @@ def test_is_not_contract(owner, keyfile_account): assert not keyfile_account.is_contract -def test_using_different_hd_path(test_accounts, project, eth_tester_provider): +def test_using_different_hd_path(accounts, project, eth_tester_provider): test_config = { "test": { "hd_path": "m/44'/60'/0/0", } } - old_address = test_accounts[0].address + old_address = accounts[0].address original_settings = eth_tester_provider.settings.model_dump(by_alias=True) with project.temp_config(**test_config): eth_tester_provider.update_settings(test_config["test"]) - new_address = test_accounts[0].address + new_address = accounts[0].address eth_tester_provider.update_settings(original_settings) assert old_address != new_address -def test_using_random_mnemonic(test_accounts, project, eth_tester_provider): +def test_using_random_mnemonic(accounts, project, eth_tester_provider): mnemonic = "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat" test_config = {"test": {"mnemonic": mnemonic}} - old_address = test_accounts[0].address + old_address = accounts[0].address original_settings = eth_tester_provider.settings.model_dump(by_alias=True) with project.temp_config(**test_config): eth_tester_provider.update_settings(test_config["test"]) - new_address = test_accounts[0].address + new_address = accounts[0].address eth_tester_provider.update_settings(original_settings) assert old_address != new_address -def test_iter_test_accounts(test_accounts): - test_accounts.reset() - accounts = list(iter(test_accounts)) +def test_iter_test_accounts(accounts): + accounts.reset() + accounts = list(iter(accounts)) actual = len(accounts) - expected = len(test_accounts) + expected = len(accounts) assert actual == expected @@ -908,6 +902,6 @@ def test_import_account_from_private_key_insecure_passphrase(delete_account_afte import_account_from_private_key(simple_alias, "simple", PRIVATE_KEY) -def test_load(accounts, keyfile_account): - account = accounts.load(keyfile_account.alias) +def test_load(account_manager, keyfile_account): + account = account_manager.load(keyfile_account.alias) assert account == keyfile_account diff --git a/tests/functional/test_cli.py b/tests/functional/test_cli.py index 00c702bf64..68b006687a 100644 --- a/tests/functional/test_cli.py +++ b/tests/functional/test_cli.py @@ -109,21 +109,21 @@ def _teardown_numb_acct_change(accounts): @pytest.fixture -def no_accounts(accounts, empty_data_folder, project): - data = _setup_temp_acct_number_change(accounts, 0) +def no_accounts(account_manager, empty_data_folder, project): + data = _setup_temp_acct_number_change(account_manager, 0) with project.temp_config(**data): yield - _teardown_numb_acct_change(accounts) + _teardown_numb_acct_change(account_manager) @pytest.fixture -def one_account(accounts, empty_data_folder, project, test_accounts): - data = _setup_temp_acct_number_change(accounts, 1) +def one_account(account_manager, empty_data_folder, project): + data = _setup_temp_acct_number_change(account_manager, 1) with project.temp_config(**data): - yield test_accounts[0] + yield account_manager.test_accounts[0] - _teardown_numb_acct_change(accounts) + _teardown_numb_acct_change(account_manager) def get_expected_account_str(acct): @@ -371,9 +371,9 @@ def cmd(account): @pytest.mark.parametrize("test_key", ("test", "TEST")) -def test_account_option_can_use_test_account(runner, test_accounts, test_key): +def test_account_option_can_use_test_account(runner, accounts, test_key): index = 7 - test_account = test_accounts[index] + test_account = accounts[index] @click.command() @account_option() diff --git a/tests/integration/cli/test_accounts.py b/tests/integration/cli/test_accounts.py index f0bcce9486..a0087033e3 100644 --- a/tests/integration/cli/test_accounts.py +++ b/tests/integration/cli/test_accounts.py @@ -184,7 +184,7 @@ def test_import_mnemonic_custom_hdpath( @run_once -def test_export(ape_cli, runner, temp_keyfile, keyfile_account, test_accounts): +def test_export(ape_cli, runner, temp_keyfile, keyfile_account, accounts): # export key result = runner.invoke( ape_cli, @@ -196,7 +196,7 @@ def test_export(ape_cli, runner, temp_keyfile, keyfile_account, test_accounts): assert keyfile_account.address in result.output # NOTE: Both of these accounts are the same as the first # test account. - assert test_accounts[0].private_key in result.output + assert accounts[0].private_key in result.output @run_once diff --git a/tests/integration/cli/test_console.py b/tests/integration/cli/test_console.py index 647268284b..1913bc9fe0 100644 --- a/tests/integration/cli/test_console.py +++ b/tests/integration/cli/test_console.py @@ -1,3 +1,4 @@ +import subprocess from pathlib import Path import pytest @@ -241,6 +242,7 @@ def test_uncaught_txn_err(integ_project, ape_cli, runner, mocker): handler = mocker.patch("ape_console.plugin.handle_ape_exception") cmd_ls = [ "%load_ext ape_console.plugin", + "from ape import project, accounts", "account = accounts.test_accounts[0]", "contract = account.deploy(project.ContractA)", "receipt = contract.setNumber(5, sender=account)", @@ -249,12 +251,13 @@ def test_uncaught_txn_err(integ_project, ape_cli, runner, mocker): ] cmd_str = "\n".join(cmd_ls) arguments = ("console", "--project", f"{integ_project.path}") - runner.invoke( + result = runner.invoke( ape_cli, arguments, input=f"{cmd_str}\n", catch_exceptions=False, ) + assert handler.call_args, "handler not called." err = handler.call_args[0][0] assert str(err) == "Transaction failed."