From dee018e79279592d2eef38302ac113125b5953f3 Mon Sep 17 00:00:00 2001 From: nvbn Date: Mon, 20 Jul 2015 21:04:49 +0300 Subject: [PATCH] #N/A Move `get_all_executables` (formerly `get_all_callables`) to `utils` --- tests/rules/test_history.py | 2 +- tests/rules/test_no_command.py | 25 ++++++------------------- tests/test_utils.py | 17 ++++++++++++++++- thefuck/rules/history.py | 7 +++---- thefuck/rules/no_command.py | 26 +++----------------------- thefuck/utils.py | 19 +++++++++++++++++++ 6 files changed, 48 insertions(+), 48 deletions(-) diff --git a/tests/rules/test_history.py b/tests/rules/test_history.py index 40554b1c..9f9c3b24 100644 --- a/tests/rules/test_history.py +++ b/tests/rules/test_history.py @@ -18,7 +18,7 @@ def alias(mocker): @pytest.fixture def callables(mocker): - return mocker.patch('thefuck.rules.history.get_all_callables', + return mocker.patch('thefuck.rules.history.get_all_executables', return_value=['diff', 'ls']) diff --git a/tests/rules/test_no_command.py b/tests/rules/test_no_command.py index a93d8d56..6159d76b 100644 --- a/tests/rules/test_no_command.py +++ b/tests/rules/test_no_command.py @@ -1,29 +1,16 @@ import pytest -from thefuck.rules.no_command import match, get_new_command, get_all_callables +from thefuck.rules.no_command import match, get_new_command from tests.utils import Command @pytest.fixture(autouse=True) -def _safe(mocker): - mocker.patch('thefuck.rules.no_command._safe', return_value=[]) - - -@pytest.fixture(autouse=True) -def get_aliases(mocker): - mocker.patch('thefuck.rules.no_command.get_aliases', - return_value=['vim', 'apt-get', 'fsck', 'fuck']) +def get_all_executables(mocker): + mocker.patch('thefuck.rules.no_command.get_all_executables', + return_value=['vim', 'apt-get', 'fsck']) @pytest.mark.usefixtures('no_memoize') -def test_get_all_callables(*args): - all_callables = get_all_callables() - assert 'vim' in all_callables - assert 'fsck' in all_callables - assert 'fuck' not in all_callables - - -@pytest.mark.usefixtures('no_memoize') -def test_match(*args): +def test_match(): assert match(Command(stderr='vom: not found', script='vom file.py'), None) assert match(Command(stderr='fucck: not found', script='fucck'), None) assert not match(Command(stderr='qweqwe: not found', script='qweqwe'), None) @@ -31,7 +18,7 @@ def test_match(*args): @pytest.mark.usefixtures('no_memoize') -def test_get_new_command(*args): +def test_get_new_command(): assert get_new_command( Command(stderr='vom: not found', script='vom file.py'), diff --git a/tests/test_utils.py b/tests/test_utils.py index b6a02fcb..39ab00fa 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,6 +1,7 @@ import pytest from mock import Mock -from thefuck.utils import git_support, sudo_support, wrap_settings, memoize, get_closest +from thefuck.utils import git_support, sudo_support, wrap_settings,\ + memoize, get_closest, get_all_executables from thefuck.types import Settings from tests.utils import Command @@ -63,3 +64,17 @@ class TestGetClosest(object): def test_without_fallback(self): assert get_closest('st', ['status', 'reset'], fallback_to_first=False) is None + + +@pytest.fixture +def get_aliases(mocker): + mocker.patch('thefuck.shells.get_aliases', + return_value=['vim', 'apt-get', 'fsck', 'fuck']) + + +@pytest.mark.usefixtures('no_memoize', 'get_aliases') +def test_get_all_callables(): + all_callables = get_all_executables() + assert 'vim' in all_callables + assert 'fsck' in all_callables + assert 'fuck' not in all_callables diff --git a/thefuck/rules/history.py b/thefuck/rules/history.py index 765d9afd..c02f522c 100644 --- a/thefuck/rules/history.py +++ b/thefuck/rules/history.py @@ -1,7 +1,6 @@ from difflib import get_close_matches from thefuck.shells import get_history, thefuck_alias -from thefuck.utils import get_closest, memoize -from thefuck.rules.no_command import get_all_callables +from thefuck.utils import get_closest, memoize, get_all_executables def _not_corrected(history, tf_alias): @@ -19,10 +18,10 @@ def _not_corrected(history, tf_alias): def _history_of_exists_without_current(command): history = get_history() tf_alias = thefuck_alias() - callables = get_all_callables() + executables = get_all_executables() return [line for line in _not_corrected(history, tf_alias) if not line.startswith(tf_alias) and not line == command.script - and line.split(' ')[0] in callables] + and line.split(' ')[0] in executables] def match(command, settings): return len(get_close_matches(command.script, diff --git a/thefuck/rules/no_command.py b/thefuck/rules/no_command.py index f6d9f52b..9687018b 100644 --- a/thefuck/rules/no_command.py +++ b/thefuck/rules/no_command.py @@ -1,39 +1,19 @@ from difflib import get_close_matches -import os -from pathlib import Path -from thefuck.utils import sudo_support, memoize -from thefuck.shells import thefuck_alias, get_aliases - - -def _safe(fn, fallback): - try: - return fn() - except OSError: - return fallback - - -@memoize -def get_all_callables(): - tf_alias = thefuck_alias() - return [exe.name - for path in os.environ.get('PATH', '').split(':') - for exe in _safe(lambda: list(Path(path).iterdir()), []) - if not _safe(exe.is_dir, True)] + [ - alias for alias in get_aliases() if alias != tf_alias] +from thefuck.utils import sudo_support, get_all_executables @sudo_support def match(command, settings): return 'not found' in command.stderr and \ bool(get_close_matches(command.script.split(' ')[0], - get_all_callables())) + get_all_executables())) @sudo_support def get_new_command(command, settings): old_command = command.script.split(' ')[0] new_command = get_close_matches(old_command, - get_all_callables())[0] + get_all_executables())[0] return ' '.join([new_command] + command.script.split(' ')[1:]) diff --git a/thefuck/utils.py b/thefuck/utils.py index ea564d82..19d696af 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -1,5 +1,6 @@ from difflib import get_close_matches from functools import wraps +from pathlib import Path from shlex import split import os import pickle @@ -121,3 +122,21 @@ def get_closest(word, possibilities, n=3, cutoff=0.6, fallback_to_first=True): except IndexError: if fallback_to_first: return possibilities[0] + + +@memoize +def get_all_executables(): + from thefuck.shells import thefuck_alias, get_aliases + + def _safe(fn, fallback): + try: + return fn() + except OSError: + return fallback + + tf_alias = thefuck_alias() + return [exe.name + for path in os.environ.get('PATH', '').split(':') + for exe in _safe(lambda: list(Path(path).iterdir()), []) + if not _safe(exe.is_dir, True)] + [ + alias for alias in get_aliases() if alias != tf_alias]