diff --git a/tests/rules/test_sudo.py b/tests/rules/test_sudo.py index 171088e3..001132f0 100644 --- a/tests/rules/test_sudo.py +++ b/tests/rules/test_sudo.py @@ -21,5 +21,9 @@ def test_not_match(): assert not match(Command(), None) -def test_get_new_command(): - assert get_new_command(Command('ls'), None) == 'sudo ls' +@pytest.mark.parametrize('before, after', [ + ('ls', 'sudo ls'), + ('echo a > b', 'sudo sh -c "echo a > b"'), + ('echo "a" >> b', 'sudo sh -c "echo \\"a\\" >> b"')]) +def test_get_new_command(before, after): + assert get_new_command(Command(before), None) == after diff --git a/thefuck/rules/sudo.py b/thefuck/rules/sudo.py index b42064ee..63e53c9f 100644 --- a/thefuck/rules/sudo.py +++ b/thefuck/rules/sudo.py @@ -28,4 +28,7 @@ def match(command, settings): def get_new_command(command, settings): - return u'sudo {}'.format(command.script) + if '>' in command.script: + return u'sudo sh -c "{}"'.format(command.script.replace('"', '\\"')) + else: + return u'sudo {}'.format(command.script)