-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for extending cfn cli with custom commands (#1020)
* code: testing command extension * code: finishing ExtensionPlugin base class * code: removing unnecessary abstract class * code: renaming parameter * test: added unit tests for cli and extensions * refactor: renaming parameter * test: testing ExtensionPlugin * test: testing PluginRegistry * code: updated extension plugin to provider parser to plugin instead * isort: fixing isort * format: fixing black formatting * code: safety check for collisions --------- Co-authored-by: Adrian Chouza <[email protected]>
- Loading branch information
1 parent
74ed3d9
commit 61f7d71
Showing
8 changed files
with
181 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from .plugin_registry import get_extensions | ||
|
||
|
||
def _check_command_name_collision(subparsers, command_name): | ||
if command_name in subparsers.choices: | ||
raise RuntimeError( | ||
f'"{command_name}" is already registered as an extension. Please use a different name.' | ||
) | ||
|
||
|
||
def setup_subparsers(subparsers, parents): | ||
extensions = get_extensions() | ||
|
||
for extension_cls in extensions.values(): | ||
extension = extension_cls()() | ||
_check_command_name_collision(subparsers, extension.command_name) | ||
parser = subparsers.add_parser(extension.command_name, parents=parents) | ||
extension.setup_parser(parser) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import argparse | ||
from unittest import TestCase | ||
from unittest.mock import MagicMock, patch | ||
|
||
from rpdk.core.extensions import setup_subparsers | ||
|
||
|
||
class ExtensionTest(TestCase): | ||
def test_setup_subparsers(self): # pylint: disable=no-self-use | ||
expeted_command_name = "expected-command-name" | ||
|
||
mock_extension = MagicMock() | ||
mock_extension.command_name = expeted_command_name | ||
|
||
mock_extension_entry_point = MagicMock() | ||
mock_extension_entry_point.return_value.return_value = mock_extension | ||
|
||
mock_extension_entry_points = {"key": mock_extension_entry_point} | ||
|
||
subparsers, parents, parser = MagicMock(), MagicMock(), MagicMock() | ||
subparsers.add_parser.return_value = parser | ||
|
||
with patch("rpdk.core.extensions.get_extensions") as mock_get_extensions: | ||
mock_get_extensions.return_value = mock_extension_entry_points | ||
setup_subparsers(subparsers, parents) | ||
|
||
mock_extension.setup_parser.assert_called_once_with(parser) | ||
subparsers.add_parser.assert_called_with(expeted_command_name, parents=parents) | ||
|
||
def test_setup_subparsers_should_raise_error_when_collision_occur(self): | ||
command_name = "command-name" | ||
|
||
mock_extension_1, mock_extension_2 = MagicMock(), MagicMock() | ||
mock_extension_1.command_name = command_name | ||
mock_extension_2.command_name = command_name | ||
|
||
mock_extension_1_entry_point = MagicMock() | ||
mock_extension_1_entry_point.return_value.return_value = mock_extension_1 | ||
|
||
mock_extension_2_entry_point = MagicMock() | ||
mock_extension_2_entry_point.return_value.return_value = mock_extension_2 | ||
|
||
mock_extension_entry_points = { | ||
"key_1": mock_extension_1_entry_point, | ||
"key_2": mock_extension_2_entry_point, | ||
} | ||
|
||
parser = argparse.ArgumentParser() | ||
subparsers = parser.add_subparsers() | ||
|
||
with patch( | ||
"rpdk.core.extensions.get_extensions" | ||
) as mock_get_extensions, self.assertRaises(RuntimeError) as context: | ||
mock_get_extensions.return_value = mock_extension_entry_points | ||
setup_subparsers(subparsers, []) | ||
|
||
assert ( | ||
str(context.exception) | ||
== '"command-name" is already registered as an extension. Please use a different name.' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters