From f249098336c438b107fca70fcd50fc57a3c8fd46 Mon Sep 17 00:00:00 2001 From: makalaaneesh Date: Wed, 23 Dec 2015 14:35:47 +0530 Subject: [PATCH] sudo sh execute for && in commands - preventing double sudo --- tests/rules/test_sudo.py | 3 ++- thefuck/rules/sudo.py | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/rules/test_sudo.py b/tests/rules/test_sudo.py index a065b355..e36001db 100644 --- a/tests/rules/test_sudo.py +++ b/tests/rules/test_sudo.py @@ -25,6 +25,7 @@ def test_not_match(): @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"')]) + ('echo "a" >> b', 'sudo sh -c "echo \\"a\\" >> b"'), + ('mkdir && touch a', 'sudo sh -c "mkdir && touch a"')]) def test_get_new_command(before, after): assert get_new_command(Command(before)) == after diff --git a/thefuck/rules/sudo.py b/thefuck/rules/sudo.py index 97114441..3211acfa 100644 --- a/thefuck/rules/sudo.py +++ b/thefuck/rules/sudo.py @@ -21,7 +21,7 @@ patterns = ['permission denied', def match(command): - if command.script_parts and command.script_parts[0] == 'sudo': + if command.script_parts and '&&' not in command.script_parts and command.script_parts[0] == 'sudo': return False for pattern in patterns: @@ -32,7 +32,9 @@ def match(command): def get_new_command(command): - if '>' in command.script: + if '&&' in command.script: + return u'sudo sh -c "{}"'.format(" ".join([part for part in command.script_parts if part != "sudo"])) + elif '>' in command.script: return u'sudo sh -c "{}"'.format(command.script.replace('"', '\\"')) else: return u'sudo {}'.format(command.script)