-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(sdk): add noun aliasing to cli #7569
Merged
connor-mccarthy
merged 5 commits into
kubeflow:master
from
connor-mccarthy:alias-command-plurality
Apr 25, 2022
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a542a1a
allow both singular and plural versions of nouns
connor-mccarthy 0e1abcf
use command table for client creation
connor-mccarthy b76ceec
cleanup
connor-mccarthy b5cee1c
simplify docker mock
connor-mccarthy c1700b4
add docker to test requirements; clean up existing requirements
connor-mccarthy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,51 @@ | ||
# Copyright 2022 The Kubeflow Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import functools | ||
import sys | ||
import unittest | ||
from unittest import mock | ||
|
||
from click import testing | ||
|
||
# Docker is an optional install, but we need the import to succeed for tests, | ||
# so we patch it before importing from kfp.cli | ||
try: | ||
import docker | ||
except ImportError: | ||
sys.modules['docker'] = mock.Mock() | ||
|
||
from kfp.cli import cli | ||
|
||
|
||
class TestCli(unittest.TestCase): | ||
|
||
def setUp(self): | ||
runner = testing.CliRunner() | ||
self.invoke = functools.partial( | ||
runner.invoke, cli=cli.cli, catch_exceptions=False, obj={}) | ||
|
||
def test_aliases_singular(self): | ||
result = self.invoke(args=['component']) | ||
self.assertEqual(result.exit_code, 0) | ||
|
||
def test_aliases_plural(self): | ||
result = self.invoke(args=['components']) | ||
self.assertEqual(result.exit_code, 0) | ||
|
||
def test_aliases_fails(self): | ||
result = self.invoke(args=['componentss']) | ||
self.assertEqual(result.exit_code, 2) | ||
self.assertEqual("Error: Unrecognized command 'componentss'\n", | ||
result.output) |
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,13 @@ | ||
# Copyright 2022 The Kubeflow Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
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,37 @@ | ||
# Copyright 2022 The Kubeflow Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import List, Tuple, Union | ||
|
||
import click | ||
|
||
|
||
class AliasedPluralsGroup(click.Group): | ||
|
||
def get_command(self, ctx: click.Context, cmd_name: str) -> click.Command: | ||
regular = click.Group.get_command(self, ctx, cmd_name) | ||
if regular is not None: | ||
return regular | ||
elif cmd_name.endswith('s'): | ||
singular = click.Group.get_command(self, ctx, cmd_name[:-1]) | ||
if singular is not None: | ||
return singular | ||
raise click.UsageError(f"Unrecognized command '{cmd_name}'") | ||
|
||
def resolve_command( | ||
self, ctx: click.Context, args: List[str] | ||
) -> Tuple[Union[str, None], Union[click.Command, None], List[str]]: | ||
# always return the full command name | ||
_, cmd, args = super().resolve_command(ctx, args) | ||
return cmd.name, cmd, args # type: ignore |
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,55 @@ | ||
# Copyright 2022 The Kubeflow Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
|
||
import click | ||
from click import testing | ||
from kfp.cli.utils import aliased_plurals_group | ||
|
||
|
||
@click.group(cls=aliased_plurals_group.AliasedPluralsGroup) | ||
def cli(): | ||
pass | ||
|
||
|
||
@cli.command() | ||
def command(): | ||
click.echo('Called command.') | ||
|
||
|
||
class TestAliasedPluralsGroup(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.runner = testing.CliRunner() | ||
|
||
def test_aliases_default_success(self): | ||
result = self.runner.invoke(cli, ['command']) | ||
self.assertEqual(result.exit_code, 0) | ||
self.assertEqual(result.output, 'Called command.\n') | ||
|
||
def test_aliases_plural_success(self): | ||
result = self.runner.invoke(cli, ['commands']) | ||
self.assertEqual(result.exit_code, 0) | ||
self.assertEqual(result.output, 'Called command.\n') | ||
|
||
def test_aliases_failure(self): | ||
result = self.runner.invoke(cli, ['commandss']) | ||
self.assertEqual(result.exit_code, 2) | ||
self.assertEqual("Error: Unrecognized command 'commandss'\n", | ||
result.output) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case,
docker
should be test dependency, maybe add it to requirements-test.txt? WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. I did two things:
docker
(andmock
, which had a TODO note) to therequirements-test.txt
file withc1700b4. I chose not to pin the versions because we don't pin the versions in the other places these packages are/were listed (
presubmit-tests-sdk.sh
formock
andsetup.py
fordocker
.)