From 9103c1ffd52315df62b2401cc07c702fae3b3eab Mon Sep 17 00:00:00 2001 From: nvbn Date: Thu, 27 Aug 2015 16:08:29 +0300 Subject: [PATCH] Add is_app/for_app helpers --- tests/test_utils.py | 25 ++++++++++++++++++++++++- thefuck/utils.py | 23 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index b66f4c8e..5979b204 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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 diff --git a/thefuck/utils.py b/thefuck/utils.py index 8dd777b7..6091e7f0 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -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