helper.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. require 'rubygems'
  2. begin
  3. gem 'minitest'
  4. rescue Gem::LoadError
  5. end
  6. require 'minitest/autorun'
  7. require 'rake'
  8. require 'tmpdir'
  9. require File.expand_path('../file_creation', __FILE__)
  10. begin
  11. require 'test/ruby/envutil'
  12. rescue LoadError
  13. # for ruby trunk
  14. end
  15. class Rake::TestCase < MiniTest::Unit::TestCase
  16. include FileCreation
  17. include Rake::DSL
  18. class TaskManager
  19. include Rake::TaskManager
  20. end
  21. RUBY = defined?(EnvUtil) ? EnvUtil.rubybin : Gem.ruby
  22. def setup
  23. ARGV.clear
  24. @orig_PWD = Dir.pwd
  25. @orig_APPDATA = ENV['APPDATA']
  26. @orig_HOME = ENV['HOME']
  27. @orig_HOMEDRIVE = ENV['HOMEDRIVE']
  28. @orig_HOMEPATH = ENV['HOMEPATH']
  29. @orig_RAKE_COLUMNS = ENV['RAKE_COLUMNS']
  30. @orig_RAKE_SYSTEM = ENV['RAKE_SYSTEM']
  31. @orig_RAKEOPT = ENV['RAKEOPT']
  32. @orig_USERPROFILE = ENV['USERPROFILE']
  33. ENV.delete 'RAKE_COLUMNS'
  34. ENV.delete 'RAKE_SYSTEM'
  35. ENV.delete 'RAKEOPT'
  36. tmpdir = Dir.chdir Dir.tmpdir do Dir.pwd end
  37. @tempdir = File.join tmpdir, "test_rake_#{$$}"
  38. FileUtils.mkdir_p @tempdir
  39. Dir.chdir @tempdir
  40. Rake.application = Rake::Application.new
  41. Rake::TaskManager.record_task_metadata = true
  42. RakeFileUtils.verbose_flag = false
  43. end
  44. def teardown
  45. Dir.chdir @orig_PWD
  46. FileUtils.rm_rf @tempdir
  47. if @orig_APPDATA then
  48. ENV['APPDATA'] = @orig_APPDATA
  49. else
  50. ENV.delete 'APPDATA'
  51. end
  52. ENV['HOME'] = @orig_HOME
  53. ENV['HOMEDRIVE'] = @orig_HOMEDRIVE
  54. ENV['HOMEPATH'] = @orig_HOMEPATH
  55. ENV['RAKE_COLUMNS'] = @orig_RAKE_COLUMNS
  56. ENV['RAKE_SYSTEM'] = @orig_RAKE_SYSTEM
  57. ENV['RAKEOPT'] = @orig_RAKEOPT
  58. ENV['USERPROFILE'] = @orig_USERPROFILE
  59. end
  60. def ignore_deprecations
  61. Rake.application.options.ignore_deprecate = true
  62. yield
  63. ensure
  64. Rake.application.options.ignore_deprecate = false
  65. end
  66. def rake_system_dir
  67. @system_dir = 'system'
  68. FileUtils.mkdir_p @system_dir
  69. open File.join(@system_dir, 'sys1.rake'), 'w' do |io|
  70. io << <<-SYS
  71. task "sys1" do
  72. puts "SYS1"
  73. end
  74. SYS
  75. end
  76. ENV['RAKE_SYSTEM'] = @system_dir
  77. end
  78. def rakefile contents
  79. open 'Rakefile', 'w' do |io|
  80. io << contents
  81. end
  82. end
  83. def rakefile_access
  84. rakefile <<-ACCESS
  85. TOP_LEVEL_CONSTANT = 0
  86. def a_top_level_function
  87. end
  88. task :default => [:work, :obj, :const]
  89. task :work do
  90. begin
  91. a_top_level_function
  92. puts "GOOD:M Top level methods can be called in tasks"
  93. rescue NameError => ex
  94. puts "BAD:M Top level methods can not be called in tasks"
  95. end
  96. end
  97. # TODO: remove `disabled_' when DeprecatedObjectDSL removed
  98. task :obj
  99. task :disabled_obj do
  100. begin
  101. Object.new.instance_eval { task :xyzzy }
  102. puts "BAD:D Rake DSL are polluting objects"
  103. rescue StandardError => ex
  104. puts "GOOD:D Rake DSL are not polluting objects"
  105. end
  106. end
  107. task :const do
  108. begin
  109. TOP_LEVEL_CONSTANT
  110. puts "GOOD:C Top level constants are available in tasks"
  111. rescue StandardError => ex
  112. puts "BAD:C Top level constants are NOT available in tasks"
  113. end
  114. end
  115. ACCESS
  116. end
  117. def rakefile_chains
  118. rakefile <<-DEFAULT
  119. task :default => "play.app"
  120. file "play.scpt" => "base" do |t|
  121. cp t.prerequisites.first, t.name
  122. end
  123. rule ".app" => ".scpt" do |t|
  124. cp t.source, t.name
  125. end
  126. file 'base' do
  127. touch 'base'
  128. end
  129. DEFAULT
  130. end
  131. def rakefile_comments
  132. rakefile <<-COMMENTS
  133. # comment for t1
  134. task :t1 do
  135. end
  136. # no comment or task because there's a blank line
  137. task :t2 do
  138. end
  139. desc "override comment for t3"
  140. # this is not the description
  141. multitask :t3 do
  142. end
  143. # this is not the description
  144. desc "override comment for t4"
  145. file :t4 do
  146. end
  147. COMMENTS
  148. end
  149. def rakefile_default
  150. rakefile <<-DEFAULT
  151. if ENV['TESTTOPSCOPE']
  152. puts "TOPSCOPE"
  153. end
  154. task :default do
  155. puts "DEFAULT"
  156. end
  157. task :other => [:default] do
  158. puts "OTHER"
  159. end
  160. task :task_scope do
  161. if ENV['TESTTASKSCOPE']
  162. puts "TASKSCOPE"
  163. end
  164. end
  165. DEFAULT
  166. end
  167. def rakefile_dryrun
  168. rakefile <<-DRYRUN
  169. task :default => ["temp_main"]
  170. file "temp_main" => [:all_apps] do touch "temp_main" end
  171. task :all_apps => [:one, :two]
  172. task :one => ["temp_one"]
  173. task :two => ["temp_two"]
  174. file "temp_one" do |t|
  175. touch "temp_one"
  176. end
  177. file "temp_two" do |t|
  178. touch "temp_two"
  179. end
  180. task :clean do
  181. ["temp_one", "temp_two", "temp_main"].each do |file|
  182. rm_f file
  183. end
  184. end
  185. DRYRUN
  186. FileUtils.touch 'temp_main'
  187. FileUtils.touch 'temp_two'
  188. end
  189. def rakefile_extra
  190. rakefile 'task :default'
  191. FileUtils.mkdir_p 'rakelib'
  192. open File.join('rakelib', 'extra.rake'), 'w' do |io|
  193. io << <<-EXTRA_RAKE
  194. # Added for testing
  195. namespace :extra do
  196. desc "An Extra Task"
  197. task :extra do
  198. puts "Read all about it"
  199. end
  200. end
  201. EXTRA_RAKE
  202. end
  203. end
  204. def rakefile_file_creation
  205. rakefile <<-'FILE_CREATION'
  206. N = 2
  207. task :default => :run
  208. BUILD_DIR = 'build'
  209. task :clean do
  210. rm_rf 'build'
  211. rm_rf 'src'
  212. end
  213. task :run
  214. TARGET_DIR = 'build/copies'
  215. FileList['src/*'].each do |src|
  216. directory TARGET_DIR
  217. target = File.join TARGET_DIR, File.basename(src)
  218. file target => [src, TARGET_DIR] do
  219. cp src, target
  220. # sleep 3 if src !~ /foo#{N-1}$/ # I'm commenting out this sleep, it doesn't seem to do anything.
  221. end
  222. task :run => target
  223. end
  224. task :prep => :clean do
  225. mkdir_p 'src'
  226. N.times do |n|
  227. touch "src/foo#{n}"
  228. end
  229. end
  230. FILE_CREATION
  231. end
  232. def rakefile_imports
  233. rakefile <<-IMPORTS
  234. require 'rake/loaders/makefile'
  235. task :default
  236. task :other do
  237. puts "OTHER"
  238. end
  239. file "dynamic_deps" do |t|
  240. open(t.name, "w") do |f| f.puts "puts 'DYNAMIC'" end
  241. end
  242. import "dynamic_deps"
  243. import "static_deps"
  244. import "static_deps"
  245. import "deps.mf"
  246. puts "FIRST"
  247. IMPORTS
  248. open 'deps.mf', 'w' do |io|
  249. io << <<-DEPS
  250. default: other
  251. DEPS
  252. end
  253. open "static_deps", "w" do |f|
  254. f.puts 'puts "STATIC"'
  255. end
  256. end
  257. def rakefile_multidesc
  258. rakefile <<-MULTIDESC
  259. task :b
  260. desc "A"
  261. task :a
  262. desc "B"
  263. task :b
  264. desc "A2"
  265. task :a
  266. task :c
  267. desc "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  268. task :d
  269. MULTIDESC
  270. end
  271. def rakefile_namespace
  272. rakefile <<-NAMESPACE
  273. desc "copy"
  274. task :copy do
  275. puts "COPY"
  276. end
  277. namespace "nest" do
  278. desc "nest copy"
  279. task :copy do
  280. puts "NEST COPY"
  281. end
  282. task :xx => :copy
  283. end
  284. anon_ns = namespace do
  285. desc "anonymous copy task"
  286. task :copy do
  287. puts "ANON COPY"
  288. end
  289. end
  290. desc "Top level task to run the anonymous version of copy"
  291. task :anon => anon_ns[:copy]
  292. namespace "very" do
  293. namespace "nested" do
  294. task "run" => "rake:copy"
  295. end
  296. end
  297. namespace "a" do
  298. desc "Run task in the 'a' namespace"
  299. task "run" do
  300. puts "IN A"
  301. end
  302. end
  303. namespace "b" do
  304. desc "Run task in the 'b' namespace"
  305. task "run" => "a:run" do
  306. puts "IN B"
  307. end
  308. end
  309. namespace "file1" do
  310. file "xyz.rb" do
  311. puts "XYZ1"
  312. end
  313. end
  314. namespace "file2" do
  315. file "xyz.rb" do
  316. puts "XYZ2"
  317. end
  318. end
  319. namespace "scopedep" do
  320. task :prepare do
  321. touch "scopedep.rb"
  322. puts "PREPARE"
  323. end
  324. file "scopedep.rb" => [:prepare] do
  325. puts "SCOPEDEP"
  326. end
  327. end
  328. NAMESPACE
  329. end
  330. def rakefile_nosearch
  331. FileUtils.touch 'dummy'
  332. end
  333. def rakefile_rakelib
  334. FileUtils.mkdir_p 'rakelib'
  335. Dir.chdir 'rakelib' do
  336. open 'test1.rb', 'w' do |io|
  337. io << <<-TEST1
  338. task :default do
  339. puts "TEST1"
  340. end
  341. TEST1
  342. end
  343. open 'test2.rake', 'w' do |io|
  344. io << <<-TEST1
  345. task :default do
  346. puts "TEST2"
  347. end
  348. TEST1
  349. end
  350. end
  351. end
  352. def rakefile_rbext
  353. open 'rakefile.rb', 'w' do |io|
  354. io << 'task :default do puts "OK" end'
  355. end
  356. end
  357. def rakefile_unittest
  358. rakefile '# Empty Rakefile for Unit Test'
  359. readme = File.join 'subdir', 'README'
  360. FileUtils.mkdir_p File.dirname readme
  361. FileUtils.touch readme
  362. end
  363. def rakefile_verbose
  364. rakefile <<-VERBOSE
  365. task :standalone_verbose_true do
  366. verbose true
  367. sh "#{RUBY} -e '0'"
  368. end
  369. task :standalone_verbose_false do
  370. verbose false
  371. sh "#{RUBY} -e '0'"
  372. end
  373. task :inline_verbose_default do
  374. sh "#{RUBY} -e '0'"
  375. end
  376. task :inline_verbose_false do
  377. sh "#{RUBY} -e '0'", :verbose => false
  378. end
  379. task :inline_verbose_true do
  380. sh "#{RUBY} -e '0'", :verbose => true
  381. end
  382. task :block_verbose_true do
  383. verbose(true) do
  384. sh "#{RUBY} -e '0'"
  385. end
  386. end
  387. task :block_verbose_false do
  388. verbose(false) do
  389. sh "#{RUBY} -e '0'"
  390. end
  391. end
  392. VERBOSE
  393. end
  394. end