From b4392ba706cbb924d009737d7e3e5dae62cfb97f Mon Sep 17 00:00:00 2001 From: nvbn Date: Wed, 22 Jul 2015 04:44:37 +0300 Subject: [PATCH] #N/A Add `heroku_not_command` rule --- README.md | 3 ++- tests/rules/test_heroku_not_command.py | 34 ++++++++++++++++++++++++++ thefuck/rules/heroku_not_command.py | 20 +++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/rules/test_heroku_not_command.py create mode 100644 thefuck/rules/heroku_not_command.py diff --git a/README.md b/README.md index ef6c01a1..853bf49d 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `git_branch_list` – catches `git branch list` in place of `git branch` and removes created branch; * `git_checkout` – fixes branch name or creates new branch; * `git_diff_staged` – adds `--staged` to previous `git diff` with unexpected output; -* `git_no_command` – fixes wrong git commands like `git brnch`; +* `git_not_command` – fixes wrong git commands like `git brnch`; * `git_pull` – sets upstream before executing previous `git pull`; * `git_pull_clone` – clones instead of pulling when the repo does not exist; * `git_push` – adds `--set-upstream origin $branch` to previous failed `git push`; @@ -157,6 +157,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `go_run` – appends `.go` extension when compiling/running Go programs * `grep_recursive` – adds `-r` when you trying to grep directory; * `has_exists_script` – prepends `./` when script/binary exists; +* `heroku_no_command` – fixes wrong heroku commands like `heroku log`; * `history` – tries to replace command with most similar command from history; * `java` – removes `.java` extension when running Java programs; * `javac` – appends missing `.java` when compiling Java files; diff --git a/tests/rules/test_heroku_not_command.py b/tests/rules/test_heroku_not_command.py new file mode 100644 index 00000000..acbda7ae --- /dev/null +++ b/tests/rules/test_heroku_not_command.py @@ -0,0 +1,34 @@ +import pytest +from tests.utils import Command +from thefuck.rules.heroku_not_command import match, get_new_command + + +def suggest_stderr(cmd): + return ''' ! `{}` is not a heroku command. + ! Perhaps you meant `logs`, `pg`. + ! See `heroku help` for a list of available commands.'''.format(cmd) + + +no_suggest_stderr = ''' ! `aaaaa` is not a heroku command. + ! See `heroku help` for a list of available commands.''' + + +@pytest.mark.parametrize('cmd', ['log', 'pge']) +def test_match(cmd): + assert match( + Command('heroku {}'.format(cmd), stderr=suggest_stderr(cmd)), None) + + +@pytest.mark.parametrize('script, stderr', [ + ('cat log', suggest_stderr('log')), + ('heroku aaa', no_suggest_stderr)]) +def test_not_match(script, stderr): + assert not match(Command(script, stderr=stderr), None) + + +@pytest.mark.parametrize('cmd, result', [ + ('log', 'heroku logs'), + ('pge', 'heroku pg')]) +def test_get_new_command(cmd, result): + command = Command('heroku {}'.format(cmd), stderr=suggest_stderr(cmd)) + assert get_new_command(command, None) == result diff --git a/thefuck/rules/heroku_not_command.py b/thefuck/rules/heroku_not_command.py new file mode 100644 index 00000000..ca14f74d --- /dev/null +++ b/thefuck/rules/heroku_not_command.py @@ -0,0 +1,20 @@ +import re +from thefuck.utils import get_closest + + +def match(command, settings): + return command.script.startswith('heroku') and \ + 'is not a heroku command' in command.stderr and \ + 'Perhaps you meant' in command.stderr + + +def _get_suggests(stderr): + for line in stderr.split('\n'): + if 'Perhaps you meant' in line: + return re.findall(r'`([^`]+)`', line) + + +def get_new_command(command, settings): + wrong = re.findall(r'`(\w+)` is not a heroku command', command.stderr)[0] + correct = get_closest(wrong, _get_suggests(command.stderr)) + return command.script.replace(' {}'.format(wrong), ' {}'.format(correct), 1)