From 4fe64e3dfa61167a3dba4b42692b803cb5fb0fbf Mon Sep 17 00:00:00 2001 From: Vladimir Iakovlev Date: Tue, 23 Aug 2016 13:03:49 +0300 Subject: [PATCH] #N/A: Match `git_add` only if pathspec exists --- tests/rules/test_git_add.py | 17 +++++++++++++---- thefuck/rules/git_add.py | 19 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/tests/rules/test_git_add.py b/tests/rules/test_git_add.py index 18972b16..853c03af 100644 --- a/tests/rules/test_git_add.py +++ b/tests/rules/test_git_add.py @@ -3,6 +3,12 @@ from thefuck.rules.git_add import match, get_new_command from tests.utils import Command +@pytest.fixture(autouse=True) +def path_exists(mocker): + return mocker.patch('thefuck.rules.git_add.Path.exists', + return_value=True) + + @pytest.fixture def stderr(target): return ("error: pathspec '{}' did not match any " @@ -16,10 +22,13 @@ def test_match(stderr, script, target): assert match(Command(script=script, stderr=stderr)) -@pytest.mark.parametrize('script', [ - 'git submodule update known', 'git commit known']) -def test_not_match(script): - assert not match(Command(script=script, stderr='')) +@pytest.mark.parametrize('script, target, exists', [ + ('git submodule update known', '', True), + ('git commit known', '', True), + ('git submodule update known', stderr, False)]) +def test_not_match(path_exists, stderr, script, target, exists): + path_exists.return_value = exists + assert not match(Command(script=script, stderr=stderr)) @pytest.mark.parametrize('script, target, new_command', [ diff --git a/thefuck/rules/git_add.py b/thefuck/rules/git_add.py index a779a204..b9cbadb6 100644 --- a/thefuck/rules/git_add.py +++ b/thefuck/rules/git_add.py @@ -1,18 +1,27 @@ import re from thefuck.shells import shell from thefuck.specific.git import git_support +from thefuck.system import Path +from thefuck.utils import memoize + + +@memoize +def _get_missing_file(command): + pathspec = re.findall( + r"error: pathspec '([^']*)' " + r'did not match any file\(s\) known to git.', command.stderr)[0] + if Path(pathspec).exists(): + return pathspec @git_support def match(command): - return 'did not match any file(s) known to git.' in command.stderr + return ('did not match any file(s) known to git.' in command.stderr + and _get_missing_file(command)) @git_support def get_new_command(command): - missing_file = re.findall( - r"error: pathspec '([^']*)' " - r'did not match any file\(s\) known to git.', command.stderr)[0] - + missing_file = _get_missing_file(command) formatme = shell.and_('git add -- {}', '{}') return formatme.format(missing_file, command.script)