1
0
mirror of https://github.com/nvbn/thefuck.git synced 2024-10-05 18:31:10 +01:00

Fix fish shell aliasing (#753)

* Handle user defined fish aliases

* Add more aliases to test

* Revert unecessary Popen mock changes

* Add test for fish aliasing

Fixes #727
This commit is contained in:
David Hart 2018-01-03 04:14:02 +00:00 committed by Joseph Frazier
parent 045c8ae76c
commit 83e1710712
2 changed files with 28 additions and 7 deletions

View File

@ -13,9 +13,10 @@ class TestFish(object):
@pytest.fixture(autouse=True)
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 fish_key_reader /usr/bin/fish_key_reader\nalias g git']
return mock
@pytest.mark.parametrize('key, value', [
@ -42,7 +43,8 @@ class TestFish(object):
('open', 'open'),
('vim', 'vim'),
('ll', 'fish -ic "ll"'),
('ls', 'ls')]) # Fish has no aliases but functions
('ls', 'ls'),
('g', 'git')])
def test_from_shell(self, before, after, shell):
assert shell.from_shell(before) == after
@ -65,7 +67,9 @@ class TestFish(object):
'math': 'math',
'popd': 'popd',
'pushd': 'pushd',
'ruby': 'ruby'}
'ruby': 'ruby',
'g': 'git',
'fish_key_reader': '/usr/bin/fish_key_reader'}
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