Browse files

Add support for password prompts for Mercurial SCM (closes #11187)

git-svn-id: http://svn.rubyonrails.org/rails/tools/capistrano@8925 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent c6ff8b9 commit 07d777b74625a5373e326b43304c592b05ff456f @jamis jamis committed Feb 23, 2008
Showing with 140 additions and 8 deletions.
  1. +2 −0 CHANGELOG
  2. +15 −8 lib/capistrano/recipes/deploy/scm/mercurial.rb
  3. +123 −0 test/deploy/scm/mercurial_test.rb
View
2 CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add support for password prompts from the Mercurial SCM [ches]
+
* Add support for :max_hosts option in task definition or run() [Rob Holland <rob@inversepath.com>]
* Distributed git support for better operability with remote_cache strategy [voidlock]
View
23 lib/capistrano/recipes/deploy/scm/mercurial.rb
@@ -70,24 +70,25 @@ def handle_data(state, stream, text)
logger.info "[#{stream}] #{text}"
case text
when /^user:/mi
- if variable(:scm_user)
- "#{variable(:scm_user)}\n"
+ # support :scm_user for backwards compatibility of this module
+ if user = variable(:scm_username) || variable(:scm_user)
+ "#{user}\n"
else
- raise "No variable :scm_user specified and Mercurial asked!\n" +
+ raise "No variable :scm_username specified and Mercurial asked!\n" +
"Prompt was: #{text}"
end
- when /^password:/mi
- if variable(:scm_password)
- "#{variable(:scm_password)}\n"
- else
+ when /\bpassword:/mi
+ unless pass = scm_password_or_prompt
+ # fall back on old behavior of erroring out with msg
raise "No variable :scm_password specified and Mercurial asked!\n" +
"Prompt was: #{text}"
end
+ "#{pass}\n"
when /yes\/no/i
"yes\n"
end
end
-
+
private
# Fine grained mercurial commands
@@ -122,6 +123,12 @@ def verbose
else "--verbose"
end
end
+
+ # honor Cap 2.1+'s :scm_prefer_prompt if present
+ def scm_password_or_prompt
+ @scm_password_or_prompt ||= variable(:scm_password) ||
+ (Capistrano::CLI.password_prompt("hg password: ") if variable(:scm_prefer_prompt))
+ end
end
end
View
123 test/deploy/scm/mercurial_test.rb
@@ -0,0 +1,123 @@
+require "#{File.dirname(__FILE__)}/../../utils"
+require 'capistrano/recipes/deploy/scm/mercurial'
+
+class DeploySCMMercurialTest < Test::Unit::TestCase
+ class TestSCM < Capistrano::Deploy::SCM::Mercurial
+ default_command "hg"
+ end
+
+ def setup
+ @config = { }
+ def @config.exists?(name); key?(name); end
+
+ @source = TestSCM.new(@config)
+ end
+
+ def test_head
+ assert_equal "tip", @source.head
+ end
+
+ def test_checkout
+ @config[:repository] = "http://example.com/project-hg"
+ dest = "/var/www"
+ assert_equal "hg clone --noupdate http://example.com/project-hg /var/www && hg update --repository /var/www --clean 8a8e00b8f11b", @source.checkout('8a8e00b8f11b', dest)
+ end
+
+ def test_diff
+ assert_equal "hg diff --rev tip", @source.diff('tip')
+ assert_equal "hg diff --rev 1 --rev 2", @source.diff('1', '2')
+ end
+
+ def test_log
+ assert_equal "hg log --rev 8a8e00b8f11b", @source.log('8a8e00b8f11b')
+ assert_equal "hg log --rev 0:3", @source.log('0', '3')
+ end
+
+ def test_query_revision
+ assert_equal "hg log -r 8a8e00b8f11b --template '{node|short}'", @source.query_revision('8a8e00b8f11b') { |o| o }
+ end
+
+ def test_username_should_be_backwards_compatible
+ # older versions of this module required :scm_user var instead
+ # of the currently preferred :scm_username
+ require 'capistrano/logger'
+ @config[:scm_user] = "fred"
+ text = "user:"
+ assert_equal "fred\n", @source.handle_data(:test_state, :test_stream, text)
+ # :scm_username takes priority
+ @config[:scm_username] = "wilma"
+ assert_equal "wilma\n", @source.handle_data(:test_state, :test_stream, text)
+ end
+
+ def test_sync
+ dest = "/var/www"
+ assert_equal "hg pull --repository /var/www && hg update --repository /var/www --clean 8a8e00b8f11b", @source.sync('8a8e00b8f11b', dest)
+
+ # With :scm_command
+ @config[:scm_command] = "/opt/local/bin/hg"
+ assert_equal "/opt/local/bin/hg pull --repository /var/www && /opt/local/bin/hg update --repository /var/www --clean 8a8e00b8f11b", @source.sync('8a8e00b8f11b', dest)
+ end
+
+ def test_export
+ dest = "/var/www"
+ assert_raise(NotImplementedError) { @source.export('8a8e00b8f11b', dest) }
+ end
+
+ def test_sends_password_if_set
+ require 'capistrano/logger'
+ text = "password:"
+ @config[:scm_password] = "opensesame"
+ assert_equal "opensesame\n", @source.handle_data(:test_state, :test_stream, text)
+ end
+
+ def test_prompts_for_password_if_preferred
+ require 'capistrano/logger'
+ require 'capistrano/cli'
+ Capistrano::CLI.stubs(:password_prompt).with("hg password: ").returns("opensesame")
+ @config[:scm_prefer_prompt] = true
+ text = "password:"
+ assert_equal "opensesame\n", @source.handle_data(:test_state, :test_stream, text)
+ end
+
+
+ # Tests from base_test.rb, makin' sure we didn't break anything up there!
+ def test_command_should_default_to_default_command
+ assert_equal "hg", @source.command
+ @source.local { assert_equal "hg", @source.command }
+ end
+
+ def test_command_should_use_scm_command_if_available
+ @config[:scm_command] = "/opt/local/bin/hg"
+ assert_equal "/opt/local/bin/hg", @source.command
+ end
+
+ def test_command_should_use_scm_command_in_local_mode_if_local_scm_command_not_set
+ @config[:scm_command] = "/opt/local/bin/hg"
+ @source.local { assert_equal "/opt/local/bin/hg", @source.command }
+ end
+
+ def test_command_should_use_local_scm_command_in_local_mode_if_local_scm_command_is_set
+ @config[:scm_command] = "/opt/local/bin/hg"
+ @config[:local_scm_command] = "/usr/local/bin/hg"
+ assert_equal "/opt/local/bin/hg", @source.command
+ @source.local { assert_equal "/usr/local/bin/hg", @source.command }
+ end
+
+ def test_command_should_use_default_if_scm_command_is_default
+ @config[:scm_command] = :default
+ assert_equal "hg", @source.command
+ end
+
+ def test_command_should_use_default_in_local_mode_if_local_scm_command_is_default
+ @config[:scm_command] = "/foo/bar/hg"
+ @config[:local_scm_command] = :default
+ @source.local { assert_equal "hg", @source.command }
+ end
+
+ def test_local_mode_proxy_should_treat_messages_as_being_in_local_mode
+ @config[:scm_command] = "/foo/bar/hg"
+ @config[:local_scm_command] = :default
+ assert_equal "hg", @source.local.command
+ assert_equal "/foo/bar/hg", @source.command
+ end
+end

0 comments on commit 07d777b

Please sign in to comment.