From fdba69d6b1c1a5be9813c9093862ecdfa06598f4 Mon Sep 17 00:00:00 2001 From: Eric Nordelo Date: Fri, 16 Sep 2022 12:30:22 +0200 Subject: [PATCH 1/3] feat: add gget nonce to nre and cli --- src/nile/cli.py | 9 +++++++++ src/nile/common.py | 18 ++---------------- src/nile/core/account.py | 2 +- src/nile/nre.py | 5 +++++ src/nile/utils/get_nonce.py | 25 +++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 src/nile/utils/get_nonce.py diff --git a/src/nile/cli.py b/src/nile/cli.py index 0853b3fb..b2b971d6 100755 --- a/src/nile/cli.py +++ b/src/nile/cli.py @@ -19,6 +19,7 @@ from nile.core.version import version as version_command from nile.utils.debug import debug as debug_command from nile.utils.get_accounts import get_accounts as get_accounts_command +from nile.utils.get_nonce import get_nonce as get_nonce_command logging.basicConfig(level=logging.DEBUG, format="%(message)s") @@ -239,6 +240,14 @@ def get_accounts(network): return get_accounts_command(network) +@cli.command() +@click.argument("contract_address") +@network_option +def get_nonce(contract_address, network): + """Retrieve the nonce for a contract.""" + return get_nonce_command(contract_address, network) + + cli = load_plugins(cli) diff --git a/src/nile/common.py b/src/nile/common.py index 0dec63aa..1dddc26e 100644 --- a/src/nile/common.py +++ b/src/nile/common.py @@ -15,7 +15,7 @@ RETRY_AFTER_SECONDS = 30 -def _get_gateway(): +def get_gateway(): """Get the StarkNet node details.""" try: with open(NODE_FILENAME, "r") as f: @@ -27,7 +27,7 @@ def _get_gateway(): f.write('{"localhost": "http://127.0.0.1:5050/"}') -GATEWAYS = _get_gateway() +GATEWAYS = get_gateway() def get_all_contracts(ext=None, directory=None): @@ -72,20 +72,6 @@ def run_command( return subprocess.check_output(command) -def get_nonce(contract_address, network): - """Get the current nonce for contract address in a given network.""" - command = ["starknet", "get_nonce", "--contract_address", contract_address] - - if network == "mainnet": - os.environ["STARKNET_NETWORK"] = "alpha-mainnet" - elif network == "goerli": - os.environ["STARKNET_NETWORK"] = "alpha-goerli" - else: - command.append(f"--feeder_gateway_url={GATEWAYS.get(network)}") - - return int(subprocess.check_output(command).strip()) - - def parse_information(x): """Extract information from deploy/declare command.""" # address is 64, tx_hash is 64 chars long diff --git a/src/nile/core/account.py b/src/nile/core/account.py index 3e769820..09b66e1d 100644 --- a/src/nile/core/account.py +++ b/src/nile/core/account.py @@ -5,9 +5,9 @@ from dotenv import load_dotenv from nile import accounts, deployments -from nile.common import get_nonce from nile.core.call_or_invoke import call_or_invoke from nile.core.deploy import deploy +from nile.utils.get_nonce import get_nonce try: from nile.signer import Signer diff --git a/src/nile/nre.py b/src/nile/nre.py index e9fc638b..902f2110 100644 --- a/src/nile/nre.py +++ b/src/nile/nre.py @@ -7,6 +7,7 @@ from nile.core.deploy import deploy from nile.core.plugins import get_installed_plugins, skip_click_exit from nile.utils.get_accounts import get_accounts +from nile.utils.get_nonce import get_nonce as get_nonce_command class NileRuntimeEnvironment: @@ -55,3 +56,7 @@ def get_or_deploy_account(self, signer): def get_accounts(self): """Retrieve and manage deployed accounts.""" return get_accounts(self.network) + + def get_nonce(self, contract_address): + """Retrieve the nonce for a contract.""" + return get_nonce_command(contract_address, self.network) diff --git a/src/nile/utils/get_nonce.py b/src/nile/utils/get_nonce.py new file mode 100644 index 00000000..b99450e9 --- /dev/null +++ b/src/nile/utils/get_nonce.py @@ -0,0 +1,25 @@ +"""Retrieve and manage deployed accounts.""" +import logging +import os +import subprocess + +from nile.common import get_gateway + +GATEWAYS = get_gateway() + + +def get_nonce(contract_address, network): + """Get the current nonce for contract address in a given network.""" + command = ["starknet", "get_nonce", "--contract_address", contract_address] + + if network == "mainnet": + os.environ["STARKNET_NETWORK"] = "alpha-mainnet" + elif network == "goerli": + os.environ["STARKNET_NETWORK"] = "alpha-goerli" + else: + command.append(f"--feeder_gateway_url={GATEWAYS.get(network)}") + + nonce = int(subprocess.check_output(command).strip()) + + logging.info(f"\nCurrent nonce for {contract_address} is {nonce}") + return nonce From 755e62dd2492969d3305f453d8f92161fde4a159 Mon Sep 17 00:00:00 2001 From: Eric Nordelo Date: Mon, 19 Sep 2022 16:27:32 +0200 Subject: [PATCH 2/3] refactor: change the project structure --- src/nile/cli.py | 28 +++++++++---------- src/nile/core/account.py | 12 ++++---- .../core/{ => commands}/call_or_invoke.py | 3 +- src/nile/core/{ => commands}/clean.py | 2 +- src/nile/core/{ => commands}/compile.py | 2 +- src/nile/{utils => core/commands}/debug.py | 2 +- src/nile/core/{ => commands}/declare.py | 8 ++++-- src/nile/core/{ => commands}/deploy.py | 9 ++++-- .../{utils => core/commands}/get_accounts.py | 2 +- .../{utils => core/commands}/get_nonce.py | 2 +- src/nile/core/{ => commands}/init.py | 2 +- src/nile/core/{ => commands}/install.py | 0 src/nile/core/{ => commands}/node.py | 2 +- src/nile/core/{ => commands}/run.py | 0 src/nile/core/{ => commands}/test.py | 2 +- src/nile/core/{ => commands}/version.py | 0 .../{common.py => core/common/__init__.py} | 0 src/nile/{ => core/common}/accounts.py | 2 +- src/nile/{ => core/common}/deployments.py | 2 +- src/nile/{ => core}/signer.py | 0 src/nile/nre.py | 14 +++++----- tests/commands/test_clean.py | 16 +++++------ tests/commands/test_compile.py | 12 ++++---- tests/commands/test_debug.py | 14 +++++----- tests/commands/test_declare.py | 14 +++++----- tests/commands/test_deploy.py | 8 +++--- tests/commands/test_get_accounts.py | 18 ++++++------ tests/commands/test_get_nonce.py | 0 tests/commands/test_node.py | 6 ++-- tests/test_cli.py | 6 ++-- tests/test_common.py | 4 +-- tests/test_signer.py | 2 +- 32 files changed, 101 insertions(+), 93 deletions(-) rename src/nile/core/{ => commands}/call_or_invoke.py (96%) rename src/nile/core/{ => commands}/clean.py (95%) rename src/nile/core/{ => commands}/compile.py (98%) rename src/nile/{utils => core/commands}/debug.py (99%) rename src/nile/core/{ => commands}/declare.py (89%) rename src/nile/core/{ => commands}/deploy.py (85%) rename src/nile/{utils => core/commands}/get_accounts.py (96%) rename src/nile/{utils => core/commands}/get_nonce.py (94%) rename src/nile/core/{ => commands}/init.py (95%) rename src/nile/core/{ => commands}/install.py (100%) rename src/nile/core/{ => commands}/node.py (96%) rename src/nile/core/{ => commands}/run.py (100%) rename src/nile/core/{ => commands}/test.py (96%) rename src/nile/core/{ => commands}/version.py (100%) rename src/nile/{common.py => core/common/__init__.py} (100%) rename src/nile/{ => core/common}/accounts.py (96%) rename src/nile/{ => core/common}/deployments.py (97%) rename src/nile/{ => core}/signer.py (100%) create mode 100644 tests/commands/test_get_nonce.py diff --git a/src/nile/cli.py b/src/nile/cli.py index b2b971d6..42605502 100755 --- a/src/nile/cli.py +++ b/src/nile/cli.py @@ -5,21 +5,21 @@ import click from nile.core.account import Account -from nile.core.call_or_invoke import call_or_invoke as call_or_invoke_command -from nile.core.clean import clean as clean_command -from nile.core.compile import compile as compile_command -from nile.core.declare import declare as declare_command -from nile.core.deploy import deploy as deploy_command -from nile.core.init import init as init_command -from nile.core.install import install as install_command -from nile.core.node import node as node_command +from nile.core.commands.call_or_invoke import call_or_invoke as call_or_invoke_command +from nile.core.commands.clean import clean as clean_command +from nile.core.commands.compile import compile as compile_command +from nile.core.commands.debug import debug as debug_command +from nile.core.commands.declare import declare as declare_command +from nile.core.commands.deploy import deploy as deploy_command +from nile.core.commands.get_accounts import get_accounts as get_accounts_command +from nile.core.commands.get_nonce import get_nonce as get_nonce_command +from nile.core.commands.init import init as init_command +from nile.core.commands.install import install as install_command +from nile.core.commands.node import node as node_command +from nile.core.commands.run import run as run_command +from nile.core.commands.test import test as test_command +from nile.core.commands.version import version as version_command from nile.core.plugins import load_plugins -from nile.core.run import run as run_command -from nile.core.test import test as test_command -from nile.core.version import version as version_command -from nile.utils.debug import debug as debug_command -from nile.utils.get_accounts import get_accounts as get_accounts_command -from nile.utils.get_nonce import get_nonce as get_nonce_command logging.basicConfig(level=logging.DEBUG, format="%(message)s") diff --git a/src/nile/core/account.py b/src/nile/core/account.py index 09b66e1d..e8c2c88d 100644 --- a/src/nile/core/account.py +++ b/src/nile/core/account.py @@ -1,16 +1,16 @@ -"""Command to call or invoke StarkNet smart contracts.""" +"""Account class abstraction.""" import logging import os from dotenv import load_dotenv -from nile import accounts, deployments -from nile.core.call_or_invoke import call_or_invoke -from nile.core.deploy import deploy -from nile.utils.get_nonce import get_nonce +from nile.core.commands.call_or_invoke import call_or_invoke +from nile.core.commands.deploy import deploy +from nile.core.commands.get_nonce import get_nonce +from nile.core.common import accounts, deployments try: - from nile.signer import Signer + from nile.core.signer import Signer except ImportError: pass diff --git a/src/nile/core/call_or_invoke.py b/src/nile/core/commands/call_or_invoke.py similarity index 96% rename from src/nile/core/call_or_invoke.py rename to src/nile/core/commands/call_or_invoke.py index 2e98e59c..10f55314 100644 --- a/src/nile/core/call_or_invoke.py +++ b/src/nile/core/commands/call_or_invoke.py @@ -3,8 +3,7 @@ import os import subprocess -from nile import deployments -from nile.common import GATEWAYS, prepare_params +from nile.core.common import GATEWAYS, prepare_params, deployments def call_or_invoke( diff --git a/src/nile/core/clean.py b/src/nile/core/commands/clean.py similarity index 95% rename from src/nile/core/clean.py rename to src/nile/core/commands/clean.py index 83eda2d4..28f04d45 100644 --- a/src/nile/core/clean.py +++ b/src/nile/core/commands/clean.py @@ -3,7 +3,7 @@ import os import shutil -from nile.common import ( +from nile.core.common import ( ACCOUNTS_FILENAME, BUILD_DIRECTORY, DECLARATIONS_FILENAME, diff --git a/src/nile/core/compile.py b/src/nile/core/commands/compile.py similarity index 98% rename from src/nile/core/compile.py rename to src/nile/core/commands/compile.py index 31dfcd05..757dbd3e 100644 --- a/src/nile/core/compile.py +++ b/src/nile/core/commands/compile.py @@ -3,7 +3,7 @@ import os import subprocess -from nile.common import ( +from nile.core.common import ( ABIS_DIRECTORY, BUILD_DIRECTORY, CONTRACTS_DIRECTORY, diff --git a/src/nile/utils/debug.py b/src/nile/core/commands/debug.py similarity index 99% rename from src/nile/utils/debug.py rename to src/nile/core/commands/debug.py index ff93a56b..c1e7168f 100644 --- a/src/nile/utils/debug.py +++ b/src/nile/core/commands/debug.py @@ -7,7 +7,7 @@ import subprocess import time -from nile.common import ( +from nile.core.common import ( BUILD_DIRECTORY, DEPLOYMENTS_FILENAME, GATEWAYS, diff --git a/src/nile/core/declare.py b/src/nile/core/commands/declare.py similarity index 89% rename from src/nile/core/declare.py rename to src/nile/core/commands/declare.py index f6712c6c..1ff5467f 100644 --- a/src/nile/core/declare.py +++ b/src/nile/core/commands/declare.py @@ -1,8 +1,12 @@ """Command to declare StarkNet smart contracts.""" import logging -from nile import deployments -from nile.common import DECLARATIONS_FILENAME, parse_information, run_command +from nile.core.common import ( + DECLARATIONS_FILENAME, + deployments, + parse_information, + run_command, +) def declare(contract_name, network, alias=None, overriding_path=None): diff --git a/src/nile/core/deploy.py b/src/nile/core/commands/deploy.py similarity index 85% rename from src/nile/core/deploy.py rename to src/nile/core/commands/deploy.py index ce293668..958060b7 100644 --- a/src/nile/core/deploy.py +++ b/src/nile/core/commands/deploy.py @@ -1,8 +1,13 @@ """Command to deploy StarkNet smart contracts.""" import logging -from nile import deployments -from nile.common import ABIS_DIRECTORY, BUILD_DIRECTORY, parse_information, run_command +from nile.core.common import ( + ABIS_DIRECTORY, + BUILD_DIRECTORY, + deployments, + parse_information, + run_command, +) def deploy(contract_name, arguments, network, alias, overriding_path=None): diff --git a/src/nile/utils/get_accounts.py b/src/nile/core/commands/get_accounts.py similarity index 96% rename from src/nile/utils/get_accounts.py rename to src/nile/core/commands/get_accounts.py index adb4c2b5..04da1fc6 100644 --- a/src/nile/utils/get_accounts.py +++ b/src/nile/core/commands/get_accounts.py @@ -2,8 +2,8 @@ import json import logging -from nile.accounts import current_index from nile.core.account import Account +from nile.core.common.accounts import current_index def get_accounts(network): diff --git a/src/nile/utils/get_nonce.py b/src/nile/core/commands/get_nonce.py similarity index 94% rename from src/nile/utils/get_nonce.py rename to src/nile/core/commands/get_nonce.py index b99450e9..cb1db97f 100644 --- a/src/nile/utils/get_nonce.py +++ b/src/nile/core/commands/get_nonce.py @@ -3,7 +3,7 @@ import os import subprocess -from nile.common import get_gateway +from nile.core.common import get_gateway GATEWAYS = get_gateway() diff --git a/src/nile/core/init.py b/src/nile/core/commands/init.py similarity index 95% rename from src/nile/core/init.py rename to src/nile/core/commands/init.py index 85c65e3c..ae52659a 100644 --- a/src/nile/core/init.py +++ b/src/nile/core/commands/init.py @@ -5,7 +5,7 @@ from distutils.dir_util import copy_tree from pathlib import Path -from nile.core.install import install +from nile.core.commands.install import install def init(): diff --git a/src/nile/core/install.py b/src/nile/core/commands/install.py similarity index 100% rename from src/nile/core/install.py rename to src/nile/core/commands/install.py diff --git a/src/nile/core/node.py b/src/nile/core/commands/node.py similarity index 96% rename from src/nile/core/node.py rename to src/nile/core/commands/node.py index 127fa28d..22ad7990 100644 --- a/src/nile/core/node.py +++ b/src/nile/core/commands/node.py @@ -3,7 +3,7 @@ import logging import subprocess -from nile.common import NODE_FILENAME +from nile.core.common import NODE_FILENAME def node(host="127.0.0.1", port=5050, seed=None, lite_mode=False): diff --git a/src/nile/core/run.py b/src/nile/core/commands/run.py similarity index 100% rename from src/nile/core/run.py rename to src/nile/core/commands/run.py diff --git a/src/nile/core/test.py b/src/nile/core/commands/test.py similarity index 96% rename from src/nile/core/test.py rename to src/nile/core/commands/test.py index 20f74be2..0df9151a 100644 --- a/src/nile/core/test.py +++ b/src/nile/core/commands/test.py @@ -1,7 +1,7 @@ """Command to run Cairo tests written in Cairo.""" import asyncio -from nile.common import CONTRACTS_DIRECTORY, get_all_contracts +from nile.core.common import CONTRACTS_DIRECTORY, get_all_contracts def test(contracts): diff --git a/src/nile/core/version.py b/src/nile/core/commands/version.py similarity index 100% rename from src/nile/core/version.py rename to src/nile/core/commands/version.py diff --git a/src/nile/common.py b/src/nile/core/common/__init__.py similarity index 100% rename from src/nile/common.py rename to src/nile/core/common/__init__.py diff --git a/src/nile/accounts.py b/src/nile/core/common/accounts.py similarity index 96% rename from src/nile/accounts.py rename to src/nile/core/common/accounts.py index 76994a6e..6586acea 100644 --- a/src/nile/accounts.py +++ b/src/nile/core/common/accounts.py @@ -2,7 +2,7 @@ import json import os -from nile.common import ACCOUNTS_FILENAME +from nile.core.common import ACCOUNTS_FILENAME def register(pubkey, address, index, alias, network): diff --git a/src/nile/deployments.py b/src/nile/core/common/deployments.py similarity index 97% rename from src/nile/deployments.py rename to src/nile/core/common/deployments.py index fb0ae3eb..f5135904 100644 --- a/src/nile/deployments.py +++ b/src/nile/core/common/deployments.py @@ -2,7 +2,7 @@ import logging import os -from nile.common import DECLARATIONS_FILENAME, DEPLOYMENTS_FILENAME +from nile.core.common import DECLARATIONS_FILENAME, DEPLOYMENTS_FILENAME def register(address, abi, network, alias): diff --git a/src/nile/signer.py b/src/nile/core/signer.py similarity index 100% rename from src/nile/signer.py rename to src/nile/core/signer.py diff --git a/src/nile/nre.py b/src/nile/nre.py index 902f2110..5a4880e0 100644 --- a/src/nile/nre.py +++ b/src/nile/nre.py @@ -1,13 +1,13 @@ """nile runtime environment.""" -from nile import deployments from nile.core.account import Account -from nile.core.call_or_invoke import call_or_invoke -from nile.core.compile import compile -from nile.core.declare import declare -from nile.core.deploy import deploy +from nile.core.commands.call_or_invoke import call_or_invoke +from nile.core.commands.compile import compile +from nile.core.commands.declare import declare +from nile.core.commands.deploy import deploy +from nile.core.commands.get_accounts import get_accounts +from nile.core.commands.get_nonce import get_nonce as get_nonce_command from nile.core.plugins import get_installed_plugins, skip_click_exit -from nile.utils.get_accounts import get_accounts -from nile.utils.get_nonce import get_nonce as get_nonce_command +from nile.core.common import deployments class NileRuntimeEnvironment: diff --git a/tests/commands/test_clean.py b/tests/commands/test_clean.py index 1cf76b47..2fd9795a 100644 --- a/tests/commands/test_clean.py +++ b/tests/commands/test_clean.py @@ -4,13 +4,13 @@ import pytest -from nile.common import ( +from nile.core.commands.clean import clean +from nile.core.common import ( ACCOUNTS_FILENAME, BUILD_DIRECTORY, DECLARATIONS_FILENAME, DEPLOYMENTS_FILENAME, ) -from nile.core.clean import clean @pytest.fixture(autouse=True) @@ -19,8 +19,8 @@ def tmp_working_dir(monkeypatch, tmp_path): return tmp_path -@patch("nile.core.clean.shutil.rmtree") -@patch("nile.core.clean.os.remove") +@patch("nile.core.commands.clean.shutil.rmtree") +@patch("nile.core.commands.clean.os.remove") def test_clean_already_clean(mock_os_remove, mock_shutil_rmtree): clean() mock_os_remove.assert_not_called() @@ -35,8 +35,8 @@ def test_clean_already_clean(mock_os_remove, mock_shutil_rmtree): f"localhost.{DECLARATIONS_FILENAME}", ], ) -@patch("nile.core.clean.shutil.rmtree") -@patch("nile.core.clean.os.remove") +@patch("nile.core.commands.clean.shutil.rmtree") +@patch("nile.core.commands.clean.os.remove") def test_clean_clean_files(mock_os_remove, mock_shutil_rmtree, fname): Path(fname).touch() clean() @@ -44,8 +44,8 @@ def test_clean_clean_files(mock_os_remove, mock_shutil_rmtree, fname): mock_shutil_rmtree.assert_not_called() -@patch("nile.core.clean.shutil.rmtree") -@patch("nile.core.clean.os.remove") +@patch("nile.core.commands.clean.shutil.rmtree") +@patch("nile.core.commands.clean.os.remove") def test_clean_clean_build_dir(mock_os_remove, mock_shutil_rmtree): Path(BUILD_DIRECTORY).mkdir() clean() diff --git a/tests/commands/test_compile.py b/tests/commands/test_compile.py index 4d7aee96..6bb13a67 100644 --- a/tests/commands/test_compile.py +++ b/tests/commands/test_compile.py @@ -9,8 +9,8 @@ import pytest -from nile.common import ABIS_DIRECTORY, CONTRACTS_DIRECTORY -from nile.core.compile import _compile_contract, compile +from nile.core.common import ABIS_DIRECTORY, CONTRACTS_DIRECTORY +from nile.core.commands.compile import _compile_contract, compile CONTRACT = "foo.cairo" @@ -25,7 +25,7 @@ def tmp_working_dir(monkeypatch, tmp_path): # shell commands. @pytest.fixture(autouse=True) def mock_subprocess(): - with patch("nile.core.compile.subprocess") as mock_subprocess: + with patch("nile.core.commands.compile.subprocess") as mock_subprocess: yield mock_subprocess @@ -35,7 +35,7 @@ def test_compile_create_abis_directory(tmp_working_dir): assert (tmp_working_dir / ABIS_DIRECTORY).exists() -@patch("nile.core.compile.get_all_contracts") +@patch("nile.core.commands.compile.get_all_contracts") def test_compile_get_all_contracts_called(mock_get_all_contracts): compile([]) mock_get_all_contracts.assert_called_once() @@ -45,7 +45,7 @@ def test_compile_get_all_contracts_called(mock_get_all_contracts): mock_get_all_contracts.assert_not_called() -@patch("nile.core.compile._compile_contract") +@patch("nile.core.commands.compile._compile_contract") def test_compile__compile_contract_called(mock__compile_contract): compile([CONTRACT]) mock__compile_contract.assert_called_once_with( @@ -53,7 +53,7 @@ def test_compile__compile_contract_called(mock__compile_contract): ) -@patch("nile.core.compile._compile_contract") +@patch("nile.core.commands.compile._compile_contract") def test_compile_failure_feedback(mock__compile_contract, caplog): # make logs visible to test logging.getLogger().setLevel(logging.INFO) diff --git a/tests/commands/test_debug.py b/tests/commands/test_debug.py index b3cd9271..35c6a1ed 100644 --- a/tests/commands/test_debug.py +++ b/tests/commands/test_debug.py @@ -6,8 +6,8 @@ import pytest -from nile.common import BUILD_DIRECTORY -from nile.utils.debug import _abi_to_build_path, _locate_error_lines_with_abis, debug +from nile.core.common import BUILD_DIRECTORY +from nile.core.commands.debug import _abi_to_build_path, _locate_error_lines_with_abis, debug MOCK_HASH = "0x1234" NETWORK = "goerli" @@ -41,11 +41,11 @@ def test__abi_to_build_path(): ([f"{DEBUG_ADDRESS}:{ABI_PATH}:{ALIAS}"], [int(DEBUG_ADDRESS, 16)]), ], ) -@patch("nile.utils.debug._abi_to_build_path", return_value=ABI_PATH) +@patch("nile.core.commands.debug._abi_to_build_path", return_value=ABI_PATH) def test__locate_error_lines_with_abis_with_and_without_alias( mock_path, file, address_set ): - with patch("nile.utils.debug.open") as mock_open: + with patch("nile.core.commands.debug.open") as mock_open: mock_open.return_value.__enter__.return_value = file return_array = _locate_error_lines_with_abis(MOCK_FILE, address_set, mock_path) # Values should be pushed into the array (and not return an error) @@ -53,11 +53,11 @@ def test__locate_error_lines_with_abis_with_and_without_alias( assert return_array == [f"{DEBUG_ADDRESS}:{ABI_PATH}"] -@patch("nile.utils.debug._abi_to_build_path", return_value=ABI_PATH) +@patch("nile.core.commands.debug._abi_to_build_path", return_value=ABI_PATH) def test__locate_error_lines_with_abis_misformatted_line(mock_path, caplog): logging.getLogger().setLevel(logging.INFO) - with patch("nile.utils.debug.open") as mock_open: + with patch("nile.core.commands.debug.open") as mock_open: # The DEBUG_ADDRESS alone without ":" is misformatted mock_open.return_value.__enter__.return_value = [DEBUG_ADDRESS] _locate_error_lines_with_abis(MOCK_FILE, int(DEBUG_ADDRESS, 16), mock_path) @@ -77,7 +77,7 @@ def test__locate_error_lines_with_abis_misformatted_line(mock_path, caplog): reason="Issue in cairo-lang. " "See https://github.com/starkware-libs/cairo-lang/issues/27", ) -@patch("nile.utils.debug.json.loads") +@patch("nile.core.commands.debug.json.loads") def test_debug_feedback_with_message(mock_json, caplog, args, expected): logging.getLogger().setLevel(logging.INFO) mock_json.return_value = mocked_json_message(args) diff --git a/tests/commands/test_declare.py b/tests/commands/test_declare.py index e8eb1726..8ef3f781 100644 --- a/tests/commands/test_declare.py +++ b/tests/commands/test_declare.py @@ -4,8 +4,8 @@ import pytest -from nile.common import DECLARATIONS_FILENAME -from nile.core.declare import alias_exists, declare +from nile.core.common import DECLARATIONS_FILENAME +from nile.core.commands.declare import alias_exists, declare @pytest.fixture(autouse=True) @@ -28,7 +28,7 @@ def test_alias_exists(): assert alias_exists(ALIAS, NETWORK) is False # when alias exists - with patch("nile.core.declare.deployments.load_class") as mock_load: + with patch("nile.core.commands.declare.deployments.load_class") as mock_load: mock_load.__iter__.side_effect = HASH assert alias_exists(ALIAS, NETWORK) is True @@ -53,9 +53,9 @@ def test_alias_exists(): ), ], ) -@patch("nile.core.declare.run_command", return_value=RUN_OUTPUT) -@patch("nile.core.declare.parse_information", return_value=[HASH, TX_HASH]) -@patch("nile.core.declare.deployments.register_class_hash") +@patch("nile.core.commands.declare.run_command", return_value=RUN_OUTPUT) +@patch("nile.core.commands.declare.parse_information", return_value=[HASH, TX_HASH]) +@patch("nile.core.commands.declare.deployments.register_class_hash") def test_declare( mock_register, mock_parse, mock_run_cmd, caplog, args, exp_command, exp_register ): @@ -76,7 +76,7 @@ def test_declare( assert f"๐Ÿงพ Transaction hash: {TX_HASH}" in caplog.text -@patch("nile.core.declare.alias_exists", return_value=True) +@patch("nile.core.commands.declare.alias_exists", return_value=True) def test_declare_duplicate_hash(mock_alias_check): with pytest.raises(Exception) as err: diff --git a/tests/commands/test_deploy.py b/tests/commands/test_deploy.py index 0e04bb9e..1ff4a86a 100644 --- a/tests/commands/test_deploy.py +++ b/tests/commands/test_deploy.py @@ -4,7 +4,7 @@ import pytest -from nile.core.deploy import ABIS_DIRECTORY, BUILD_DIRECTORY, deploy +from nile.core.commands.deploy import ABIS_DIRECTORY, BUILD_DIRECTORY, deploy @pytest.fixture(autouse=True) @@ -38,9 +38,9 @@ def tmp_working_dir(monkeypatch, tmp_path): ), ], ) -@patch("nile.core.deploy.run_command", return_value=RUN_OUTPUT) -@patch("nile.core.deploy.parse_information", return_value=[ADDRESS, TX_HASH]) -@patch("nile.core.deploy.deployments.register") +@patch("nile.core.commands.deploy.run_command", return_value=RUN_OUTPUT) +@patch("nile.core.commands.deploy.parse_information", return_value=[ADDRESS, TX_HASH]) +@patch("nile.core.commands.deploy.deployments.register") def test_deploy(mock_register, mock_parse, mock_run_cmd, caplog, args, exp_command): logging.getLogger().setLevel(logging.INFO) diff --git a/tests/commands/test_get_accounts.py b/tests/commands/test_get_accounts.py index 0205f844..ca8fd498 100644 --- a/tests/commands/test_get_accounts.py +++ b/tests/commands/test_get_accounts.py @@ -5,7 +5,7 @@ import pytest from nile.core.account import Account -from nile.utils.get_accounts import _check_and_return_account, get_accounts +from nile.core.commands.get_accounts import _check_and_return_account, get_accounts NETWORK = "goerli" PUBKEYS = [ @@ -38,7 +38,7 @@ def tmp_working_dir(monkeypatch, tmp_path): @pytest.fixture(autouse=True) def mock_subprocess(): - with patch("nile.core.compile.subprocess") as mock_subprocess: + with patch("nile.core.commands.compile.subprocess") as mock_subprocess: yield mock_subprocess @@ -85,9 +85,9 @@ def test_get_accounts_no_activated_accounts_feedback(capsys): ) -@patch("nile.utils.get_accounts.current_index", MagicMock(return_value=len(PUBKEYS))) -@patch("nile.utils.get_accounts.open", MagicMock()) -@patch("nile.utils.get_accounts.json.load", MagicMock(return_value=MOCK_ACCOUNTS)) +@patch("nile.core.commands.get_accounts.current_index", MagicMock(return_value=len(PUBKEYS))) +@patch("nile.core.commands.get_accounts.open", MagicMock()) +@patch("nile.core.commands.get_accounts.json.load", MagicMock(return_value=MOCK_ACCOUNTS)) def test_get_accounts_activated_accounts_feedback(caplog): logging.getLogger().setLevel(logging.INFO) @@ -105,13 +105,13 @@ def test_get_accounts_activated_accounts_feedback(caplog): assert "\n๐Ÿš€ Successfully retrieved deployed accounts" in caplog.text -@patch("nile.utils.get_accounts.current_index", MagicMock(return_value=len(PUBKEYS))) -@patch("nile.utils.get_accounts.open", MagicMock()) -@patch("nile.utils.get_accounts.json.load", MagicMock(return_value=MOCK_ACCOUNTS)) +@patch("nile.core.commands.get_accounts.current_index", MagicMock(return_value=len(PUBKEYS))) +@patch("nile.core.commands.get_accounts.open", MagicMock()) +@patch("nile.core.commands.get_accounts.json.load", MagicMock(return_value=MOCK_ACCOUNTS)) def test_get_accounts_with_keys(): with patch( - "nile.utils.get_accounts._check_and_return_account" + "nile.core.commands.get_accounts._check_and_return_account" ) as mock_return_account: result = get_accounts(NETWORK) diff --git a/tests/commands/test_get_nonce.py b/tests/commands/test_get_nonce.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/commands/test_node.py b/tests/commands/test_node.py index 4e46b851..11c0ad3f 100644 --- a/tests/commands/test_node.py +++ b/tests/commands/test_node.py @@ -4,7 +4,7 @@ import pytest -from nile.core.node import node +from nile.core.commands.node import node HOST = "127.0.0.1" PORT = "5050" @@ -28,7 +28,7 @@ def tmp_working_dir(monkeypatch, tmp_path): ({"host": HOST, "port": PORT, "seed": SEED, "lite_mode": LITE}), ], ) -@patch("nile.core.node.subprocess.check_call") +@patch("nile.core.commands.node.subprocess.check_call") def test_node_call(mock_subprocess, args): node(**args) @@ -41,7 +41,7 @@ def test_node_call(mock_subprocess, args): mock_subprocess.assert_called_once_with(command) -@patch("nile.core.node.subprocess.check_call") +@patch("nile.core.commands.node.subprocess.check_call") def test_node_error(mock_subprocess, caplog): logging.getLogger().setLevel(logging.INFO) diff --git a/tests/test_cli.py b/tests/test_cli.py index e7b8a3f0..3449a27a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -17,7 +17,7 @@ from click.testing import CliRunner from nile.cli import cli -from nile.common import ( +from nile.core.common import ( ABIS_DIRECTORY, BUILD_DIRECTORY, CONTRACTS_DIRECTORY, @@ -110,7 +110,7 @@ def test_compile(args, expected): reason="Issue in cairo-lang. " "See https://github.com/starkware-libs/cairo-lang/issues/27", ) -@patch("nile.core.node.subprocess") +@patch("nile.core.commands.node.subprocess") def test_node_forwards_args(mock_subprocess): args = [ "--host", @@ -184,7 +184,7 @@ def test_node_runs_gateway(opts, expected): ([MOCK_HASH, "--network", "mainnet", "--contracts_file", "example.txt"]), ], ) -@patch("nile.utils.debug.subprocess") +@patch("nile.core.commands.debug.subprocess") def test_debug(mock_subprocess, args): # debug will hang without patch mock_subprocess.check_output.return_value = json.dumps({"tx_status": "ACCEPTED"}) diff --git a/tests/test_common.py b/tests/test_common.py index f8944ded..9bfbb02d 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -3,7 +3,7 @@ import pytest -from nile.common import BUILD_DIRECTORY, prepare_params, run_command, stringify +from nile.core.common import BUILD_DIRECTORY, prepare_params, run_command, stringify CONTRACT = "contract" OPERATION = "invoke" @@ -15,7 +15,7 @@ @pytest.mark.parametrize("operation", ["invoke", "call"]) -@patch("nile.common.subprocess.check_output") +@patch("nile.core.common.subprocess.check_output") def test_run_command(mock_subprocess, operation): run_command( diff --git a/tests/test_signer.py b/tests/test_signer.py index 9c8ae7cc..31e6c431 100644 --- a/tests/test_signer.py +++ b/tests/test_signer.py @@ -12,7 +12,7 @@ from starkware.starknet.services.api.gateway.transaction import InvokeFunction from starkware.starknet.testing.starknet import Starknet -from nile.signer import TRANSACTION_VERSION, Signer, from_call_to_call_array +from nile.core.signer import TRANSACTION_VERSION, Signer, from_call_to_call_array SIGNER = Signer(12345678987654321) From 1c86bef5fbb6154d2d453092f183aa4d31bc0d71 Mon Sep 17 00:00:00 2001 From: Eric Nordelo Date: Mon, 19 Sep 2022 16:31:30 +0200 Subject: [PATCH 3/3] refactor: format the code --- src/nile/core/commands/call_or_invoke.py | 2 +- src/nile/nre.py | 2 +- tests/commands/test_compile.py | 2 +- tests/commands/test_debug.py | 6 +++++- tests/commands/test_declare.py | 2 +- tests/commands/test_get_accounts.py | 18 ++++++++++++++---- tests/commands/test_get_nonce.py | 1 + 7 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/nile/core/commands/call_or_invoke.py b/src/nile/core/commands/call_or_invoke.py index 10f55314..4cb8467f 100644 --- a/src/nile/core/commands/call_or_invoke.py +++ b/src/nile/core/commands/call_or_invoke.py @@ -3,7 +3,7 @@ import os import subprocess -from nile.core.common import GATEWAYS, prepare_params, deployments +from nile.core.common import GATEWAYS, deployments, prepare_params def call_or_invoke( diff --git a/src/nile/nre.py b/src/nile/nre.py index 5a4880e0..5e6241c7 100644 --- a/src/nile/nre.py +++ b/src/nile/nre.py @@ -6,8 +6,8 @@ from nile.core.commands.deploy import deploy from nile.core.commands.get_accounts import get_accounts from nile.core.commands.get_nonce import get_nonce as get_nonce_command -from nile.core.plugins import get_installed_plugins, skip_click_exit from nile.core.common import deployments +from nile.core.plugins import get_installed_plugins, skip_click_exit class NileRuntimeEnvironment: diff --git a/tests/commands/test_compile.py b/tests/commands/test_compile.py index 6bb13a67..7627483c 100644 --- a/tests/commands/test_compile.py +++ b/tests/commands/test_compile.py @@ -9,8 +9,8 @@ import pytest -from nile.core.common import ABIS_DIRECTORY, CONTRACTS_DIRECTORY from nile.core.commands.compile import _compile_contract, compile +from nile.core.common import ABIS_DIRECTORY, CONTRACTS_DIRECTORY CONTRACT = "foo.cairo" diff --git a/tests/commands/test_debug.py b/tests/commands/test_debug.py index 35c6a1ed..98789440 100644 --- a/tests/commands/test_debug.py +++ b/tests/commands/test_debug.py @@ -6,8 +6,12 @@ import pytest +from nile.core.commands.debug import ( + _abi_to_build_path, + _locate_error_lines_with_abis, + debug, +) from nile.core.common import BUILD_DIRECTORY -from nile.core.commands.debug import _abi_to_build_path, _locate_error_lines_with_abis, debug MOCK_HASH = "0x1234" NETWORK = "goerli" diff --git a/tests/commands/test_declare.py b/tests/commands/test_declare.py index 8ef3f781..be4c750e 100644 --- a/tests/commands/test_declare.py +++ b/tests/commands/test_declare.py @@ -4,8 +4,8 @@ import pytest -from nile.core.common import DECLARATIONS_FILENAME from nile.core.commands.declare import alias_exists, declare +from nile.core.common import DECLARATIONS_FILENAME @pytest.fixture(autouse=True) diff --git a/tests/commands/test_get_accounts.py b/tests/commands/test_get_accounts.py index ca8fd498..f14ae93c 100644 --- a/tests/commands/test_get_accounts.py +++ b/tests/commands/test_get_accounts.py @@ -85,9 +85,14 @@ def test_get_accounts_no_activated_accounts_feedback(capsys): ) -@patch("nile.core.commands.get_accounts.current_index", MagicMock(return_value=len(PUBKEYS))) +@patch( + "nile.core.commands.get_accounts.current_index", + MagicMock(return_value=len(PUBKEYS)), +) @patch("nile.core.commands.get_accounts.open", MagicMock()) -@patch("nile.core.commands.get_accounts.json.load", MagicMock(return_value=MOCK_ACCOUNTS)) +@patch( + "nile.core.commands.get_accounts.json.load", MagicMock(return_value=MOCK_ACCOUNTS) +) def test_get_accounts_activated_accounts_feedback(caplog): logging.getLogger().setLevel(logging.INFO) @@ -105,9 +110,14 @@ def test_get_accounts_activated_accounts_feedback(caplog): assert "\n๐Ÿš€ Successfully retrieved deployed accounts" in caplog.text -@patch("nile.core.commands.get_accounts.current_index", MagicMock(return_value=len(PUBKEYS))) +@patch( + "nile.core.commands.get_accounts.current_index", + MagicMock(return_value=len(PUBKEYS)), +) @patch("nile.core.commands.get_accounts.open", MagicMock()) -@patch("nile.core.commands.get_accounts.json.load", MagicMock(return_value=MOCK_ACCOUNTS)) +@patch( + "nile.core.commands.get_accounts.json.load", MagicMock(return_value=MOCK_ACCOUNTS) +) def test_get_accounts_with_keys(): with patch( diff --git a/tests/commands/test_get_nonce.py b/tests/commands/test_get_nonce.py index e69de29b..52dc3093 100644 --- a/tests/commands/test_get_nonce.py +++ b/tests/commands/test_get_nonce.py @@ -0,0 +1 @@ +"""Tests for get-nonce command."""