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

Simplify paths for fetching contract classes in tests #377

Merged
merged 9 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ from utils import get_contract_class, cached_contract
@pytest.fixture(scope='module')
def foo_factory():
# get contract class
foo_cls = get_contract_class('path/to/foo.cairo')
foo_cls = get_contract_class('Foo')

# deploy contract
starknet = await Starknet.empty()
Expand Down
12 changes: 9 additions & 3 deletions docs/Utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,16 @@ Memoizing functions allow for quicker and computationally cheaper calculations w

### `get_contract_class`

A helper method that returns the contract class from the given path. To capture the contract class, simply add the contract path as an argument like this:
A helper method that returns the contract class from the contract's name. To capture the contract class, simply add the contract's name as an argument like this:

```python
contract_class = get_contract_class('path/to/contract.cairo')
contract_class = get_contract_class('ContractName')
```

If multiple contracts exist with the same name, then the contract's path must be used instead of the name. To pass the contract's path:
andrew-fleming marked this conversation as resolved.
Show resolved Hide resolved

```python
contract_class = get_contract_class('path/to/Contract.cairo', is_path=True)
```

### `cached_contract`
Expand All @@ -215,7 +221,7 @@ A helper method that returns the cached state of a given contract. It's recommen
# get contract classes
@pytest.fixture(scope='module')
def contract_classes():
foo_cls = get_contract_class('path/to/foo.cairo')
foo_cls = get_contract_class('Foo')
return foo_cls

# deploy contracts
Expand Down
4 changes: 2 additions & 2 deletions tests/access/test_AccessControl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def contract_classes():
return {
Path(key).stem: get_contract_class(key)
for key in [
'openzeppelin/account/Account.cairo',
'tests/mocks/AccessControl.cairo',
'Account',
'AccessControl',
]
}

Expand Down
4 changes: 2 additions & 2 deletions tests/access/test_Ownable.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
@pytest.fixture(scope='module')
def contract_classes():
return (
get_contract_class('openzeppelin/account/Account.cairo'),
get_contract_class('tests/mocks/Ownable.cairo')
get_contract_class('Account'),
get_contract_class('Ownable')
)


Expand Down
6 changes: 3 additions & 3 deletions tests/account/test_Account.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

@pytest.fixture(scope='module')
def contract_classes():
account_cls = get_contract_class('openzeppelin/account/Account.cairo')
init_cls = get_contract_class("tests/mocks/Initializable.cairo")
attacker_cls = get_contract_class("tests/mocks/account_reentrancy.cairo")
account_cls = get_contract_class('Account')
init_cls = get_contract_class("Initializable")
attacker_cls = get_contract_class("account_reentrancy")

return account_cls, init_cls, attacker_cls

Expand Down
4 changes: 2 additions & 2 deletions tests/account/test_AddressRegistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
@pytest.fixture(scope='module')
async def registry_factory():
# contract classes
registry_cls = get_contract_class("openzeppelin/account/AddressRegistry.cairo")
account_cls = get_contract_class('openzeppelin/account/Account.cairo')
registry_cls = get_contract_class("AddressRegistry")
account_cls = get_contract_class('Account')

# deployments
starknet = await Starknet.empty()
Expand Down
6 changes: 3 additions & 3 deletions tests/account/test_EthAccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

@pytest.fixture(scope='module')
def contract_defs():
account_cls = get_contract_class('openzeppelin/account/EthAccount.cairo')
init_cls = get_contract_class("tests/mocks/Initializable.cairo")
attacker_cls = get_contract_class("tests/mocks/account_reentrancy.cairo")
account_cls = get_contract_class('EthAccount')
init_cls = get_contract_class("Initializable")
attacker_cls = get_contract_class("account_reentrancy")

return account_cls, init_cls, attacker_cls

Expand Down
2 changes: 1 addition & 1 deletion tests/introspection/test_ERC165.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@pytest.fixture(scope='module')
async def erc165_factory():
# class
erc165_cls = get_contract_class("tests/mocks/ERC165.cairo")
erc165_cls = get_contract_class("tests/mocks/ERC165.cairo", is_path=True)

# deployment
starknet = await Starknet.empty()
Expand Down
6 changes: 4 additions & 2 deletions tests/security/test_initializable.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import pytest
from starkware.starknet.testing.starknet import Starknet
from utils import TRUE, FALSE, assert_revert, contract_path
from utils import TRUE, FALSE, assert_revert, get_contract_class
from signers import MockSigner

signer = MockSigner(12345678987654321)
Copy link
Contributor

Choose a reason for hiding this comment

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

why was this needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah it's not!


@pytest.mark.asyncio
async def test_initializer():
starknet = await Starknet.empty()
initializable = await starknet.deploy(
contract_path("tests/mocks/Initializable.cairo")
contract_class=get_contract_class("Initializable")
)
expected = await initializable.initialized().call()
assert expected.result == (FALSE,)
Expand Down
4 changes: 2 additions & 2 deletions tests/security/test_pausable.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
@pytest.fixture
async def pausable_factory():
# class
pausable_cls = get_contract_class("tests/mocks/Pausable.cairo")
account_cls = get_contract_class("openzeppelin/account/Account.cairo")
pausable_cls = get_contract_class("Pausable")
account_cls = get_contract_class("Account")

