2007-02-28 14:13:13 -05:00
require " #{ File . dirname ( __FILE__ ) } /utils "
2007-02-26 18:59:33 -05:00
require 'capistrano/task_definition'
class TaskDefinitionTest < Test :: Unit :: TestCase
def setup
@namespace = namespace do | s |
role ( s , :app , " app1 " , :primary = > true )
role ( s , :app , " app2 " , " app3 " )
role ( s , :web , " web1 " , " web2 " )
role ( s , :report , " app2 " , :no_deploy = > true )
role ( s , :file , " file " , :no_deploy = > true )
end
end
def test_task_without_roles_should_apply_to_all_defined_hosts
2007-03-04 13:19:26 -05:00
task = new_task ( :testing )
2007-02-26 18:59:33 -05:00
assert_equal %w( app1 app2 app3 web1 web2 file ) . sort , task . servers . map { | s | s . host } . sort
end
def test_task_with_explicit_role_list_should_apply_only_to_those_roles
task = new_task ( :testing , @namespace , :roles = > %w( app web ) )
assert_equal %w( app1 app2 app3 web1 web2 ) . sort , task . servers . map { | s | s . host } . sort
end
def test_task_with_single_role_should_apply_only_to_that_role
task = new_task ( :testing , @namespace , :roles = > :web )
assert_equal %w( web1 web2 ) . sort , task . servers . map { | s | s . host } . sort
end
def test_task_with_hosts_option_should_apply_only_to_those_hosts
task = new_task ( :testing , @namespace , :hosts = > %w( foo bar ) )
assert_equal %w( foo bar ) . sort , task . servers . map { | s | s . host } . sort
end
def test_task_with_single_hosts_option_should_apply_only_to_that_host
task = new_task ( :testing , @namespace , :hosts = > " foo " )
assert_equal %w( foo ) . sort , task . servers . map { | s | s . host } . sort
end
def test_task_with_roles_as_environment_variable_should_apply_only_to_that_role
ENV [ 'ROLES' ] = " app,file "
2007-03-04 13:19:26 -05:00
task = new_task ( :testing )
2007-02-26 18:59:33 -05:00
assert_equal %w( app1 app2 app3 file ) . sort , task . servers . map { | s | s . host } . sort
ensure
ENV [ 'ROLES' ] = nil
end
def test_task_with_hosts_as_environment_variable_should_apply_only_to_those_hosts
ENV [ 'HOSTS' ] = " foo,bar "
2007-03-04 13:19:26 -05:00
task = new_task ( :testing )
2007-02-26 18:59:33 -05:00
assert_equal %w( foo bar ) . sort , task . servers . map { | s | s . host } . sort
ensure
ENV [ 'HOSTS' ] = nil
end
def test_task_with_only_should_apply_only_to_matching_tasks
task = new_task ( :testing , @namespace , :roles = > :app , :only = > { :primary = > true } )
assert_equal %w( app1 ) , task . servers . map { | s | s . host }
end
def test_task_with_except_should_apply_only_to_matching_tasks
task = new_task ( :testing , @namespace , :except = > { :no_deploy = > true } )
assert_equal %w( app1 app2 app3 web1 web2 ) . sort , task . servers . map { | s | s . host } . sort
end
def test_fqn_at_top_level_should_be_task_name
2007-03-04 13:19:26 -05:00
task = new_task ( :testing )
2007-02-26 18:59:33 -05:00
assert_equal " testing " , task . fully_qualified_name
end
def test_fqn_in_namespace_should_include_namespace_fqn
ns = namespace ( " outer:inner " )
task = new_task ( :testing , ns )
assert_equal " outer:inner:testing " , task . fully_qualified_name
end
2007-03-04 13:19:26 -05:00
def test_fqn_at_top_level_when_default_should_be_default
task = new_task ( :default )
assert_equal " default " , task . fully_qualified_name
end
def test_fqn_in_namespace_when_default_should_be_namespace_fqn
ns = namespace ( " outer:inner " )
task = new_task ( :default , ns )
ns . stubs ( :default_task = > task )
assert_equal " outer:inner " , task . fully_qualified_name
end
2007-02-26 18:59:33 -05:00
def test_task_should_require_block
assert_raises ( ArgumentError ) do
Capistrano :: TaskDefinition . new ( :testing , @namespace )
end
end
2007-03-04 13:19:26 -05:00
def test_description_should_return_empty_string_if_not_given
assert_equal " " , new_task ( :testing ) . description
end
def test_description_should_return_desc_attribute
assert_equal " something " , new_task ( :testing , @namespace , :desc = > " something " ) . description
end
def test_description_should_strip_leading_and_trailing_whitespace
assert_equal " something " , new_task ( :testing , @namespace , :desc = > " something " ) . description
end
def test_description_should_normalize_newlines
assert_equal " a \n b \n c " , new_task ( :testing , @namespace , :desc = > " a \n b \r \n c " ) . description
end
2007-03-30 14:46:40 -04:00
def test_description_should_detect_and_remove_indentation
desc = <<-DESC
Here is some indented text \
and I want all of this to \
run together on a single line , \
without any extraneous spaces .
additional indentation will
be preserved .
DESC
task = new_task ( :testing , @namespace , :desc = > desc )
assert_equal " Here is some indented text and I want all of this to run together on a single line, without any extraneous spaces. \n \n additional indentation will \n be preserved. " , task . description
end
def test_description_munging_should_be_sensitive_to_code_blocks
desc = <<-DESC
Here is a line \
wrapped with spacing in it .
foo bar
baz bang
DESC
task = new_task ( :testing , @namespace , :desc = > desc )
assert_equal " Here is a line wrapped with spacing in it. \n \n foo bar \n baz bang " , task . description
end
2007-03-04 13:19:26 -05:00
def test_task_brief_description_should_return_first_sentence_in_description
desc = " This is the task. It does all kinds of things. "
task = new_task ( :testing , @namespace , :desc = > desc )
assert_equal " This is the task. " , task . brief_description
end
def test_task_brief_description_should_truncate_if_length_given
desc = " This is the task that does all kinds of things. And then some. "
task = new_task ( :testing , @namespace , :desc = > desc )
assert_equal " This is the task ... " , task . brief_description ( 20 )
end
2007-02-26 18:59:33 -05:00
private
def namespace ( fqn = nil )
2007-03-04 13:19:26 -05:00
space = stub ( :roles = > { } , :fully_qualified_name = > fqn , :default_task = > nil )
2007-02-26 18:59:33 -05:00
yield ( space ) if block_given?
space
end
def role ( space , name , * args )
opts = args . last . is_a? ( Hash ) ? args . pop : { }
space . roles [ name ] || = [ ]
space . roles [ name ] . concat ( args . map { | h | Capistrano :: ServerDefinition . new ( h , opts ) } )
end
2007-03-04 13:19:26 -05:00
def new_task ( name , namespace = @namespace , options = { } , & block )
2007-02-26 18:59:33 -05:00
block || = Proc . new { }
task = Capistrano :: TaskDefinition . new ( name , namespace , options , & block )
assert_equal block , task . body
return task
end
end