PageRenderTime 83ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/jruby-1.1.6RC1/lib/ruby/gems/1.8/gems/rake-0.8.3/test/test_tasks.rb

https://bitbucket.org/nicksieger/advent-jruby
Ruby | 371 lines | 354 code | 15 blank | 2 comment | 0 complexity | 7ff1949e053d58ee9fd88bcc814d8534 MD5 | raw file
Possible License(s): CPL-1.0, AGPL-1.0, LGPL-2.1, JSON
  1. #!/usr/bin/env ruby
  2. require 'test/unit'
  3. require 'fileutils'
  4. require 'rake'
  5. require 'test/filecreation'
  6. require 'test/capture_stdout'
  7. ######################################################################
  8. class TestTask < Test::Unit::TestCase
  9. include CaptureStdout
  10. include Rake
  11. def setup
  12. Task.clear
  13. end
  14. def test_create
  15. arg = nil
  16. t = task(:name) { |task| arg = task; 1234 }
  17. assert_equal "name", t.name
  18. assert_equal [], t.prerequisites
  19. assert t.needed?
  20. t.execute(0)
  21. assert_equal t, arg
  22. assert_nil t.source
  23. assert_equal [], t.sources
  24. end
  25. def test_inspect
  26. t = task(:foo, :needs => [:bar, :baz])
  27. assert_equal "<Rake::Task foo => [bar, baz]>", t.inspect
  28. end
  29. def test_invoke
  30. runlist = []
  31. t1 = task(:t1 => [:t2, :t3]) { |t| runlist << t.name; 3321 }
  32. t2 = task(:t2) { |t| runlist << t.name }
  33. t3 = task(:t3) { |t| runlist << t.name }
  34. assert_equal ["t2", "t3"], t1.prerequisites
  35. t1.invoke
  36. assert_equal ["t2", "t3", "t1"], runlist
  37. end
  38. def test_invoke_with_circular_dependencies
  39. runlist = []
  40. t1 = task(:t1 => [:t2]) { |t| runlist << t.name; 3321 }
  41. t2 = task(:t2 => [:t1]) { |t| runlist << t.name }
  42. assert_equal ["t2"], t1.prerequisites
  43. assert_equal ["t1"], t2.prerequisites
  44. ex = assert_raise RuntimeError do
  45. t1.invoke
  46. end
  47. assert_match(/circular dependency/i, ex.message)
  48. assert_match(/t1 => t2 => t1/, ex.message)
  49. end
  50. def test_dry_run_prevents_actions
  51. Rake.application.options.dryrun = true
  52. runlist = []
  53. t1 = task(:t1) { |t| runlist << t.name; 3321 }
  54. out = capture_stdout { t1.invoke }
  55. assert_match(/execute .*t1/i, out)
  56. assert_match(/dry run/i, out)
  57. assert_no_match(/invoke/i, out)
  58. assert_equal [], runlist
  59. ensure
  60. Rake.application.options.dryrun = false
  61. end
  62. def test_tasks_can_be_traced
  63. Rake.application.options.trace = true
  64. t1 = task(:t1)
  65. out = capture_stdout {
  66. t1.invoke
  67. }
  68. assert_match(/invoke t1/i, out)
  69. assert_match(/execute t1/i, out)
  70. ensure
  71. Rake.application.options.trace = false
  72. end
  73. def test_no_double_invoke
  74. runlist = []
  75. t1 = task(:t1 => [:t2, :t3]) { |t| runlist << t.name; 3321 }
  76. t2 = task(:t2 => [:t3]) { |t| runlist << t.name }
  77. t3 = task(:t3) { |t| runlist << t.name }
  78. t1.invoke
  79. assert_equal ["t3", "t2", "t1"], runlist
  80. end
  81. def test_can_double_invoke_with_reenable
  82. runlist = []
  83. t1 = task(:t1) { |t| runlist << t.name }
  84. t1.invoke
  85. t1.reenable
  86. t1.invoke
  87. assert_equal ["t1", "t1"], runlist
  88. end
  89. def test_clear
  90. t = task("t" => "a") { }
  91. t.clear
  92. assert t.prerequisites.empty?, "prerequisites should be empty"
  93. assert t.actions.empty?, "actions should be empty"
  94. end
  95. def test_clear_prerequisites
  96. t = task("t" => ["a", "b"])
  97. assert_equal ['a', 'b'], t.prerequisites
  98. t.clear_prerequisites
  99. assert_equal [], t.prerequisites
  100. end
  101. def test_clear_actions
  102. t = task("t") { }
  103. t.clear_actions
  104. assert t.actions.empty?, "actions should be empty"
  105. end
  106. def test_find
  107. task :tfind
  108. assert_equal "tfind", Task[:tfind].name
  109. ex = assert_raises(RuntimeError) { Task[:leaves] }
  110. assert_equal "Don't know how to build task 'leaves'", ex.message
  111. end
  112. def test_defined
  113. assert ! Task.task_defined?(:a)
  114. task :a
  115. assert Task.task_defined?(:a)
  116. end
  117. def test_multi_invocations
  118. runs = []
  119. p = proc do |t| runs << t.name end
  120. task({:t1=>[:t2,:t3]}, &p)
  121. task({:t2=>[:t3]}, &p)
  122. task(:t3, &p)
  123. Task[:t1].invoke
  124. assert_equal ["t1", "t2", "t3"], runs.sort
  125. end
  126. def test_task_list
  127. task :t2
  128. task :t1 => [:t2]
  129. assert_equal ["t1", "t2"], Task.tasks.collect {|t| t.name}
  130. end
  131. def test_task_gives_name_on_to_s
  132. task :abc
  133. assert_equal "abc", Task[:abc].to_s
  134. end
  135. def test_symbols_can_be_prerequisites
  136. task :a => :b
  137. assert_equal ["b"], Task[:a].prerequisites
  138. end
  139. def test_strings_can_be_prerequisites
  140. task :a => "b"
  141. assert_equal ["b"], Task[:a].prerequisites
  142. end
  143. def test_arrays_can_be_prerequisites
  144. task :a => ["b", "c"]
  145. assert_equal ["b", "c"], Task[:a].prerequisites
  146. end
  147. def test_filelists_can_be_prerequisites
  148. task :a => FileList.new.include("b", "c")
  149. assert_equal ["b", "c"], Task[:a].prerequisites
  150. end
  151. def test_investigation_output
  152. t1 = task(:t1 => [:t2, :t3]) { |t| runlist << t.name; 3321 }
  153. task(:t2)
  154. task(:t3)
  155. out = t1.investigation
  156. assert_match(/class:\s*Rake::Task/, out)
  157. assert_match(/needed:\s*true/, out)
  158. assert_match(/pre-requisites:\s*--t2/, out)
  159. end
  160. def test_extended_comments
  161. desc %{
  162. This is a comment.
  163. And this is the extended comment.
  164. name -- Name of task to execute.
  165. rev -- Software revision to use.
  166. }
  167. t = task(:t, :name, :rev)
  168. assert_equal "[name,rev]", t.arg_description
  169. assert_equal "This is a comment.", t.comment
  170. assert_match(/^\s*name -- Name/, t.full_comment)
  171. assert_match(/^\s*rev -- Software/, t.full_comment)
  172. assert_match(/\A\s*This is a comment\.$/, t.full_comment)
  173. end
  174. def test_multiple_comments
  175. desc "line one"
  176. t = task(:t)
  177. desc "line two"
  178. task(:t)
  179. assert_equal "line one / line two", t.comment
  180. end
  181. def test_settable_comments
  182. t = task(:t)
  183. t.comment = "HI"
  184. assert_equal "HI", t.comment
  185. end
  186. end
  187. ######################################################################
  188. class TestTaskWithArguments < Test::Unit::TestCase
  189. include CaptureStdout
  190. include Rake
  191. def setup
  192. Task.clear
  193. end
  194. def test_no_args_given
  195. t = task :t
  196. assert_equal [], t.arg_names
  197. end
  198. def test_args_given
  199. t = task :t, :a, :b
  200. assert_equal [:a, :b], t.arg_names
  201. end
  202. def test_name_and_needs
  203. t = task(:t => [:pre])
  204. assert_equal "t", t.name
  205. assert_equal [], t.arg_names
  206. assert_equal ["pre"], t.prerequisites
  207. end
  208. def test_name_and_explicit_needs
  209. t = task(:t, :needs => [:pre])
  210. assert_equal "t", t.name
  211. assert_equal [], t.arg_names
  212. assert_equal ["pre"], t.prerequisites
  213. end
  214. def test_name_args_and_explicit_needs
  215. t = task(:t, :x, :y, :needs => [:pre])
  216. assert_equal "t", t.name
  217. assert_equal [:x, :y], t.arg_names
  218. assert_equal ["pre"], t.prerequisites
  219. end
  220. def test_illegal_keys_in_task_name_hash
  221. assert_raise RuntimeError do
  222. t = task(:t, :x, :y => 1, :needs => [:pre])
  223. end
  224. end
  225. def test_arg_list_is_empty_if_no_args_given
  226. t = task(:t) { |tt, args| assert_equal({}, args.to_hash) }
  227. t.invoke(1, 2, 3)
  228. end
  229. def test_tasks_can_access_arguments_as_hash
  230. t = task :t, :a, :b, :c do |tt, args|
  231. assert_equal({:a => 1, :b => 2, :c => 3}, args.to_hash)
  232. assert_equal 1, args[:a]
  233. assert_equal 2, args[:b]
  234. assert_equal 3, args[:c]
  235. assert_equal 1, args.a
  236. assert_equal 2, args.b
  237. assert_equal 3, args.c
  238. end
  239. t.invoke(1, 2, 3)
  240. end
  241. def test_actions_of_various_arity_are_ok_with_args
  242. notes = []
  243. t = task(:t, :x) do
  244. notes << :a
  245. end
  246. t.enhance do | |
  247. notes << :b
  248. end
  249. t.enhance do |task|
  250. notes << :c
  251. assert_kind_of Task, task
  252. end
  253. t.enhance do |t2, args|
  254. notes << :d
  255. assert_equal t, t2
  256. assert_equal({:x => 1}, args.to_hash)
  257. end
  258. assert_nothing_raised do t.invoke(1) end
  259. assert_equal [:a, :b, :c, :d], notes
  260. end
  261. def test_arguments_are_passed_to_block
  262. t = task(:t, :a, :b) { |tt, args|
  263. assert_equal( { :a => 1, :b => 2 }, args.to_hash )
  264. }
  265. t.invoke(1, 2)
  266. end
  267. def test_extra_parameters_are_ignored
  268. t = task(:t, :a) { |tt, args|
  269. assert_equal 1, args.a
  270. assert_nil args.b
  271. }
  272. t.invoke(1, 2)
  273. end
  274. def test_arguments_are_passed_to_all_blocks
  275. counter = 0
  276. t = task :t, :a
  277. task :t do |tt, args|
  278. assert_equal 1, args.a
  279. counter += 1
  280. end
  281. task :t do |tt, args|
  282. assert_equal 1, args.a
  283. counter += 1
  284. end
  285. t.invoke(1)
  286. assert_equal 2, counter
  287. end
  288. def test_block_with_no_parameters_is_ok
  289. t = task(:t) { }
  290. t.invoke(1, 2)
  291. end
  292. def test_name_with_args
  293. desc "T"
  294. t = task(:tt, :a, :b)
  295. assert_equal "tt", t.name
  296. assert_equal "T", t.comment
  297. assert_equal "[a,b]", t.arg_description
  298. assert_equal "tt[a,b]", t.name_with_args
  299. assert_equal [:a, :b],t.arg_names
  300. end
  301. def test_named_args_are_passed_to_prereqs
  302. value = nil
  303. pre = task(:pre, :rev) { |t, args| value = args.rev }
  304. t = task(:t, :name, :rev, :needs => [:pre])
  305. t.invoke("bill", "1.2")
  306. assert_equal "1.2", value
  307. end
  308. def test_args_not_passed_if_no_prereq_names
  309. pre = task(:pre) { |t, args|
  310. assert_equal({}, args.to_hash)
  311. assert_equal "bill", args.name
  312. }
  313. t = task(:t, :name, :rev, :needs => [:pre])
  314. t.invoke("bill", "1.2")
  315. end
  316. def test_args_not_passed_if_no_arg_names
  317. pre = task(:pre, :rev) { |t, args|
  318. assert_equal({}, args.to_hash)
  319. }
  320. t = task(:t, :needs => [:pre])
  321. t.invoke("bill", "1.2")
  322. end
  323. end