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

fix: issue where compile_code was creating a file #131

Merged
merged 4 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 6 additions & 3 deletions ape_vyper/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,15 +985,18 @@ def compile_code(
# Vyper in our tests because of the monkeypatch. Also, their approach
# isn't really different than our approach implemented below.
pm = project or self.local_project
with pm.isolate_in_tempdir():
with pm.isolate_in_tempdir() as tmp_project:
name = kwargs.get("contractName", "code")
file = pm.path / f"{name}.vy"
file = tmp_project.path / f"{name}.vy"
file.write_text(code, encoding="utf8")
contract_type = next(self.compile((file,), project=pm), None)
contract_type = next(self.compile((file,), project=tmp_project), None)
if contract_type is None:
# Not sure when this would happen.
raise VyperCompileError("Failed to produce contract type.")

# Clean-up (just in case tmp_project is re-used)
file.unlink(missing_ok=True)

return contract_type

def _source_vyper_version(self, code: str) -> Version:
Expand Down
16 changes: 13 additions & 3 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import re
from pathlib import Path
from typing import Optional

import ape
import pytest
import vvm # type: ignore
from ape.exceptions import CompilerError, ContractLogicError
from ape.types import SourceTraceback
from ape.utils import get_full_extension
from ethpm_types import ContractType
from packaging.version import Version
Expand Down Expand Up @@ -564,9 +566,13 @@ def test_enrich_error_handle_when_name(compiler, geth_provider, mocker):
which we are still able to enrich.
"""

tb = mocker.MagicMock()
tb.revert_type = "NONPAYABLE_CHECK"
error = ContractLogicError("", source_traceback=tb)
class TB(SourceTraceback):
@property
def revert_type(self) -> Optional[str]:
return "NONPAYABLE_CHECK"

tb = TB([{"statements": [], "closure": {"name": "fn"}, "depth": 0}]) # type: ignore
error = ContractLogicError(None, source_traceback=tb)
new_error = compiler.enrich_error(error)
assert isinstance(new_error, NonPayableError)

Expand Down Expand Up @@ -684,6 +690,10 @@ def test_compile_code(project, compiler, dev_revert_source):
assert len(actual.deployment_bytecode.bytecode) > 1
assert len(actual.runtime_bytecode.bytecode) > 1

# Ensure temp-file was deleted.
file = project.path / "MyContract.vy"
assert not file.is_file()


def test_compile_with_version_set_in_settings_dict(config, compiler_manager, projects_path):
path = projects_path / "version_in_config"
Expand Down
Loading