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

chore: support vyper files for tests #2684

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
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
78 changes: 78 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import os
import pathlib
from functools import wraps

import pytest
Expand All @@ -9,7 +11,9 @@
from web3.providers.eth_tester import EthereumTesterProvider

from vyper import compiler
from vyper.cli.vyper_compile import compile_files
from vyper.codegen.lll_node import LLLnode
from vyper.exceptions import VyperException
from vyper.lll import compile_lll, optimizer

from .base_conftest import VyperContract, _get_contract, zero_gas_price_strategy
Expand Down Expand Up @@ -208,3 +212,77 @@ def _f(_addr, _salt, _initcode):
return keccak(prefix + addr + salt + keccak(initcode))[12:]

return _f


@pytest.fixture
def vyper_contract(request):

# Get the absolute path of the Vyper test file
caller_directory = os.path.split(request.fspath)[0]
contract_file_path = pathlib.Path(os.path.join(caller_directory, request.param))

with contract_file_path.open() as fh:
# trailing newline fixes python parsing bug when source ends in a comment
# https://bugs.python.org/issue35107
source_code = fh.read() + "\n"

custom_genesis = PyEVMBackend._generate_genesis_params(overrides={"gas_limit": 4500000})
custom_genesis["base_fee_per_gas"] = 0
backend = PyEVMBackend(genesis_parameters=custom_genesis)
tester = EthereumTester(backend=backend)
w3 = Web3(EthereumTesterProvider(tester))
w3.eth.set_gas_price_strategy(zero_gas_price_strategy)

contract = _get_contract(w3, source_code, "no_optimize")

return contract


def pytest_collect_file(parent, path):
if path.ext == ".vy" and path.basename.startswith("test"):
return VyperFile.from_parent(parent, fspath=path)


class VyperFile(pytest.File):
def collect(self):
filepath = str(self.fspath)
yield VyperTestItem.from_parent(self, name=filepath)


class VyperTestItem(pytest.Item):
def __init__(self, name, parent):
super().__init__(name, parent)
self.test_condition = self.name[:-3].split("_")[-1]

def runtest(self):
try:

compile_files(
[self.fspath],
("bytecode",),
)
self.test_result = "Success"

except VyperException as v:
self.test_result = v.__class__.__name__

except Exception:
self.test_result = "Fail"

if self.test_condition != self.test_result:
raise VyperTestException(self, self.name)

def repr_failure(self, excinfo):
if isinstance(excinfo.value, VyperTestException):
return (
f"Test failed : {self.name}\n"
f"Expected: {self.test_condition}\n"
f"Actual: {self.test_result}\n"
)

def reportinfo(self):
return self.fspath, 0, f"Vyper test file: {self.name}"


class VyperTestException(Exception):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VAL: constant(uint256)
1 change: 1 addition & 0 deletions tests/parser/syntax/constants/test_constant2_Success.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VAL: constant(uint256) = 123
5 changes: 5 additions & 0 deletions tests/parser/syntax/constants/test_constantGetter1_Success.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
VAL: constant(uint256) = 123

@external
def foo() -> uint256:
return VAL
7 changes: 7 additions & 0 deletions tests/parser/syntax/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,10 @@ def deposit(deposit_input: Bytes[2048]):
@pytest.mark.parametrize("good_code", valid_list)
def test_constant_success(good_code):
assert compiler.compile_code(good_code) is not None


@pytest.mark.parametrize(
"vyper_contract", ["constants/test_constantGetter1_Success.vy"], indirect=True
)
def test_constant_success_2(vyper_contract):
assert vyper_contract.foo() == 123
Copy link
Member

Choose a reason for hiding this comment

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

Oh, this is clever. Basically, reuse compiled "successful parsing" test cases to additionally perform other checks?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that's correct. This is more deliberate as compared to automatically creating fixtures for "successful parsing" test cases (which I still cannot figure out, and on further thought could be more confusing if all these contracts appear in the global namespace).

Copy link
Member

Choose a reason for hiding this comment

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

Would the array of filenames passed in create multiple parametrized tests?

Also, some of the folders get really deep. I wonder if it could just assume a local path e.g. ./test_constantGetter1_Success.vy

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Would the array of filenames passed in create multiple parametrized tests?

Yes, I have updated the example to better demonstrate this.

Also, some of the folders get really deep. I wonder if it could just assume a local path e.g. ./test_constantGetter1_Success.vy

Do you mean that there will be some sort of collection process in the current directory and subdirectories to get the Vyper files based on the filename provided?