1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-18 20:11:17 +00:00

#N/A: Add sudo_command_from_user_path rule

This commit is contained in:
Vladimir Iakovlev 2017-02-09 16:09:37 +01:00
parent 6f842ab747
commit bbed17fe07
3 changed files with 61 additions and 0 deletions

View File

@ -235,6 +235,7 @@ using the matched rule and runs it. Rules enabled by default are as follows:
* `sl_ls` – changes `sl` to `ls`;
* `ssh_known_hosts` – removes host from `known_hosts` on warning;
* `sudo` – prepends `sudo` to previous command if it failed because of permissions;
* `sudo_command_from_user_path` – runs commands from users `$PATH` with `sudo`;
* `switch_lang` – switches command from your local layout to en;
* `systemctl` – correctly orders parameters of confusing `systemctl`;
* `test.py` – runs `py.test` instead of `test.py`;

View File

@ -0,0 +1,39 @@
import pytest
from thefuck.rules.sudo_command_from_user_path import match, get_new_command
from tests.utils import Command
stderr = 'sudo: {}: command not found'
@pytest.fixture(autouse=True)
def which(mocker):
return mocker.patch('thefuck.rules.sudo_command_from_user_path.which',
return_value='/usr/bin/app')
@pytest.mark.parametrize('script, stderr', [
('sudo npm install -g react-native-cli', stderr.format('npm')),
('sudo -u app appcfg update .', stderr.format('appcfg'))])
def test_match(script, stderr):
assert match(Command(script, stderr=stderr))
@pytest.mark.parametrize('script, stderr, which_result', [
('npm --version', stderr.format('npm'), '/usr/bin/npm'),
('sudo npm --version', '', '/usr/bin/npm'),
('sudo npm --version', stderr.format('npm'), None)])
def test_not_match(which, script, stderr, which_result):
which.return_value = which_result
assert not match(Command(script, stderr=stderr))
@pytest.mark.parametrize('script, stderr, result', [
('sudo npm install -g react-native-cli',
stderr.format('npm'),
'sudo env "PATH=$PATH" npm install -g react-native-cli'),
('sudo -u app appcfg update .',
stderr.format('appcfg'),
'sudo -u app env "PATH=$PATH" appcfg update .')])
def test_get_new_command(script, stderr, result):
assert get_new_command(Command(script, stderr=stderr)) == result

View File

@ -0,0 +1,21 @@
import re
from thefuck.utils import for_app, which, replace_argument
def _get_command_name(command):
found = re.findall(r'sudo: (.*): command not found', command.stderr)
if found:
return found[0]
@for_app('sudo')
def match(command):
if 'command not found' in command.stderr:
command_name = _get_command_name(command)
return which(command_name)
def get_new_command(command):
command_name = _get_command_name(command)
return replace_argument(command.script, command_name,
u'env "PATH=$PATH" {}'.format(command_name))