From 70f187d67130540d06355ca74b115c5125280472 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 2 Aug 2022 16:41:06 -0400 Subject: [PATCH 1/3] add try/except for signer --- src/nile/core/account.py | 13 +++++++++++-- tests/commands/test_account.py | 12 ++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/nile/core/account.py b/src/nile/core/account.py index a1d8efe5..ccc22582 100644 --- a/src/nile/core/account.py +++ b/src/nile/core/account.py @@ -1,4 +1,5 @@ """Command to call or invoke StarkNet smart contracts.""" +import logging import os from dotenv import load_dotenv @@ -20,8 +21,16 @@ class Account: def __init__(self, signer, network): """Get or deploy an Account contract for the given private key.""" - self.signer = Signer(int(os.environ[signer])) - self.network = network + try: + self.signer = Signer(int(os.environ[signer])) + self.network = network + except KeyError: + logging.error( + f"\n❌ Cannot find {signer} in env." + "\nCheck spelling and that it exists." + "\nTry moving the .env to the directory outside of your project." + ) + return if accounts.exists(str(self.signer.public_key), network): signer_data = next(accounts.load(str(self.signer.public_key), network)) diff --git a/tests/commands/test_account.py b/tests/commands/test_account.py index ff21208b..961b0698 100644 --- a/tests/commands/test_account.py +++ b/tests/commands/test_account.py @@ -1,4 +1,5 @@ """Tests for account commands.""" +import logging from unittest.mock import MagicMock, patch import pytest @@ -27,6 +28,17 @@ def test_account_init(mock_deploy): mock_deploy.assert_called_once() +def test_account_init_bad_key(caplog): + logging.getLogger().setLevel(logging.INFO) + + Account("BAD_KEY", NETWORK) + assert ( + "\n❌ Cannot find BAD_KEY in env." + "\nCheck spelling and that it exists." + "\nTry moving the .env to the directory outside of your project." + ) in caplog.text + + def test_account_multiple_inits_with_same_key(): account = Account(KEY, NETWORK) account.deploy() From 57b38e96e95aa960bac12032cbd5fd7f44d5665a Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 3 Aug 2022 17:52:44 -0400 Subject: [PATCH 2/3] fix msg --- src/nile/core/account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nile/core/account.py b/src/nile/core/account.py index ccc22582..303d2aeb 100644 --- a/src/nile/core/account.py +++ b/src/nile/core/account.py @@ -28,7 +28,7 @@ def __init__(self, signer, network): logging.error( f"\n❌ Cannot find {signer} in env." "\nCheck spelling and that it exists." - "\nTry moving the .env to the directory outside of your project." + "\nTry moving the .env to the root of your project." ) return From c33a6d08433a869fb74c2ba73aea4059923475d1 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 3 Aug 2022 17:54:19 -0400 Subject: [PATCH 3/3] fix test --- tests/commands/test_account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/commands/test_account.py b/tests/commands/test_account.py index 961b0698..6efac1fa 100644 --- a/tests/commands/test_account.py +++ b/tests/commands/test_account.py @@ -35,7 +35,7 @@ def test_account_init_bad_key(caplog): assert ( "\n❌ Cannot find BAD_KEY in env." "\nCheck spelling and that it exists." - "\nTry moving the .env to the directory outside of your project." + "\nTry moving the .env to the root of your project." ) in caplog.text