-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from charles-cooper/native-imports
implement "native" imports for boa
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import contextlib | ||
import os | ||
import sys | ||
|
||
from boa.interpret import VyperDeployer | ||
|
||
|
||
@contextlib.contextmanager | ||
def mock_sys_path(path): | ||
anchor = sys.path | ||
try: | ||
sys.path = [path] | ||
yield | ||
finally: | ||
sys.path = anchor | ||
|
||
|
||
@contextlib.contextmanager | ||
def workdir(path): | ||
tmp = os.getcwd() | ||
try: | ||
os.chdir(path) | ||
with mock_sys_path("."): | ||
yield | ||
finally: | ||
os.chdir(tmp) | ||
|
||
|
||
def test_imports(tmp_path): | ||
code = """ | ||
totalSupply: public(uint256) | ||
@external | ||
def __init__(initial_supply: uint256): | ||
self.totalSupply = initial_supply | ||
""" | ||
|
||
filepath = tmp_path / "foo" / "bar.vy" | ||
filepath.parent.mkdir(parents=True) | ||
|
||
with filepath.open("w") as f: | ||
f.write(code) | ||
|
||
# note that `with mock_sys_path(tmp_path)` does not work here! | ||
# apparently, there are different semantics for module loading | ||
# depending on if we are in the current directory or not. | ||
with workdir(tmp_path): | ||
from foo import bar | ||
|
||
assert isinstance(bar, VyperDeployer) | ||
contract = bar.deploy(100) | ||
assert contract.totalSupply() == 100 | ||
|
||
from foo import bar as baz | ||
|
||
assert isinstance(baz, VyperDeployer) | ||
assert baz is bar |