mirror of
				https://github.com/nvbn/thefuck.git
				synced 2025-10-31 23:22:10 +00:00 
			
		
		
		
	Merge pull request #277 from mcarton/fix-sudo
Fix the pacman rule with `sudo`
This commit is contained in:
		| @@ -23,23 +23,25 @@ extra/vim-python3 7.4.712-1 \t/usr/bin/vim | |||||||
| @pytest.mark.skipif(not getattr(pacman, 'enabled_by_default', True), | @pytest.mark.skipif(not getattr(pacman, 'enabled_by_default', True), | ||||||
|                     reason='Skip if pacman is not available') |                     reason='Skip if pacman is not available') | ||||||
| @pytest.mark.parametrize('command', [ | @pytest.mark.parametrize('command', [ | ||||||
|     Command(script='vim', stderr='vim: command not found')]) |     Command(script='vim', stderr='vim: command not found'), | ||||||
|  |     Command(script='sudo vim', stderr='sudo: vim: command not found')]) | ||||||
| def test_match(command): | def test_match(command): | ||||||
|     assert match(command, None) |     assert match(command, None) | ||||||
|  |  | ||||||
|  |  | ||||||
| @pytest.mark.parametrize('command, return_value', [ | @pytest.mark.parametrize('command, return_value', [ | ||||||
|     (Command(script='vim', stderr='vim: command not found'), PKGFILE_OUTPUT_VIM)]) |     (Command(script='vim', stderr='vim: command not found'), PKGFILE_OUTPUT_VIM), | ||||||
|  |     (Command(script='sudo vim', stderr='sudo: vim: command not found'), PKGFILE_OUTPUT_VIM)]) | ||||||
| @patch('thefuck.rules.pacman.subprocess') | @patch('thefuck.rules.pacman.subprocess') | ||||||
| @patch.multiple(pacman, create=True, pacman=pacman_cmd) | @patch.multiple(pacman, create=True, pacman=pacman_cmd) | ||||||
| def test_match_mocked(subp_mock, command, return_value): | def test_match_mocked(subp_mock, command, return_value): | ||||||
|     subp_mock.check_output.return_value = return_value |     subp_mock.check_output.return_value = return_value | ||||||
|     assert match(command, None) |     assert match(command, None) | ||||||
|     assert subp_mock.check_output.called |  | ||||||
|  |  | ||||||
|  |  | ||||||
| @pytest.mark.parametrize('command', [ | @pytest.mark.parametrize('command', [ | ||||||
|     Command(script='vim', stderr=''), Command()]) |     Command(script='vim', stderr=''), Command(), | ||||||
|  |     Command(script='sudo vim', stderr=''), Command()]) | ||||||
| def test_not_match(command): | def test_not_match(command): | ||||||
|     assert not match(command, None) |     assert not match(command, None) | ||||||
|  |  | ||||||
| @@ -48,19 +50,20 @@ def test_not_match(command): | |||||||
|                     reason='Skip if pacman is not available') |                     reason='Skip if pacman is not available') | ||||||
| @pytest.mark.parametrize('command, new_command', [ | @pytest.mark.parametrize('command, new_command', [ | ||||||
|     (Command('vim'), '{} -S extra/gvim && vim'.format(pacman_cmd)), |     (Command('vim'), '{} -S extra/gvim && vim'.format(pacman_cmd)), | ||||||
|     (Command('convert'), '{} -S extra/imagemagick && convert'.format(pacman_cmd))]) |     (Command('sudo vim'), '{} -S extra/gvim && sudo vim'.format(pacman_cmd)), | ||||||
|  |     (Command('convert'), '{} -S extra/imagemagick && convert'.format(pacman_cmd)), | ||||||
|  |     (Command('sudo convert'), '{} -S extra/imagemagick && sudo convert'.format(pacman_cmd))]) | ||||||
| def test_get_new_command(command, new_command, mocker): | def test_get_new_command(command, new_command, mocker): | ||||||
|     assert get_new_command(command, None) == new_command |     assert get_new_command(command, None) == new_command | ||||||
|  |  | ||||||
|  |  | ||||||
| @pytest.mark.parametrize('command, new_command, return_value', [ | @pytest.mark.parametrize('command, new_command, return_value', [ | ||||||
|     (Command('vim'), '{} -S extra/gvim && vim'.format(pacman_cmd), |     (Command('vim'), '{} -S extra/gvim && vim'.format(pacman_cmd), PKGFILE_OUTPUT_VIM), | ||||||
|         PKGFILE_OUTPUT_VIM), |     (Command('sudo vim'), '{} -S extra/gvim && sudo vim'.format(pacman_cmd), PKGFILE_OUTPUT_VIM), | ||||||
|     (Command('convert'), '{} -S extra/imagemagick && convert'.format(pacman_cmd), |     (Command('convert'), '{} -S extra/imagemagick && convert'.format(pacman_cmd), PKGFILE_OUTPUT_CONVERT), | ||||||
|         PKGFILE_OUTPUT_CONVERT)]) |     (Command('sudo convert'), '{} -S extra/imagemagick && sudo convert'.format(pacman_cmd), PKGFILE_OUTPUT_CONVERT)]) | ||||||
| @patch('thefuck.rules.pacman.subprocess') | @patch('thefuck.rules.pacman.subprocess') | ||||||
| @patch.multiple(pacman, create=True, pacman=pacman_cmd) | @patch.multiple(pacman, create=True, pacman=pacman_cmd) | ||||||
| def test_get_new_command_mocked(subp_mock, command, new_command, return_value): | def test_get_new_command_mocked(subp_mock, command, new_command, return_value): | ||||||
|     subp_mock.check_output.return_value = return_value |     subp_mock.check_output.return_value = return_value | ||||||
|     assert get_new_command(command, None) == new_command |     assert get_new_command(command, None) == new_command | ||||||
|     assert subp_mock.check_output.called |  | ||||||
|   | |||||||
| @@ -1,12 +1,21 @@ | |||||||
| import subprocess | import subprocess | ||||||
| from thefuck.utils import DEVNULL, which | from thefuck.utils import DEVNULL, which | ||||||
| from thefuck import shells | from thefuck import shells | ||||||
|  | from thefuck.utils import memoize | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @memoize | ||||||
| def __get_pkgfile(command): | def __get_pkgfile(command): | ||||||
|     try: |     try: | ||||||
|  |         command = command.script | ||||||
|  |  | ||||||
|  |         if command.startswith('sudo'): | ||||||
|  |             command = command[5:] | ||||||
|  |  | ||||||
|  |         command = command.split(" ")[0] | ||||||
|  |  | ||||||
|         return subprocess.check_output( |         return subprocess.check_output( | ||||||
|             ['pkgfile', '-b', '-v', command.script.split(" ")[0]], |             ['pkgfile', '-b', '-v', command], | ||||||
|             universal_newlines=True, stderr=DEVNULL |             universal_newlines=True, stderr=DEVNULL | ||||||
|         ).split() |         ).split() | ||||||
|     except subprocess.CalledProcessError: |     except subprocess.CalledProcessError: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user