Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8905

Show
Ignore:
Timestamp:
02/19/08 23:39:26 (7 months ago)
Author:
minam
Message:

Dynamic roles (closes #11107, thanks for the awesome patch!)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tools/capistrano/CHANGELOG

    r8903 r8905  
    11*SVN* 
     2 
     3* Dynamic roles (e.g. role(:app) { "host.name" }) [dmasover] 
    24 
    35* Implement Bzr#next_revision so that pending changes can be reported correctly [casret] 
  • tools/capistrano/lib/capistrano/configuration/roles.rb

    r6953 r8905  
    11require 'capistrano/server_definition' 
     2require 'capistrano/role' 
    23 
    34module Capistrano 
     
    1617      def initialize_with_roles(*args) #:nodoc: 
    1718        initialize_without_roles(*args) 
    18         @roles = Hash.new { |h,k| h[k] = []
     19        @roles = Hash.new { |h,k| h[k] = Role.new
    1920      end 
    2021 
     
    4243      # 
    4344      #   role :web, "web2", "web3", :user => "www", :port => 2345 
    44       def role(which, *args
     45      def role(which, *args, &block
    4546        options = args.last.is_a?(Hash) ? args.pop : {} 
    4647        which = which.to_sym 
     48        roles[which].push(block, options) if block_given? 
    4749        args.each { |host| roles[which] << ServerDefinition.new(host, options) } 
    4850      end 
  • tools/capistrano/test/configuration/roles_test.rb

    r6953 r8905  
    11require "#{File.dirname(__FILE__)}/../utils" 
    22require 'capistrano/configuration/roles' 
     3require 'capistrano/server_definition' 
    34 
    45class ConfigurationRolesTest < Test::Unit::TestCase 
     
    1718  end 
    1819 
     20  def assert_role_equals(list) 
     21    assert_equal list, @config.roles[:app].map { |s| s.host } 
     22  end 
     23 
    1924  def test_initialize_should_initialize_roles_collection 
    2025    assert @config.original_initialize_called 
     
    3035    @config.role :app, "app1.capistrano.test" 
    3136    assert_equal [:app], @config.roles.keys 
    32     assert_equal %w(app1.capistrano.test), @config.roles[:app].map { |s| s.host } 
     37    assert_role_equals %w(app1.capistrano.test) 
     38  end 
     39 
     40  def test_role_block_returning_single_string_is_added_to_roles_collection 
     41    @config.role :app do 
     42      'app1.capistrano.test' 
     43    end 
     44    assert_role_equals %w(app1.capistrano.test) 
    3345  end 
    3446 
     
    3648    @config.role :app, "app1.capistrano.test", "app2.capistrano.test" 
    3749    assert_equal [:app], @config.roles.keys 
    38     assert_equal %w(app1.capistrano.test app2.capistrano.test), @config.roles[:app].map { |s| s.host } 
     50    assert_role_equals %w(app1.capistrano.test app2.capistrano.test) 
     51  end 
     52 
     53  def test_role_with_block_and_strings_should_add_both_to_roles_collection 
     54    @config.role :app, 'app1.capistrano.test' do 
     55      'app2.capistrano.test' 
     56    end 
     57    assert_role_equals %w(app1.capistrano.test app2.capistrano.test) 
     58  end 
     59 
     60  def test_role_block_returning_array_should_add_each_to_roles_collection 
     61    @config.role :app do 
     62      ['app1.capistrano.test', 'app2.capistrano.test'] 
     63    end 
     64    assert_role_equals %w(app1.capistrano.test app2.capistrano.test) 
    3965  end 
    4066 
     
    4571    end 
    4672  end 
     73 
     74  def test_role_with_options_should_apply_options_to_block_results 
     75    @config.role :app, :extra => :value do 
     76      ['app1.capistrano.test', 'app2.capistrano.test'] 
     77    end 
     78    @config.roles[:app].each do |server| 
     79      assert_equal({:extra => :value}, server.options) 
     80    end 
     81  end 
     82 
     83  def test_options_should_apply_only_to_this_argument_set 
     84    @config.role :app, 'app1.capistrano.test', 'app2.capistrano.test' do 
     85      ['app3.capistrano.test', 'app4.capistrano.test'] 
     86    end 
     87    @config.role :app, 'app5.capistrano.test', 'app6.capistrano.test', :extra => :value do 
     88      ['app7.capistrano.test', 'app8.capistrano.test'] 
     89    end 
     90    @config.role :app, 'app9.capistrano.test' 
     91 
     92    option_hosts = ['app5.capistrano.test', 'app6.capistrano.test', 'app7.capistrano.test', 'app8.capistrano.test'] 
     93    @config.roles[:app].each do |server| 
     94      if (option_hosts.include? server.host) 
     95        assert_equal({:extra => :value}, server.options) 
     96      else 
     97        assert_not_equal({:extra => :value}, server.options) 
     98      end 
     99    end 
     100  end 
     101 
     102  # Here, the source should be more readable than the method name 
     103  def test_role_block_returns_options_hash_is_merged_with_role_options_argument 
     104    @config.role :app, :first => :one, :second => :two do 
     105      ['app1.capistrano.test', 'app2.capistrano.test', {:second => :please, :third => :three}] 
     106    end 
     107    @config.roles[:app].each do |server| 
     108      assert_equal({:first => :one, :second => :please, :third => :three}, server.options) 
     109    end 
     110  end 
     111 
     112  def test_role_block_can_override_role_options_argument 
     113    @config.role :app, :value => :wrong do 
     114      Capistrano::ServerDefinition.new('app.capistrano.test') 
     115    end 
     116    @config.roles[:app].servers 
     117    @config.roles[:app].servers.each do |server| 
     118      assert_not_equal({:value => :wrong}, server.options) 
     119    end 
     120  end 
     121 
     122  def test_role_block_can_return_nil 
     123    @config.role :app do 
     124      nil 
     125    end 
     126    assert_role_equals ([]) 
     127  end 
     128 
     129  def test_role_block_can_return_empty_array 
     130    @config.role :app do 
     131      [] 
     132    end 
     133    assert_role_equals ([]) 
     134  end 
    47135end