From 926e9ef963464075184f5c2e04ffdce9cb971997 Mon Sep 17 00:00:00 2001 From: James Turnbull Date: Wed, 15 Aug 2018 18:22:24 -0400 Subject: [PATCH] Added a rule to match az binary sub-command misses (#834) --- README.md | 1 + tests/rules/test_az_cli.py | 44 ++++++++++++++++++++++++++++++++++++++ thefuck/rules/az_cli.py | 17 +++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 tests/rules/test_az_cli.py create mode 100644 thefuck/rules/az_cli.py diff --git a/README.md b/README.md index 19db1fc..5fa401e 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,7 @@ following rules are enabled by default: * `adb_unknown_command` – fixes misspelled commands like `adb logcta`; * `ag_literal` – adds `-Q` to `ag` when suggested; * `aws_cli` – fixes misspelled commands like `aws dynamdb scan`; +* `az_cli` – fixes misspelled commands like `az providers`; * `cargo` – runs `cargo build` instead of `cargo`; * `cargo_no_command` – fixes wrongs commands like `cargo buid`; * `cat_dir` – replaces `cat` with `ls` when you try to `cat` a directory; diff --git a/tests/rules/test_az_cli.py b/tests/rules/test_az_cli.py new file mode 100644 index 0000000..1cb19f1 --- /dev/null +++ b/tests/rules/test_az_cli.py @@ -0,0 +1,44 @@ +import pytest + +from thefuck.rules.az_cli import match, get_new_command +from thefuck.types import Command + + +no_suggestions = '''\ +az provider: error: the following arguments are required: _subcommand +usage: az provider [-h] {list,show,register,unregister,operation} ... +''' + + +misspelled_command = '''\ +az: 'providers' is not in the 'az' command group. See 'az --help'. + +The most similar choice to 'providers' is: + provider +''' + +misspelled_subcommand = '''\ +az provider: 'lis' is not in the 'az provider' command group. See 'az provider --help'. + +The most similar choice to 'lis' is: + list +''' + + +@pytest.mark.parametrize('command', [ + Command('az providers', misspelled_command), + Command('az provider lis', misspelled_subcommand)]) +def test_match(command): + assert match(command) + + +def test_not_match(): + assert not match(Command('az provider', no_suggestions)) + + +@pytest.mark.parametrize('command, result', [ + (Command('az providers list', misspelled_command), ['az provider list']), + (Command('az provider lis', misspelled_subcommand), ['az provider list']) +]) +def test_get_new_command(command, result): + assert get_new_command(command) == result diff --git a/thefuck/rules/az_cli.py b/thefuck/rules/az_cli.py new file mode 100644 index 0000000..ae53834 --- /dev/null +++ b/thefuck/rules/az_cli.py @@ -0,0 +1,17 @@ +import re + +from thefuck.utils import for_app, replace_argument + +INVALID_CHOICE = "(?=az)(?:.*): '(.*)' is not in the '.*' command group." +OPTIONS = "^The most similar choice to '.*' is:\n\s*(.*)$" + + +@for_app('az') +def match(command): + return "is not in the" in command.output and "command group" in command.output + + +def get_new_command(command): + mistake = re.search(INVALID_CHOICE, command.output).group(1) + options = re.findall(OPTIONS, command.output, flags=re.MULTILINE) + return [replace_argument(command.script, mistake, o) for o in options]