1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-20 20:09:07 +00:00

Add is_app/for_app helpers

This commit is contained in:
nvbn 2015-08-27 16:08:29 +03:00
parent edf77a90ad
commit 9103c1ffd5
2 changed files with 47 additions and 1 deletions

View File

@ -2,8 +2,9 @@ import pytest
from mock import Mock
from thefuck.utils import wrap_settings,\
memoize, get_closest, get_all_executables, replace_argument, \
get_all_matched_commands
get_all_matched_commands, is_app, for_app
from thefuck.types import Settings
from tests.utils import Command
@pytest.mark.parametrize('override, old, new', [
@ -93,3 +94,25 @@ def test_replace_argument(args, result):
'service-status', 'service-unbind'])])
def test_get_all_matched_commands(stderr, result):
assert list(get_all_matched_commands(stderr)) == result
@pytest.mark.usefixtures('no_memoize')
@pytest.mark.parametrize('script, names, result', [
('git diff', ['git', 'hub'], True),
('hub diff', ['git', 'hub'], True),
('hg diff', ['git', 'hub'], False)])
def test_is_app(script, names, result):
assert is_app(Command(script), *names) == result
@pytest.mark.usefixtures('no_memoize')
@pytest.mark.parametrize('script, names, result', [
('git diff', ['git', 'hub'], True),
('hub diff', ['git', 'hub'], True),
('hg diff', ['git', 'hub'], False)])
def test_for_app(script, names, result):
@for_app(*names)
def match(command, settings):
return True
assert match(Command(script), None) == result

View File

@ -132,3 +132,26 @@ def replace_command(command, broken, matched):
new_cmds = get_close_matches(broken, matched, cutoff=0.1)
return [replace_argument(command.script, broken, new_cmd.strip())
for new_cmd in new_cmds]
@memoize
def is_app(command, *app_names):
"""Returns `True` if command is call to one of passed app names."""
for name in app_names:
if command.script.startswith(u'{} '.format(name)):
return True
return False
def for_app(*app_names):
"""Specifies that matching script is for on of app names."""
def decorator(fn):
@wraps(fn)
def wrapper(command, settings):
if is_app(command, *app_names):
return fn(command, settings)
else:
return False
return wrapper
return decorator