From f6c013d0333dcff08a61db8c43d48ab384c68616 Mon Sep 17 00:00:00 2001 From: mcarton Date: Sat, 6 Jun 2015 17:06:10 +0200 Subject: [PATCH] Add a `cargo_no_command` rule --- README.md | 3 ++- tests/rules/test_cargo_no_command.py | 21 +++++++++++++++++++++ thefuck/rules/cargo_no_command.py | 14 ++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/rules/test_cargo_no_command.py create mode 100644 thefuck/rules/cargo_no_command.py diff --git a/README.md b/README.md index d85cda0..22b5c2e 100644 --- a/README.md +++ b/README.md @@ -146,7 +146,8 @@ sudo pip install thefuck --upgrade The Fuck tries to match a rule for the previous command, creates a new command using the matched rule and runs it. Rules enabled by default are as follows: -* `cargo` – run `cargo build` instead of `cargo`; +* `cargo` – runs `cargo build` instead of `cargo`; +* `cargo_no_command` – fixes wrongs commands like `cargo buid`; * `cd_correction` – spellchecks and correct failed cd commands; * `cd_mkdir` – creates directories before cd'ing into them; * `cd_parent` – changes `cd..` to `cd ..`; diff --git a/tests/rules/test_cargo_no_command.py b/tests/rules/test_cargo_no_command.py new file mode 100644 index 0000000..df87585 --- /dev/null +++ b/tests/rules/test_cargo_no_command.py @@ -0,0 +1,21 @@ +import pytest +from thefuck.rules.cargo_no_command import match, get_new_command +from tests.utils import Command + + +no_such_subcommand = """No such subcommand + + Did you mean `build`? +""" + + +@pytest.mark.parametrize('command', [ + Command(script='cargo buid', stderr=no_such_subcommand)]) +def test_match(command): + assert match(command, None) + + +@pytest.mark.parametrize('command, new_command', [ + (Command('cargo buid', stderr=no_such_subcommand), 'cargo build')]) +def test_get_new_command(command, new_command): + assert get_new_command(command, None) == new_command diff --git a/thefuck/rules/cargo_no_command.py b/thefuck/rules/cargo_no_command.py new file mode 100644 index 0000000..350e927 --- /dev/null +++ b/thefuck/rules/cargo_no_command.py @@ -0,0 +1,14 @@ +import re + + +def match(command, settings): + return ('cargo' in command.script + and 'No such subcommand' in command.stderr + and 'Did you mean' in command.stderr) + + +def get_new_command(command, settings): + broken = command.script.split()[1] + fix = re.findall(r'Did you mean `([^`]*)`', command.stderr)[0] + + return command.script.replace(broken, fix, 1)