mirror of
				https://github.com/nvbn/thefuck.git
				synced 2025-10-30 22:54:14 +00: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:
		
				
					committed by
					
						 Joseph Frazier
						Joseph Frazier
					
				
			
			
				
	
			
			
			
						parent
						
							045c8ae76c
						
					
				
				
					commit
					83e1710712
				
			| @@ -13,9 +13,10 @@ class TestFish(object): | |||||||
|     @pytest.fixture(autouse=True) |     @pytest.fixture(autouse=True) | ||||||
|     def Popen(self, mocker): |     def Popen(self, mocker): | ||||||
|         mock = mocker.patch('thefuck.shells.fish.Popen') |         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'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 |         return mock | ||||||
|  |  | ||||||
|     @pytest.mark.parametrize('key, value', [ |     @pytest.mark.parametrize('key, value', [ | ||||||
| @@ -42,7 +43,8 @@ class TestFish(object): | |||||||
|         ('open', 'open'), |         ('open', 'open'), | ||||||
|         ('vim', 'vim'), |         ('vim', 'vim'), | ||||||
|         ('ll', 'fish -ic "ll"'), |         ('ll', 'fish -ic "ll"'), | ||||||
|         ('ls', 'ls')])  # Fish has no aliases but functions |         ('ls', 'ls'), | ||||||
|  |         ('g', 'git')]) | ||||||
|     def test_from_shell(self, before, after, shell): |     def test_from_shell(self, before, after, shell): | ||||||
|         assert shell.from_shell(before) == after |         assert shell.from_shell(before) == after | ||||||
|  |  | ||||||
| @@ -65,7 +67,9 @@ class TestFish(object): | |||||||
|                                        'math': 'math', |                                        'math': 'math', | ||||||
|                                        'popd': 'popd', |                                        'popd': 'popd', | ||||||
|                                        'pushd': 'pushd', |                                        'pushd': 'pushd', | ||||||
|                                        'ruby': 'ruby'} |                                        'ruby': 'ruby', | ||||||
|  |                                        'g': 'git', | ||||||
|  |                                        'fish_key_reader': '/usr/bin/fish_key_reader'} | ||||||
|  |  | ||||||
|     def test_app_alias(self, shell): |     def test_app_alias(self, shell): | ||||||
|         assert 'function fuck' in shell.app_alias('fuck') |         assert 'function fuck' in shell.app_alias('fuck') | ||||||
|   | |||||||
| @@ -10,12 +10,24 @@ from .generic import Generic | |||||||
|  |  | ||||||
|  |  | ||||||
| @cache('~/.config/fish/config.fish', '~/.config/fish/functions') | @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) |     proc = Popen(['fish', '-ic', 'functions'], stdout=PIPE, stderr=DEVNULL) | ||||||
|     functions = proc.stdout.read().decode('utf-8').strip().split('\n') |     functions = proc.stdout.read().decode('utf-8').strip().split('\n') | ||||||
|     return {func: func for func in functions if func not in overridden} |     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): | class Fish(Generic): | ||||||
|     def _get_overridden_aliases(self): |     def _get_overridden_aliases(self): | ||||||
|         overridden = os.environ.get('THEFUCK_OVERRIDDEN_ALIASES', |         overridden = os.environ.get('THEFUCK_OVERRIDDEN_ALIASES', | ||||||
| @@ -44,12 +56,17 @@ class Fish(Generic): | |||||||
|  |  | ||||||
|     def get_aliases(self): |     def get_aliases(self): | ||||||
|         overridden = self._get_overridden_aliases() |         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): |     def _expand_aliases(self, command_script): | ||||||
|         aliases = self.get_aliases() |         aliases = self.get_aliases() | ||||||
|         binary = command_script.split(' ')[0] |         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'\"')) |             return u'fish -ic "{}"'.format(command_script.replace('"', r'\"')) | ||||||
|         else: |         else: | ||||||
|             return command_script |             return command_script | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user