From 51e89a36effec11646b076e820a9d7e7e4e7cb0c Mon Sep 17 00:00:00 2001 From: Pablo Santiago Blum de Aguiar Date: Mon, 14 Mar 2016 18:59:32 -0300 Subject: [PATCH] #N/A: Add new `git_rm_recursive` rule --- README.md | 1 + tests/rules/test_git_rm_recursive.py | 27 +++++++++++++++++++++++++++ thefuck/rules/git_rm_recursive.py | 15 +++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 tests/rules/test_git_rm_recursive.py create mode 100644 thefuck/rules/git_rm_recursive.py diff --git a/README.md b/README.md index e8e4cb14..7e9e19d1 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,7 @@ using the matched rule and runs it. Rules enabled by default are as follows: * `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`; * `git_push_pull` – runs `git pull` when `push` was rejected; +* `git_rm_recursive` – adds `-r` when you try to `rm` a directory; * `git_remote_seturl_add` – runs `git remote add` when `git remote set_url` on nonexistant remote; * `git_stash` – stashes you local modifications before rebasing or switching branch; * `git_two_dashes` – adds a missing dash to commands like `git commit -amend` or `git rebase -continue`; diff --git a/tests/rules/test_git_rm_recursive.py b/tests/rules/test_git_rm_recursive.py new file mode 100644 index 00000000..31b7d974 --- /dev/null +++ b/tests/rules/test_git_rm_recursive.py @@ -0,0 +1,27 @@ +import pytest +from thefuck.rules.git_rm_recursive import match, get_new_command +from tests.utils import Command + + +@pytest.fixture +def stderr(target): + return "fatal: not removing '{}' recursively without -r".format(target) + + +@pytest.mark.parametrize('script, target', [ + ('git rm foo', 'foo'), + ('git rm foo bar', 'foo bar')]) +def test_match(stderr, script, target): + assert match(Command(script=script, stderr=stderr)) + + +@pytest.mark.parametrize('script', ['git rm foo', 'git rm foo bar']) +def test_not_match(script): + assert not match(Command(script=script, stderr='')) + + +@pytest.mark.parametrize('script, target, new_command', [ + ('git rm foo', 'foo', 'git rm -r foo'), + ('git rm foo bar', 'foo bar', 'git rm -r foo bar')]) +def test_get_new_command(stderr, script, target, new_command): + assert get_new_command(Command(script=script, stderr=stderr)) == new_command diff --git a/thefuck/rules/git_rm_recursive.py b/thefuck/rules/git_rm_recursive.py new file mode 100644 index 00000000..b9cad274 --- /dev/null +++ b/thefuck/rules/git_rm_recursive.py @@ -0,0 +1,15 @@ +from thefuck.specific.git import git_support + + +@git_support +def match(command): + return (' rm ' in command.script + and "fatal: not removing '" in command.stderr + and "' recursively without -r" in command.stderr) + + +@git_support +def get_new_command(command): + index = command.script_parts.index('rm') + 1 + command.script_parts.insert(index, '-r') + return u' '.join(command.script_parts)