1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-04-19 09:10:47 +01:00

#209 add support of aliases to no_command

This commit is contained in:
nvbn 2015-05-20 16:58:05 +03:00
parent 5319871326
commit 2c3df1ad47
3 changed files with 8 additions and 7 deletions

View File

@ -3,7 +3,7 @@ from thefuck.rules.no_command import match, get_new_command
def test_match(): def test_match():
with patch('thefuck.rules.no_command._get_all_bins', with patch('thefuck.rules.no_command._get_all_callables',
return_value=['vim', 'apt-get']): return_value=['vim', 'apt-get']):
assert match(Mock(stderr='vom: not found', script='vom file.py'), None) assert match(Mock(stderr='vom: not found', script='vom file.py'), None)
assert not match(Mock(stderr='qweqwe: not found', script='qweqwe'), None) assert not match(Mock(stderr='qweqwe: not found', script='qweqwe'), None)
@ -11,7 +11,7 @@ def test_match():
def test_get_new_command(): def test_get_new_command():
with patch('thefuck.rules.no_command._get_all_bins', with patch('thefuck.rules.no_command._get_all_callables',
return_value=['vim', 'apt-get']): return_value=['vim', 'apt-get']):
assert get_new_command( assert get_new_command(
Mock(stderr='vom: not found', Mock(stderr='vom: not found',

View File

@ -2,6 +2,7 @@ from difflib import get_close_matches
import os import os
from pathlib import Path from pathlib import Path
from thefuck.utils import sudo_support from thefuck.utils import sudo_support
from thefuck.shells import get_aliases
def _safe(fn, fallback): def _safe(fn, fallback):
@ -11,25 +12,25 @@ def _safe(fn, fallback):
return fallback return fallback
def _get_all_bins(): def _get_all_callables():
return [exe.name return [exe.name
for path in os.environ.get('PATH', '').split(':') for path in os.environ.get('PATH', '').split(':')
for exe in _safe(lambda: list(Path(path).iterdir()), []) for exe in _safe(lambda: list(Path(path).iterdir()), [])
if not _safe(exe.is_dir, True)] if not _safe(exe.is_dir, True)] + get_aliases()
@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_bins())) _get_all_callables()))
@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_bins())[0] _get_all_callables())[0]
return ' '.join([new_command] + command.script.split(' ')[1:]) return ' '.join([new_command] + command.script.split(' ')[1:])

View File

@ -186,4 +186,4 @@ def and_(*commands):
def get_aliases(): def get_aliases():
return _get_shell().get_aliases().keys() return list(_get_shell().get_aliases().keys())