PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/gems/capistrano-2.5.9/test/configuration/namespace_dsl_test.rb

https://github.com/wangmh/webistrano
Ruby | 311 lines | 256 code | 55 blank | 0 comment | 2 complexity | ec6fe154e5d7ab8d5002998e5b329e42 MD5 | raw file
  1. require "utils"
  2. require 'capistrano/configuration/namespaces'
  3. class ConfigurationNamespacesDSLTest < Test::Unit::TestCase
  4. class MockConfig
  5. attr_reader :original_initialize_called, :options
  6. def initialize
  7. @original_initialize_called = true
  8. @options = {}
  9. end
  10. include Capistrano::Configuration::Namespaces
  11. end
  12. def setup
  13. @config = MockConfig.new
  14. end
  15. def test_initialize_should_initialize_collections
  16. assert @config.original_initialize_called
  17. assert @config.tasks.empty?
  18. assert @config.namespaces.empty?
  19. end
  20. def test_unqualified_task_should_define_task_at_top_namespace
  21. assert !@config.tasks.key?(:testing)
  22. @config.task(:testing) { puts "something" }
  23. assert @config.tasks.key?(:testing)
  24. end
  25. def test_qualification_should_define_task_within_namespace
  26. @config.namespace(:testing) do
  27. task(:nested) { puts "nested" }
  28. end
  29. assert !@config.tasks.key?(:nested)
  30. assert @config.namespaces.key?(:testing)
  31. assert @config.namespaces[:testing].tasks.key?(:nested)
  32. end
  33. def test_namespace_within_namespace_should_define_task_within_nested_namespace
  34. @config.namespace :outer do
  35. namespace :inner do
  36. task :nested do
  37. puts "nested"
  38. end
  39. end
  40. end
  41. assert !@config.tasks.key?(:nested)
  42. assert @config.namespaces.key?(:outer)
  43. assert @config.namespaces[:outer].namespaces.key?(:inner)
  44. assert @config.namespaces[:outer].namespaces[:inner].tasks.key?(:nested)
  45. end
  46. def test_pending_desc_should_apply_only_to_immediately_subsequent_task
  47. @config.desc "A description"
  48. @config.task(:testing) { puts "foo" }
  49. @config.task(:another) { puts "bar" }
  50. assert_equal "A description", @config.tasks[:testing].desc
  51. assert_nil @config.tasks[:another].desc
  52. end
  53. def test_pending_desc_should_apply_only_to_next_task_in_any_namespace
  54. @config.desc "A description"
  55. @config.namespace(:outer) { task(:testing) { puts "foo" } }
  56. assert_equal "A description", @config.namespaces[:outer].tasks[:testing].desc
  57. end
  58. def test_defining_task_without_block_should_raise_error
  59. assert_raises(ArgumentError) do
  60. @config.task(:testing)
  61. end
  62. end
  63. def test_defining_task_that_shadows_existing_method_should_raise_error
  64. assert_raises(ArgumentError) do
  65. @config.task(:sprintf) { puts "foo" }
  66. end
  67. end
  68. def test_defining_task_that_shadows_existing_namespace_should_raise_error
  69. @config.namespace(:outer) {}
  70. assert_raises(ArgumentError) do
  71. @config.task(:outer) { puts "foo" }
  72. end
  73. end
  74. def test_defining_namespace_that_shadows_existing_method_should_raise_error
  75. assert_raises(ArgumentError) do
  76. @config.namespace(:sprintf) {}
  77. end
  78. end
  79. def test_defining_namespace_that_shadows_existing_task_should_raise_error
  80. @config.task(:testing) { puts "foo" }
  81. assert_raises(ArgumentError) do
  82. @config.namespace(:testing) {}
  83. end
  84. end
  85. def test_defining_task_that_shadows_existing_task_should_not_raise_error
  86. @config.task(:original) { puts "foo" }
  87. assert_nothing_raised do
  88. @config.task(:original) { puts "bar" }
  89. end
  90. end
  91. def test_defining_ask_should_add_task_as_method
  92. assert !@config.methods.any? { |m| m.to_sym == :original }
  93. @config.task(:original) { puts "foo" }
  94. assert @config.methods.any? { |m| m.to_sym == :original }
  95. end
  96. def test_calling_defined_task_should_delegate_to_execute_task
  97. @config.task(:original) { puts "foo" }
  98. @config.expects(:execute_task).with(@config.tasks[:original])
  99. @config.original
  100. end
  101. def test_role_inside_namespace_should_raise_error
  102. assert_raises(NotImplementedError) do
  103. @config.namespace(:outer) do
  104. role :app, "hello"
  105. end
  106. end
  107. end
  108. def test_name_for_top_level_should_be_nil
  109. assert_nil @config.name
  110. end
  111. def test_parent_for_top_level_should_be_nil
  112. assert_nil @config.parent
  113. end
  114. def test_fqn_for_top_level_should_be_nil
  115. assert_nil @config.fully_qualified_name
  116. end
  117. def test_fqn_for_namespace_should_be_the_name_of_the_namespace
  118. @config.namespace(:outer) {}
  119. assert_equal "outer", @config.namespaces[:outer].fully_qualified_name
  120. end
  121. def test_parent_for_namespace_should_be_the_top_level
  122. @config.namespace(:outer) {}
  123. assert_equal @config, @config.namespaces[:outer].parent
  124. end
  125. def test_fqn_for_nested_namespace_should_be_color_delimited
  126. @config.namespace(:outer) { namespace(:inner) {} }
  127. assert_equal "outer:inner", @config.namespaces[:outer].namespaces[:inner].fully_qualified_name
  128. end
  129. def test_parent_for_nested_namespace_should_be_the_nesting_namespace
  130. @config.namespace(:outer) { namespace(:inner) {} }
  131. assert_equal @config.namespaces[:outer], @config.namespaces[:outer].namespaces[:inner].parent
  132. end
  133. def test_find_task_should_dereference_nested_tasks
  134. @config.namespace(:outer) do
  135. namespace(:inner) { task(:nested) { puts "nested" } }
  136. end
  137. task = @config.find_task("outer:inner:nested")
  138. assert_not_nil task
  139. assert_equal "outer:inner:nested", task.fully_qualified_name
  140. end
  141. def test_find_task_should_return_nil_if_no_task_matches
  142. assert_nil @config.find_task("outer:inner:nested")
  143. end
  144. def test_find_task_should_return_default_if_deferences_to_namespace_and_namespace_has_default
  145. @config.namespace(:outer) do
  146. namespace(:inner) { task(:default) { puts "nested" } }
  147. end
  148. task = @config.find_task("outer:inner")
  149. assert_not_nil task
  150. assert_equal :default, task.name
  151. assert_equal "outer:inner", task.namespace.fully_qualified_name
  152. end
  153. def test_find_task_should_return_nil_if_deferences_to_namespace_and_namespace_has_no_default
  154. @config.namespace(:outer) do
  155. namespace(:inner) { task(:nested) { puts "nested" } }
  156. end
  157. assert_nil @config.find_task("outer:inner")
  158. end
  159. def test_default_task_should_return_nil_for_top_level
  160. @config.task(:default) {}
  161. assert_nil @config.default_task
  162. end
  163. def test_default_task_should_return_nil_for_namespace_without_default
  164. @config.namespace(:outer) { task(:nested) { puts "nested" } }
  165. assert_nil @config.namespaces[:outer].default_task
  166. end
  167. def test_default_task_should_return_task_for_namespace_with_default
  168. @config.namespace(:outer) { task(:default) { puts "nested" } }
  169. task = @config.namespaces[:outer].default_task
  170. assert_not_nil task
  171. assert_equal :default, task.name
  172. end
  173. def test_task_list_should_return_only_tasks_immediately_within_namespace
  174. @config.task(:first) { puts "here" }
  175. @config.namespace(:outer) do
  176. task(:second) { puts "here" }
  177. namespace(:inner) do
  178. task(:third) { puts "here" }
  179. end
  180. end
  181. assert_equal %w(first), @config.task_list.map { |t| t.fully_qualified_name }
  182. end
  183. def test_task_list_with_all_should_return_all_tasks_under_this_namespace_recursively
  184. @config.task(:first) { puts "here" }
  185. @config.namespace(:outer) do
  186. task(:second) { puts "here" }
  187. namespace(:inner) do
  188. task(:third) { puts "here" }
  189. end
  190. end
  191. assert_equal %w(first outer:inner:third outer:second), @config.task_list(:all).map { |t| t.fully_qualified_name }.sort
  192. end
  193. def test_namespace_should_respond_to_its_parents_methods
  194. @config.namespace(:outer) {}
  195. ns = @config.namespaces[:outer]
  196. assert ns.respond_to?(:original_initialize_called)
  197. end
  198. def test_namespace_should_accept_respond_to_with_include_priv_parameter
  199. @config.namespace(:outer) {}
  200. ns = @config.namespaces[:outer]
  201. assert ns.respond_to?(:original_initialize_called, true)
  202. end
  203. def test_namespace_should_delegate_unknown_messages_to_its_parent
  204. @config.namespace(:outer) {}
  205. ns = @config.namespaces[:outer]
  206. assert ns.original_initialize_called
  207. end
  208. def test_namespace_should_not_understand_messages_that_neither_it_nor_its_parent_understands
  209. @config.namespace(:outer) {}
  210. ns = @config.namespaces[:outer]
  211. assert_raises(NoMethodError) { ns.alskdfjlsf }
  212. end
  213. def test_search_task_should_find_tasks_in_current_namespace
  214. @config.namespace(:outer) do
  215. namespace(:inner) do
  216. task(:third) { puts "here" }
  217. end
  218. end
  219. inner = @config.namespaces[:outer].namespaces[:inner]
  220. assert_equal inner.tasks[:third], inner.search_task(:third)
  221. end
  222. def test_search_task_should_find_tasks_in_parent_namespace
  223. @config.task(:first) { puts "here" }
  224. @config.namespace(:outer) do
  225. task(:second) { puts "here" }
  226. namespace(:inner) do
  227. task(:third) { puts "here" }
  228. end
  229. end
  230. inner = @config.namespaces[:outer].namespaces[:inner]
  231. assert_equal @config.tasks[:first], inner.search_task(:first)
  232. end
  233. def test_search_task_should_return_nil_if_no_tasks_are_found
  234. @config.namespace(:outer) { namespace(:inner) {} }
  235. inner = @config.namespaces[:outer].namespaces[:inner]
  236. assert_nil inner.search_task(:first)
  237. end
  238. def test_top_should_return_self_if_self_is_top
  239. assert_equal @config, @config.top
  240. end
  241. def test_top_should_return_parent_if_parent_is_top
  242. @config.namespace(:outer) {}
  243. assert_equal @config, @config.namespaces[:outer].top
  244. end
  245. def test_top_should_return_topmost_parent_if_self_is_deeply_nested
  246. @config.namespace(:outer) { namespace(:middle) { namespace(:inner) {} } }
  247. assert_equal @config, @config.namespaces[:outer].namespaces[:middle].namespaces[:inner].top
  248. end
  249. def test_find_task_should_return_nil_when_empty_inner_task
  250. @config.namespace :outer do
  251. namespace :inner do
  252. end
  253. end
  254. assert_nil @config.find_task("outer::inner")
  255. end
  256. end