starknet = await Starknet.empty()
pausable = await starknet.deploy(
Expand Down
4 changes: 2 additions & 2 deletions tests/security/test_reentrancy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from starkware.starknet.testing.starknet import Starknet
from utils import (
assert_revert
assert_revert, get_contract_class
)

INITIAL_COUNTER = 0
Expand All @@ -11,7 +11,7 @@
async def reentrancy_mock():
starknet = await Starknet.empty()
contract = await starknet.deploy(
"tests/mocks/reentrancy_mock.cairo",
contract_class=get_contract_class("reentrancy_mock"),
Copy link
Contributor

Choose a reason for hiding this comment

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

how was this working?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Without the contract_class kwarg, you mean?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes

constructor_calldata=[INITIAL_COUNTER]
)

Expand Down
5 changes: 3 additions & 2 deletions tests/security/test_safemath.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
from starkware.starknet.testing.starknet import Starknet
from utils import (
MAX_UINT256, assert_revert, add_uint, sub_uint,
mul_uint, div_rem_uint, to_uint, contract_path
mul_uint, div_rem_uint, to_uint, contract_path,
get_contract_class
)


@pytest.fixture(scope='module')
async def safemath_mock():
starknet = await Starknet.empty()
safemath = await starknet.deploy(
contract_path("tests/mocks/safemath_mock.cairo")
contract_class=get_contract_class("safemath_mock")
)

return safemath
Expand Down
5 changes: 2 additions & 3 deletions tests/token/erc20/test_ERC20.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@

@pytest.fixture(scope='module')
def contract_classes():
account_cls = get_contract_class('openzeppelin/account/Account.cairo')
erc20_cls = get_contract_class(
'openzeppelin/token/erc20/ERC20.cairo')
account_cls = get_contract_class('Account')
erc20_cls = get_contract_class('ERC20')

return account_cls, erc20_cls

Expand Down
5 changes: 2 additions & 3 deletions tests/token/erc20/test_ERC20_Burnable_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

@pytest.fixture(scope='module')
def contract_classes():
account_cls = get_contract_class('openzeppelin/account/Account.cairo')
erc20_cls = get_contract_class(
'tests/mocks/ERC20_Burnable_mock.cairo')
account_cls = get_contract_class('Account')
erc20_cls = get_contract_class('ERC20_Burnable_mock')

return account_cls, erc20_cls

Expand Down
5 changes: 2 additions & 3 deletions tests/token/erc20/test_ERC20_Mintable.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@

@pytest.fixture(scope='module')
def contract_classes():
account_cls = get_contract_class('openzeppelin/account/Account.cairo')
erc20_cls = get_contract_class(
'openzeppelin/token/erc20/ERC20_Mintable.cairo')
account_cls = get_contract_class('Account')
erc20_cls = get_contract_class('ERC20_Mintable')

return account_cls, erc20_cls

Expand Down
5 changes: 2 additions & 3 deletions tests/token/erc20/test_ERC20_Pausable.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@

@pytest.fixture(scope='module')
def contract_classes():
account_cls = get_contract_class('openzeppelin/account/Account.cairo')
erc20_cls = get_contract_class(
'openzeppelin/token/erc20/ERC20_Pausable.cairo')
account_cls = get_contract_class('Account')
erc20_cls = get_contract_class('ERC20_Pausable')

return account_cls, erc20_cls

Expand Down
7 changes: 3 additions & 4 deletions tests/token/erc20/test_ERC20_Upgradeable.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@

@pytest.fixture(scope='module')
def contract_classes():
account_cls = get_contract_class('openzeppelin/account/Account.cairo')
token_cls = get_contract_class(
'openzeppelin/token/erc20/ERC20_Upgradeable.cairo')
proxy_cls = get_contract_class('openzeppelin/upgrades/Proxy.cairo')
account_cls = get_contract_class('Account')
token_cls = get_contract_class('ERC20_Upgradeable')
proxy_cls = get_contract_class('Proxy')

return account_cls, token_cls, proxy_cls

Expand Down
11 changes: 4 additions & 7 deletions tests/token/erc721/test_ERC721_Mintable_Burnable.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@

@pytest.fixture(scope='module')
def contract_classes():
account_cls = get_contract_class('openzeppelin/account/Account.cairo')
erc721_cls = get_contract_class(
'openzeppelin/token/erc721/ERC721_Mintable_Burnable.cairo')
erc721_holder_cls = get_contract_class(
'openzeppelin/token/erc721/utils/ERC721_Holder.cairo')
unsupported_cls = get_contract_class(
'tests/mocks/Initializable.cairo')
account_cls = get_contract_class('Account')
erc721_cls = get_contract_class('ERC721_Mintable_Burnable')
erc721_holder_cls = get_contract_class('ERC721_Holder')
unsupported_cls = get_contract_class('Initializable')
Copy link
Contributor

Choose a reason for hiding this comment

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

image


return account_cls, erc721_cls, erc721_holder_cls, unsupported_cls

Expand Down
8 changes: 3 additions & 5 deletions tests/token/erc721/test_ERC721_Mintable_Pausable.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

@pytest.fixture(scope='module')
def contract_classes():
account_cls = get_contract_class('openzeppelin/account/Account.cairo')
erc721_cls = get_contract_class(
'openzeppelin/token/erc721/ERC721_Mintable_Pausable.cairo')
erc721_holder_cls = get_contract_class(
'openzeppelin/token/erc721/utils/ERC721_Holder.cairo')
account_cls = get_contract_class('Account')
erc721_cls = get_contract_class('ERC721_Mintable_Pausable')
erc721_holder_cls = get_contract_class('ERC721_Holder')

return account_cls, erc721_cls, erc721_holder_cls

Expand Down
10 changes: 4 additions & 6 deletions tests/token/erc721/test_ERC721_SafeMintable_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@

@pytest.fixture(scope='module')
def contract_classes():
account_cls = get_contract_class('openzeppelin/account/Account.cairo')
erc721_cls = get_contract_class('tests/mocks/ERC721_SafeMintable_mock.cairo')
erc721_holder_cls = get_contract_class(
'openzeppelin/token/erc721/utils/ERC721_Holder.cairo')
unsupported_cls = get_contract_class(
'tests/mocks/Initializable.cairo')
account_cls = get_contract_class('Account')
erc721_cls = get_contract_class('ERC721_SafeMintable_mock')
erc721_holder_cls = get_contract_class('ERC721_Holder')
unsupported_cls = get_contract_class('Initializable')

return account_cls, erc721_cls, erc721_holder_cls, unsupported_cls

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@

@pytest.fixture(scope='module')
def contract_classes():
account_cls = get_contract_class('openzeppelin/account/Account.cairo')
erc721_cls = get_contract_class(
'openzeppelin/token/erc721_enumerable/ERC721_Enumerable_Mintable_Burnable.cairo')
account_cls = get_contract_class('Account')
erc721_cls = get_contract_class('ERC721_Enumerable_Mintable_Burnable')

return account_cls, erc721_cls

Expand Down
8 changes: 3 additions & 5 deletions tests/upgrades/test_Proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@

@pytest.fixture(scope='module')
def contract_classes():
account_cls = get_contract_class('openzeppelin/account/Account.cairo')
implementation_cls = get_contract_class(
'tests/mocks/proxiable_implementation.cairo'
)
proxy_cls = get_contract_class('openzeppelin/upgrades/Proxy.cairo')
account_cls = get_contract_class('Account')
implementation_cls = get_contract_class('proxiable_implementation')
proxy_cls = get_contract_class('Proxy')

return account_cls, implementation_cls, proxy_cls

Expand Down
8 changes: 4 additions & 4 deletions tests/upgrades/test_upgrades.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

@pytest.fixture(scope='module')
def contract_classes():
account_cls = get_contract_class('openzeppelin/account/Account.cairo')
v1_cls = get_contract_class('tests/mocks/upgrades_v1_mock.cairo')
v2_cls = get_contract_class('tests/mocks/upgrades_v2_mock.cairo')
proxy_cls = get_contract_class('openzeppelin/upgrades/Proxy.cairo')
account_cls = get_contract_class('Account')
v1_cls = get_contract_class('upgrades_v1_mock')
v2_cls = get_contract_class('upgrades_v2_mock')
proxy_cls = get_contract_class('Proxy')

return account_cls, v1_cls, v2_cls, proxy_cls

Expand Down
23 changes: 20 additions & 3 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pathlib import Path
import math
import os
from starkware.starknet.public.abi import get_selector_from_name
from starkware.starknet.compiler.compile import compile_starknet_files
from starkware.starkware_utils.error_handling import StarkException
Expand Down Expand Up @@ -110,9 +111,25 @@ def assert_event_emitted(tx_exec_info, from_address, name, data):
) in tx_exec_info.raw_events


def get_contract_class(path):
"""Return the contract class from the contract path"""
path = contract_path(path)
def _get_path_from_name(name):
"""Return the contract path by contract name."""
dirs = ["src", "tests/mocks"]
for dir in dirs:
for (dirpath, _, filenames) in os.walk(dir):
for file in filenames:
if file == f"{name}.cairo":
return os.path.join(dirpath, file)

raise FileNotFoundError(f"Cannot find '{name}'.")


def get_contract_class(contract, is_path=False):
"""Return the contract class from the contract name or path"""
if is_path:
path = contract_path(contract)
else:
path = _get_path_from_name(contract)

contract_class = compile_starknet_files(
files=[path],
debug_info=True
Expand Down