From b29113c22973227f6c103ba2ec46d473aa91e709 Mon Sep 17 00:00:00 2001 From: nvbn Date: Tue, 11 Aug 2015 01:15:05 +0300 Subject: [PATCH] #326 Add support of sudo with pipes --- tests/rules/test_sudo.py | 8 ++++++-- thefuck/rules/sudo.py | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) 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)