diff --git a/tests/shells/test_fish.py b/tests/shells/test_fish.py index dacc0188..efd684f0 100644 --- a/tests/shells/test_fish.py +++ b/tests/shells/test_fish.py @@ -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') diff --git a/thefuck/shells/fish.py b/thefuck/shells/fish.py index 0b635dbd..471df2a9 100644 --- a/thefuck/shells/fish.py +++ b/thefuck/shells/fish.py @@ -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