From f9d2af0b24f109874951162bd3fa761c648038ab Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Wed, 30 Apr 2008 22:40:30 -0600 Subject: [PATCH] make sudo helper play nicely with complex commands --- CHANGELOG | 2 ++ lib/capistrano/command.rb | 4 ++-- lib/capistrano/configuration/actions/invocation.rb | 2 +- test/configuration/actions/invocation_test.rb | 10 +++++----- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index c35925e4..c97fba64 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *unreleased* +* Make sudo helper play nicely with complex command chains [Jamis Buck] + * Expand file-transfer options with new upload() and download() helpers. [Jamis Buck] * Allow SCP transfers in addition to SFTP. [Jamis Buck] diff --git a/lib/capistrano/command.rb b/lib/capistrano/command.rb index c08b7c51..32a1aaa8 100644 --- a/lib/capistrano/command.rb +++ b/lib/capistrano/command.rb @@ -82,12 +82,12 @@ module Capistrano if options[:shell] == false shell = nil else - shell = "#{options[:shell] || "sh"} -c" + shell = [options.fetch(:shell, "sh"), "-c"].join(" ") cmd = cmd.gsub(/[$\\`"]/) { |m| "\\#{m}" } cmd = "\"#{cmd}\"" end - command_line = [environment, shell, cmd].compact.join(" ") + command_line = [environment, options[:command_prefix], shell, cmd].compact.join(" ") ch.exec(command_line) ch.send_data(options[:data]) if options[:data] diff --git a/lib/capistrano/configuration/actions/invocation.rb b/lib/capistrano/configuration/actions/invocation.rb index b2f4f342..85ee1b8a 100644 --- a/lib/capistrano/configuration/actions/invocation.rb +++ b/lib/capistrano/configuration/actions/invocation.rb @@ -70,7 +70,7 @@ module Capistrano as = options.delete(:as) user = as && "-u #{as}" - command = [fetch(:sudo, "sudo"), "-p '#{sudo_prompt}'", user, command].compact.join(" ") + options[:command_prefix] = [fetch(:sudo, "sudo"), "-p '#{sudo_prompt}'", user].compact.join(" ") run(command, options, &sudo_behavior_callback(block)) end diff --git a/test/configuration/actions/invocation_test.rb b/test/configuration/actions/invocation_test.rb index b260d29a..6e5fca74 100644 --- a/test/configuration/actions/invocation_test.rb +++ b/test/configuration/actions/invocation_test.rb @@ -97,29 +97,29 @@ class ConfigurationActionsInvocationTest < Test::Unit::TestCase end def test_sudo_should_default_to_sudo - @config.expects(:run).with("sudo -p 'sudo password: ' ls", {}) + @config.expects(:run).with("ls", :command_prefix => "sudo -p 'sudo password: '") @config.sudo "ls" end def test_sudo_should_use_sudo_variable_definition - @config.expects(:run).with("/opt/local/bin/sudo -p 'sudo password: ' ls", {}) + @config.expects(:run).with("ls", :command_prefix => "/opt/local/bin/sudo -p 'sudo password: '") @config.options[:sudo] = "/opt/local/bin/sudo" @config.sudo "ls" end def test_sudo_should_interpret_as_option_as_user - @config.expects(:run).with("sudo -p 'sudo password: ' -u app ls", {}) + @config.expects(:run).with("ls", :command_prefix => "sudo -p 'sudo password: ' -u app") @config.sudo "ls", :as => "app" end def test_sudo_should_pass_options_through_to_run - @config.expects(:run).with("sudo -p 'sudo password: ' ls", :foo => "bar") + @config.expects(:run).with("ls", :command_prefix => "sudo -p 'sudo password: '", :foo => "bar") @config.sudo "ls", :foo => "bar" end def test_sudo_should_interpret_sudo_prompt_variable_as_custom_prompt @config.set :sudo_prompt, "give it to me: " - @config.expects(:run).with("sudo -p 'give it to me: ' ls", {}) + @config.expects(:run).with("ls", :command_prefix => "sudo -p 'give it to me: '") @config.sudo "ls" end