1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-18 12:06:04 +00:00

Added tests for su rule

This commit is contained in:
Brandon Moore 2019-11-10 21:34:31 -05:00
parent cdb1965c9b
commit f82ba5ff21
No known key found for this signature in database
GPG Key ID: C7A3DAE4ADF4F8ED
2 changed files with 28 additions and 6 deletions

25
tests/rules/test_su.py Normal file
View File

@ -0,0 +1,25 @@
import pytest
from thefuck.rules.su import match, get_new_command
from thefuck.types import Command
@pytest.mark.parametrize('output', [
'command not found: sudo'])
def test_match(output):
assert match(Command('', output))
def test_not_match():
assert not match(Command('', ''))
assert not match(Command('sudo ls', 'Permission denied'))
assert not match(Command('su -c ls', 'Permission denied'))
assert not match(Command('ls', 'command not found: ls'))
@pytest.mark.parametrize('before, after', [
('sudo ls', 'su -c "ls"'),
('sudo echo a > b', 'su -c "echo a > b"'),
('sudo echo "a" >> b', 'su -c "echo \\"a\\" >> b"'),
('sudo mkdir && touch a', 'su -c "mkdir && touch a"')])
def test_get_new_command(before, after):
assert get_new_command(Command(before, '')) == after

View File

@ -6,10 +6,7 @@ def match(command):
def get_new_command(command):
if '&&' in command.script:
return u'su -c "sh -c "{}""'.format(" ".join([part for part in command.script_parts if part != "sudo"]))
elif '>' in command.script:
return u'su -c "sh -c "{}""'.format(command.script.replace('"', '\\"'))
else:
return u'su -c {}'.format(command.script)
return u'su -c "{}"'.format(command.script.replace('"', '\\"').replace('sudo ', ''))
priority = 1200