Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor[test]: change default expected exception type #4004

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from hexbytes import HexBytes

import vyper.evm.opcodes as evm_opcodes
from tests.evm_backends.base_env import BaseEnv, EvmError
from tests.evm_backends.base_env import BaseEnv, ExecutionReverted
from tests.evm_backends.pyevm_env import PyEvmEnv
from tests.evm_backends.revm_env import RevmEnv
from tests.utils import working_directory
Expand Down Expand Up @@ -314,7 +314,7 @@ def assert_side_effects_invoked(side_effects_contract, side_effects_trigger, n=1
@pytest.fixture(scope="module")
def tx_failed(env):
@contextmanager
def fn(exception=EvmError, exc_text=None):
def fn(exception=ExecutionReverted, exc_text=None):
with pytest.raises(exception) as excinfo:
yield

Expand Down
1 change: 1 addition & 0 deletions tests/evm_backends/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class BaseEnv:
"""

INVALID_OPCODE_ERROR = "NotImplemented" # must be implemented by subclasses
OUT_OF_GAS_ERROR = "NotImplemented" # must be implemented by subclasses
charles-cooper marked this conversation as resolved.
Show resolved Hide resolved
DEFAULT_CHAIN_ID = 1

def __init__(self, gas_limit: int, account_keys: list[PrivateKey]) -> None:
Expand Down
1 change: 1 addition & 0 deletions tests/evm_backends/pyevm_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class PyEvmEnv(BaseEnv):
"""EVM backend environment using the Py-EVM library."""

INVALID_OPCODE_ERROR = "Invalid opcode"
OUT_OF_GAS_ERROR = "Out of gas"

def __init__(
self,
Expand Down
1 change: 1 addition & 0 deletions tests/evm_backends/revm_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

class RevmEnv(BaseEnv):
INVALID_OPCODE_ERROR = "InvalidFEOpcode"
OUT_OF_GAS_ERROR = "OutOfGas"

def __init__(
self,
Expand Down
17 changes: 10 additions & 7 deletions tests/functional/builtins/codegen/test_abi_decode.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from eth.codecs import abi

from tests.evm_backends.base_env import EvmError, ExecutionReverted
from tests.utils import decimal_to_int
from vyper.exceptions import ArgumentException, StructureException

Expand Down Expand Up @@ -421,15 +422,17 @@ def abi_decode(x: Bytes[160]) -> uint256:


@pytest.mark.parametrize(
"output_typ1,output_typ2,input_",
"output_typ1,output_typ2,input_,error,error_property",
[
("DynArray[uint256, 3]", "uint256", b""),
("DynArray[uint256, 3]", "uint256", b"\x01" * 128),
("Bytes[5]", "address", b""),
("Bytes[5]", "address", b"\x01" * 128),
("DynArray[uint256, 3]", "uint256", b"", ExecutionReverted, ""),
("DynArray[uint256, 3]", "uint256", b"\x01" * 128, EvmError, "OUT_OF_GAS_ERROR"),
("Bytes[5]", "address", b"", ExecutionReverted, ""),
("Bytes[5]", "address", b"\x01" * 128, EvmError, "OUT_OF_GAS_ERROR"),
Comment on lines +427 to +430
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of scope but these should really all be ExecutionReverted actually; it should be fixed by #3925

],
)
def test_clamper_dynamic_tuple(get_contract, tx_failed, output_typ1, output_typ2, input_):
def test_clamper_dynamic_tuple(
get_contract, tx_failed, output_typ1, output_typ2, input_, error, error_property, env
):
contract = f"""
@external
def abi_decode(x: Bytes[224]) -> ({output_typ1}, {output_typ2}):
Expand All @@ -439,7 +442,7 @@ def abi_decode(x: Bytes[224]) -> ({output_typ1}, {output_typ2}):
return a, b
"""
c = get_contract(contract)
with tx_failed():
with tx_failed(error, exc_text=getattr(env, error_property, None)):
c.abi_decode(input_)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ def __init__():

assert c.x() == 123
assert env.get_balance(c.address) == 0
value = to_wei(0.1, "ether")
env.set_balance(env.deployer, value)
with tx_failed():
env.message_call(c.address, value=to_wei(0.1, "ether"), data=b"") # call default function
env.message_call(c.address, value=value, data=b"") # call default function
assert env.get_balance(c.address) == 0


Expand Down Expand Up @@ -70,6 +72,7 @@ def __default__():
log Sent(msg.sender)
"""
c = get_contract(code)
env.set_balance(env.deployer, 10**17)

with tx_failed():
env.message_call(c.address, value=10**17, data=b"") # call default function
Expand Down
3 changes: 3 additions & 0 deletions tests/functional/codegen/features/decorators/test_payable.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def baz() -> bool:
@pytest.mark.parametrize("code", nonpayable_code)
def test_nonpayable_runtime_assertion(env, keccak, tx_failed, get_contract, code):
c = get_contract(code)
env.set_balance(env.deployer, 10**18)

c.foo(value=0)
sig = keccak("foo()".encode()).hex()[:10]
Expand Down Expand Up @@ -371,6 +372,7 @@ def __default__():

c = get_contract(code)
env.message_call(c.address, value=0, data="0x12345678")
env.set_balance(env.deployer, 100)
with tx_failed():
env.message_call(c.address, value=100, data="0x12345678")

Expand All @@ -391,5 +393,6 @@ def __default__():
data = bytes([1, 2, 3, 4])
for i in range(5):
calldata = "0x" + data[:i].hex()
env.set_balance(env.deployer, 100)
with tx_failed():
env.message_call(c.address, value=100, data=calldata)
17 changes: 10 additions & 7 deletions tests/functional/codegen/features/test_assert_unreachable.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from tests.evm_backends.base_env import EvmError


def test_unreachable_refund(env, get_contract, tx_failed):
code = """
@external
Expand All @@ -8,7 +11,7 @@ def foo():

c = get_contract(code)
gas_sent = 10**6
with tx_failed():
with tx_failed(EvmError, exc_text=env.INVALID_OPCODE_ERROR):
c.foo(gas=gas_sent, gas_price=10)

assert env.last_result.gas_used == gas_sent # Drains all gas sent per INVALID opcode
Expand All @@ -27,11 +30,11 @@ def foo(val: int128) -> bool:

assert c.foo(2) is True

with tx_failed(exc_text=env.INVALID_OPCODE_ERROR):
with tx_failed(EvmError, exc_text=env.INVALID_OPCODE_ERROR):
c.foo(1)
with tx_failed(exc_text=env.INVALID_OPCODE_ERROR):
with tx_failed(EvmError, exc_text=env.INVALID_OPCODE_ERROR):
c.foo(-1)
with tx_failed(exc_text=env.INVALID_OPCODE_ERROR):
with tx_failed(EvmError, exc_text=env.INVALID_OPCODE_ERROR):
c.foo(-2)


Expand All @@ -53,9 +56,9 @@ def foo(val: int128) -> int128:

assert c.foo(33) == -123

with tx_failed(exc_text=env.INVALID_OPCODE_ERROR):
with tx_failed(EvmError, exc_text=env.INVALID_OPCODE_ERROR):
c.foo(1)
with tx_failed(exc_text=env.INVALID_OPCODE_ERROR):
with tx_failed(EvmError, exc_text=env.INVALID_OPCODE_ERROR):
c.foo(-1)


Expand All @@ -68,5 +71,5 @@ def foo():

c = get_contract(code)

with tx_failed(exc_text=env.INVALID_OPCODE_ERROR):
with tx_failed(EvmError, exc_text=env.INVALID_OPCODE_ERROR):
c.foo()
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_initial_state(market_maker):
def test_initiate(env, market_maker, erc20, tx_failed):
a0 = env.accounts[0]
ether, ethers = to_wei(1, "ether"), to_wei(2, "ether")
env.set_balance(a0, ethers)
env.set_balance(a0, ethers * 2)
erc20.approve(market_maker.address, ethers)
market_maker.initiate(erc20.address, ether, value=ethers)
assert market_maker.totalEthQty() == ethers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def get_balance():


def test_initial_state(env, tx_failed, get_contract, get_balance, contract_code):
env.set_balance(env.deployer, to_wei(2, "ether"))
# Initial deposit has to be divisible by two
with tx_failed():
get_contract(contract_code, value=13)
env.set_balance(env.deployer, to_wei(2, "ether"))
# Seller puts item up for sale
a0_pre_bal, a1_pre_bal = get_balance()
c = get_contract(contract_code, value=to_wei(2, "ether"))
Expand Down Expand Up @@ -73,8 +73,8 @@ def test_abort(env, tx_failed, get_balance, get_contract, contract_code):

def test_purchase(env, get_contract, tx_failed, get_balance, contract_code):
a0, a1, a2, a3 = env.accounts[:4]
env.set_balance(a0, 10**18)
env.set_balance(a1, 10**18)
for a in env.accounts[:4]:
env.set_balance(a, 10**18)

init_bal_a0, init_bal_a1 = get_balance()
c = get_contract(contract_code, value=2)
Expand Down
Loading