From c5453b21bad288b3a2662105e15c50dabd31f5c5 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Wed, 21 Aug 2024 15:57:12 -0500 Subject: [PATCH] fix: issue where account option didnt fail --- src/ape/cli/choices.py | 10 +++++++--- tests/functional/test_cli.py | 19 +++++++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/ape/cli/choices.py b/src/ape/cli/choices.py index 56ed20af52..e5ed9778b3 100644 --- a/src/ape/cli/choices.py +++ b/src/ape/cli/choices.py @@ -195,9 +195,13 @@ def convert( else: alias = value - if isinstance(alias, str) and alias.startswith("TEST::"): - idx_str = value.replace("TEST::", "") + if isinstance(alias, str) and alias.upper().startswith("TEST::"): + idx_str = alias.upper().replace("TEST::", "") if not idx_str.isnumeric(): + if alias in ManagerAccessMixin.account_manager.aliases: + # Was actually a similar-alias. + return ManagerAccessMixin.account_manager.load(alias) + self.fail(f"Cannot reference test account by '{value}'.", param=param) account_idx = int(idx_str) @@ -209,7 +213,7 @@ def convert( elif alias and alias in ManagerAccessMixin.account_manager.aliases: return ManagerAccessMixin.account_manager.load(alias) - return None + self.fail(f"Account with alias '{alias}' not found.", param=param) def print_choices(self): choices = dict(enumerate(self.choices, 0)) diff --git a/tests/functional/test_cli.py b/tests/functional/test_cli.py index 69a6ea3142..2f17950486 100644 --- a/tests/functional/test_cli.py +++ b/tests/functional/test_cli.py @@ -365,7 +365,8 @@ def cmd(account): assert expected in result.output -def test_account_option_can_use_test_account(runner, test_accounts): +@pytest.mark.parametrize("test_key", ("test", "TEST")) +def test_account_option_can_use_test_account(runner, test_accounts, test_key): index = 7 test_account = test_accounts[index] @@ -376,7 +377,21 @@ def cmd(account): click.echo(_expected) expected = get_expected_account_str(test_account) - result = runner.invoke(cmd, ("--account", f"TEST::{index}")) + result = runner.invoke(cmd, ("--account", f"{test_key}::{index}")) + assert expected in result.output + + +def test_account_option_alias_not_found(runner, keyfile_account): + @click.command() + @account_option() + def cmd(account): + pass + + result = runner.invoke(cmd, ("--account", "THIS ALAS IS NOT FOUND")) + expected = ( + "Invalid value for '--account': " + "Account with alias 'THIS ALAS IS NOT FOUND' not found" + ) assert expected in result.output