mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-21 20:38:54 +00:00
#N/A Move get_all_executables
(formerly get_all_callables
) to utils
This commit is contained in:
parent
c67560864a
commit
dee018e792
@ -18,7 +18,7 @@ def alias(mocker):
|
|||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def callables(mocker):
|
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'])
|
return_value=['diff', 'ls'])
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,29 +1,16 @@
|
|||||||
import pytest
|
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
|
from tests.utils import Command
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def _safe(mocker):
|
def get_all_executables(mocker):
|
||||||
mocker.patch('thefuck.rules.no_command._safe', return_value=[])
|
mocker.patch('thefuck.rules.no_command.get_all_executables',
|
||||||
|
return_value=['vim', 'apt-get', 'fsck'])
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
|
||||||
def get_aliases(mocker):
|
|
||||||
mocker.patch('thefuck.rules.no_command.get_aliases',
|
|
||||||
return_value=['vim', 'apt-get', 'fsck', 'fuck'])
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures('no_memoize')
|
@pytest.mark.usefixtures('no_memoize')
|
||||||
def test_get_all_callables(*args):
|
def test_match():
|
||||||
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):
|
|
||||||
assert match(Command(stderr='vom: not found', script='vom file.py'), None)
|
assert match(Command(stderr='vom: not found', script='vom file.py'), None)
|
||||||
assert match(Command(stderr='fucck: not found', script='fucck'), None)
|
assert match(Command(stderr='fucck: not found', script='fucck'), None)
|
||||||
assert not match(Command(stderr='qweqwe: not found', script='qweqwe'), 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')
|
@pytest.mark.usefixtures('no_memoize')
|
||||||
def test_get_new_command(*args):
|
def test_get_new_command():
|
||||||
assert get_new_command(
|
assert get_new_command(
|
||||||
Command(stderr='vom: not found',
|
Command(stderr='vom: not found',
|
||||||
script='vom file.py'),
|
script='vom file.py'),
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from mock import Mock
|
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 thefuck.types import Settings
|
||||||
from tests.utils import Command
|
from tests.utils import Command
|
||||||
|
|
||||||
@ -63,3 +64,17 @@ class TestGetClosest(object):
|
|||||||
def test_without_fallback(self):
|
def test_without_fallback(self):
|
||||||
assert get_closest('st', ['status', 'reset'],
|
assert get_closest('st', ['status', 'reset'],
|
||||||
fallback_to_first=False) is None
|
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
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
from difflib import get_close_matches
|
from difflib import get_close_matches
|
||||||
from thefuck.shells import get_history, thefuck_alias
|
from thefuck.shells import get_history, thefuck_alias
|
||||||
from thefuck.utils import get_closest, memoize
|
from thefuck.utils import get_closest, memoize, get_all_executables
|
||||||
from thefuck.rules.no_command import get_all_callables
|
|
||||||
|
|
||||||
|
|
||||||
def _not_corrected(history, tf_alias):
|
def _not_corrected(history, tf_alias):
|
||||||
@ -19,10 +18,10 @@ def _not_corrected(history, tf_alias):
|
|||||||
def _history_of_exists_without_current(command):
|
def _history_of_exists_without_current(command):
|
||||||
history = get_history()
|
history = get_history()
|
||||||
tf_alias = thefuck_alias()
|
tf_alias = thefuck_alias()
|
||||||
callables = get_all_callables()
|
executables = get_all_executables()
|
||||||
return [line for line in _not_corrected(history, tf_alias)
|
return [line for line in _not_corrected(history, tf_alias)
|
||||||
if not line.startswith(tf_alias) and not line == command.script
|
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):
|
def match(command, settings):
|
||||||
return len(get_close_matches(command.script,
|
return len(get_close_matches(command.script,
|
||||||
|
@ -1,39 +1,19 @@
|
|||||||
from difflib import get_close_matches
|
from difflib import get_close_matches
|
||||||
import os
|
from thefuck.utils import sudo_support, get_all_executables
|
||||||
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]
|
|
||||||
|
|
||||||
|
|
||||||
@sudo_support
|
@sudo_support
|
||||||
def match(command, settings):
|
def match(command, settings):
|
||||||
return 'not found' in command.stderr and \
|
return 'not found' in command.stderr and \
|
||||||
bool(get_close_matches(command.script.split(' ')[0],
|
bool(get_close_matches(command.script.split(' ')[0],
|
||||||
get_all_callables()))
|
get_all_executables()))
|
||||||
|
|
||||||
|
|
||||||
@sudo_support
|
@sudo_support
|
||||||
def get_new_command(command, settings):
|
def get_new_command(command, settings):
|
||||||
old_command = command.script.split(' ')[0]
|
old_command = command.script.split(' ')[0]
|
||||||
new_command = get_close_matches(old_command,
|
new_command = get_close_matches(old_command,
|
||||||
get_all_callables())[0]
|
get_all_executables())[0]
|
||||||
return ' '.join([new_command] + command.script.split(' ')[1:])
|
return ' '.join([new_command] + command.script.split(' ')[1:])
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from difflib import get_close_matches
|
from difflib import get_close_matches
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
from pathlib import Path
|
||||||
from shlex import split
|
from shlex import split
|
||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
@ -121,3 +122,21 @@ def get_closest(word, possibilities, n=3, cutoff=0.6, fallback_to_first=True):
|
|||||||
except IndexError:
|
except IndexError:
|
||||||
if fallback_to_first:
|
if fallback_to_first:
|
||||||
return possibilities[0]
|
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]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user