1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-14 14:48:49 +00:00

Handle user defined fish aliases

This commit is contained in:
David 2018-01-01 14:54:22 +00:00
parent f966ecd4f5
commit cc1c558754
2 changed files with 28 additions and 7 deletions

View File

@ -10,12 +10,13 @@ class TestFish(object):
def shell(self):
return Fish()
@pytest.fixture(autouse=True)
@pytest.fixture
def Popen(self, mocker):
mock = mocker.patch('thefuck.shells.fish.Popen')
mock.return_value.stdout.read.return_value = (
mock.return_value.stdout.read.side_effect = [(
b'cd\nfish_config\nfuck\nfunced\nfuncsave\ngrep\nhistory\nll\nls\n'
b'man\nmath\npopd\npushd\nruby')
b'man\nmath\npopd\npushd\nruby'),
b'alias g git']
return mock
@pytest.mark.parametrize('key, value', [
@ -43,6 +44,7 @@ class TestFish(object):
('vim', 'vim'),
('ll', 'fish -ic "ll"'),
('ls', 'ls')]) # Fish has no aliases but functions
@pytest.mark.usefixtures('Popen')
def test_from_shell(self, before, after, shell):
assert shell.from_shell(before) == after
@ -55,6 +57,7 @@ class TestFish(object):
def test_or_(self, shell):
assert shell.or_('foo', 'bar') == 'foo; or bar'
@pytest.mark.usefixtures('Popen')
def test_get_aliases(self, shell):
assert shell.get_aliases() == {'fish_config': 'fish_config',
'fuck': 'fuck',
@ -65,7 +68,8 @@ class TestFish(object):
'math': 'math',
'popd': 'popd',
'pushd': 'pushd',
'ruby': 'ruby'}
'ruby': 'ruby',
'g': 'git'}
def test_app_alias(self, shell):
assert 'function fuck' in shell.app_alias('fuck')

View File

@ -10,12 +10,24 @@ from .generic import Generic
@cache('~/.config/fish/config.fish', '~/.config/fish/functions')
def _get_aliases(overridden):
def _get_functions(overridden):
proc = Popen(['fish', '-ic', 'functions'], stdout=PIPE, stderr=DEVNULL)
functions = proc.stdout.read().decode('utf-8').strip().split('\n')
return {func: func for func in functions if func not in overridden}
@cache('~/.config/fish/config.fish')
def _get_aliases(overridden):
aliases = {}
proc = Popen(['fish', '-ic', 'alias'], stdout=PIPE, stderr=DEVNULL)
alias_out = proc.stdout.read().decode('utf-8').strip().split('\n')
for alias in alias_out:
name, value = alias.replace('alias ', '', 1).split(' ', 1)
if name not in overridden:
aliases[name] = value
return aliases
class Fish(Generic):
def _get_overridden_aliases(self):
overridden = os.environ.get('THEFUCK_OVERRIDDEN_ALIASES',
@ -44,12 +56,17 @@ class Fish(Generic):
def get_aliases(self):
overridden = self._get_overridden_aliases()
return _get_aliases(overridden)
functions = _get_functions(overridden)
raw_aliases = _get_aliases(overridden)
functions.update(raw_aliases)
return functions
def _expand_aliases(self, command_script):
aliases = self.get_aliases()
binary = command_script.split(' ')[0]
if binary in aliases:
if binary in aliases and aliases[binary] != binary:
return command_script.replace(binary, aliases[binary], 1)
elif binary in aliases:
return u'fish -ic "{}"'.format(command_script.replace('"', r'\"'))
else:
return command_script