diff --git a/tests/rules/test_gcloud_cli.py b/tests/rules/test_gcloud_cli.py new file mode 100644 index 00000000..bf8a0110 --- /dev/null +++ b/tests/rules/test_gcloud_cli.py @@ -0,0 +1,63 @@ +import pytest + +from thefuck.rules.gcloud_cli import match, get_new_command +from thefuck.types import Command + + +no_suggestions = '''\ +ERROR: (gcloud) Command name argument expected. +''' + + +misspelled_command = '''\ +ERROR: (gcloud) Invalid choice: 'comute'. +Usage: gcloud [optional flags] + group may be access-context-manager | ai-platform | alpha | app | + asset | auth | beta | bigtable | builds | components | + composer | compute | config | container | dataflow | + dataproc | datastore | debug | deployment-manager | + dns | domains | endpoints | filestore | firebase | + functions | iam | iot | kms | logging | ml | + ml-engine | organizations | projects | pubsub | redis | + resource-manager | scheduler | services | source | + spanner | sql | tasks | topic + command may be docker | feedback | help | info | init | version + +For detailed information on this command and its flags, run: + gcloud --help +''' + + +misspelled_subcommand = '''\ +ERROR: (gcloud.compute) Invalid choice: 'instance'. +Maybe you meant: + gcloud compute instance-groups + gcloud compute instance-templates + gcloud compute instances + gcloud compute target-instances + +To search the help text of gcloud commands, run: + gcloud help -- SEARCH_TERMS +''' + + + + +@pytest.mark.parametrize('command', [ + Command('gcloud comute', misspelled_command), + Command('gcloud compute instance list', misspelled_subcommand)]) +def test_match(command): + assert match(command) + + +def test_not_match(): + assert not match(Command('aws dynamodb invalid', no_suggestions)) + + +@pytest.mark.parametrize('command, result', [ + (Command('gcloud comute', misspelled_command), + ['gcloud compute instances list']), + (Command('gcloud compute instance list', misspelled_subcommand), + ['gcloud compute instances list'])]) +def test_get_new_command(command, result): + assert get_new_command(command) == result diff --git a/thefuck/rules/gcloud_cli.py b/thefuck/rules/gcloud_cli.py new file mode 100644 index 00000000..035df5df --- /dev/null +++ b/thefuck/rules/gcloud_cli.py @@ -0,0 +1,17 @@ +import re + +from thefuck.utils import for_app, replace_argument + +INVALID_CHOICE = "(?<=Invalid choice: ')(.*)(?=', maybe you meant:)" +OPTIONS = "^\\s*\\*\\s(.*)" + + +@for_app('gcloud') +def match(command): + return "usage:" in command.output and "maybe you meant:" in command.output + + +def get_new_command(command): + mistake = re.search(INVALID_CHOICE, command.output).group(0) + options = re.findall(OPTIONS, command.output, flags=re.MULTILINE) + return [replace_argument(command.script, mistake, o) for o in options]