Skip to content

Commit

Permalink
Fix issue displaying missing action errors when --help is specified (
Browse files Browse the repository at this point in the history
…linode#662)

Co-authored-by: ykim-1 <ykim@akamai.com>
  • Loading branch information
lgarber-akamai and ykim-akamai authored Nov 6, 2024
1 parent 360aec2 commit 12b8f25
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
1 change: 1 addition & 0 deletions linodecli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,5 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
if parsed.help:
print_help_action(cli, parsed.command, parsed.action)
sys.exit(ExitCodes.SUCCESS)

cli.handle_command(parsed.command, parsed.action, args)
2 changes: 1 addition & 1 deletion linodecli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def find_operation(self, command, action):
return op

# Fail if no matching alias was found
raise ValueError(f"No action {action} for command {command}")
raise ValueError(f"Action not found for command {command}: {action}")

@property
def user_agent(self) -> str:
Expand Down
1 change: 1 addition & 0 deletions linodecli/exit_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ class ExitCodes(IntEnum):
KUBECONFIG_ERROR = 6
ARGUMENT_ERROR = 7
FILE_ERROR = 8
UNRECOGNIZED_ACTION = 9
8 changes: 5 additions & 3 deletions linodecli/help_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
from rich.table import Column, Table
from rich.text import Text

from linodecli import plugins
from linodecli.baked import OpenAPIOperation
from linodecli.baked.request import OpenAPIRequestArg
from linodecli.exit_codes import ExitCodes
from linodecli.plugins import plugins

HELP_ENV_VARS = {
"LINODE_CLI_TOKEN": "A Linode Personal Access Token for the CLI to make requests with. "
Expand Down Expand Up @@ -181,8 +182,9 @@ def print_help_action(
"""
try:
op = cli.find_operation(command, action)
except ValueError:
return
except ValueError as exc:
print(exc, file=sys.stderr)
sys.exit(ExitCodes.UNRECOGNIZED_ACTION)

console = Console(highlight=False)

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_find_operation(
with pytest.raises(ValueError, match=r"Command not found: *"):
mock_cli.find_operation("bad", "list")

with pytest.raises(ValueError, match=r"No action *"):
with pytest.raises(ValueError, match=r"Action not found for command *"):
mock_cli.find_operation("foo", "cool")
mock_cli.find_operation("cool", "cool")

Expand Down
34 changes: 28 additions & 6 deletions tests/unit/test_help_pages.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import contextlib
from io import StringIO
from types import SimpleNamespace

from linodecli import help_pages
import pytest

from linodecli import CLI, help_pages
from linodecli.baked import OpenAPIOperation
from tests.unit.conftest import assert_contains_ordered_substrings


Expand Down Expand Up @@ -100,7 +104,7 @@ def test_help_with_ops(self, capsys, mocked_config):

def test_help_with_ops_with_plugins(self, capsys, mocker, mocked_config):
mocker.patch(
"linodecli.arg_helpers.plugins.available",
"linodecli.help_pages.plugins.available",
return_value=["testing.plugin"],
)
help_pages.print_help_plugins(mocked_config)
Expand All @@ -120,10 +124,28 @@ def test_help_topics(self, capsys):
assert topic in captured.out

# arg_helpers.print_help_action(cli, command, action)
def test_action_help_value_error(self, capsys, mock_cli):
help_pages.print_help_action(mock_cli, None, None)
captured = capsys.readouterr()
assert not captured.out
def test_action_help_value_error(
self, capsys, mock_cli: CLI, create_operation: OpenAPIOperation
):
mock_cli.ops = {
"foo": {
"bar": create_operation,
}
}

stderr_buf = StringIO()

with pytest.raises(SystemExit), contextlib.redirect_stderr(stderr_buf):
help_pages.print_help_action(mock_cli, "fake", "fake")

assert "Command not found: fake" in stderr_buf.getvalue()

stderr_buf = StringIO()

with pytest.raises(SystemExit), contextlib.redirect_stderr(stderr_buf):
help_pages.print_help_action(mock_cli, "foo", "fake")

assert "Action not found for command foo: fake" in stderr_buf.getvalue()

def test_action_help_post_method(self, capsys, mocker, mock_cli):
mocked_ops = mocker.MagicMock()
Expand Down

0 comments on commit 12b8f25

Please sign in to comment.