123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492 |
- require 'rubygems'
- begin
- gem 'minitest'
- rescue Gem::LoadError
- end
- require 'minitest/autorun'
- require 'rake'
- require 'tmpdir'
- require File.expand_path('../file_creation', __FILE__)
- begin
- require 'test/ruby/envutil'
- rescue LoadError
- # for ruby trunk
- end
- class Rake::TestCase < MiniTest::Unit::TestCase
- include FileCreation
- include Rake::DSL
- class TaskManager
- include Rake::TaskManager
- end
- RUBY = defined?(EnvUtil) ? EnvUtil.rubybin : Gem.ruby
- def setup
- ARGV.clear
- @orig_PWD = Dir.pwd
- @orig_APPDATA = ENV['APPDATA']
- @orig_HOME = ENV['HOME']
- @orig_HOMEDRIVE = ENV['HOMEDRIVE']
- @orig_HOMEPATH = ENV['HOMEPATH']
- @orig_RAKE_COLUMNS = ENV['RAKE_COLUMNS']
- @orig_RAKE_SYSTEM = ENV['RAKE_SYSTEM']
- @orig_RAKEOPT = ENV['RAKEOPT']
- @orig_USERPROFILE = ENV['USERPROFILE']
- ENV.delete 'RAKE_COLUMNS'
- ENV.delete 'RAKE_SYSTEM'
- ENV.delete 'RAKEOPT'
- tmpdir = Dir.chdir Dir.tmpdir do Dir.pwd end
- @tempdir = File.join tmpdir, "test_rake_#{$$}"
- FileUtils.mkdir_p @tempdir
- Dir.chdir @tempdir
- Rake.application = Rake::Application.new
- Rake::TaskManager.record_task_metadata = true
- RakeFileUtils.verbose_flag = false
- end
- def teardown
- Dir.chdir @orig_PWD
- FileUtils.rm_rf @tempdir
- if @orig_APPDATA then
- ENV['APPDATA'] = @orig_APPDATA
- else
- ENV.delete 'APPDATA'
- end
- ENV['HOME'] = @orig_HOME
- ENV['HOMEDRIVE'] = @orig_HOMEDRIVE
- ENV['HOMEPATH'] = @orig_HOMEPATH
- ENV['RAKE_COLUMNS'] = @orig_RAKE_COLUMNS
- ENV['RAKE_SYSTEM'] = @orig_RAKE_SYSTEM
- ENV['RAKEOPT'] = @orig_RAKEOPT
- ENV['USERPROFILE'] = @orig_USERPROFILE
- end
- def ignore_deprecations
- Rake.application.options.ignore_deprecate = true
- yield
- ensure
- Rake.application.options.ignore_deprecate = false
- end
- def rake_system_dir
- @system_dir = 'system'
- FileUtils.mkdir_p @system_dir
- open File.join(@system_dir, 'sys1.rake'), 'w' do |io|
- io << <<-SYS
- task "sys1" do
- puts "SYS1"
- end
- SYS
- end
- ENV['RAKE_SYSTEM'] = @system_dir
- end
- def rakefile contents
- open 'Rakefile', 'w' do |io|
- io << contents
- end
- end
- def rakefile_access
- rakefile <<-ACCESS
- TOP_LEVEL_CONSTANT = 0
- def a_top_level_function
- end
- task :default => [:work, :obj, :const]
- task :work do
- begin
- a_top_level_function
- puts "GOOD:M Top level methods can be called in tasks"
- rescue NameError => ex
- puts "BAD:M Top level methods can not be called in tasks"
- end
- end
- # TODO: remove `disabled_' when DeprecatedObjectDSL removed
- task :obj
- task :disabled_obj do
- begin
- Object.new.instance_eval { task :xyzzy }
- puts "BAD:D Rake DSL are polluting objects"
- rescue StandardError => ex
- puts "GOOD:D Rake DSL are not polluting objects"
- end
- end
- task :const do
- begin
- TOP_LEVEL_CONSTANT
- puts "GOOD:C Top level constants are available in tasks"
- rescue StandardError => ex
- puts "BAD:C Top level constants are NOT available in tasks"
- end
- end
- ACCESS
- end
- def rakefile_chains
- rakefile <<-DEFAULT
- task :default => "play.app"
- file "play.scpt" => "base" do |t|
- cp t.prerequisites.first, t.name
- end
- rule ".app" => ".scpt" do |t|
- cp t.source, t.name
- end
- file 'base' do
- touch 'base'
- end
- DEFAULT
- end
- def rakefile_comments
- rakefile <<-COMMENTS
- # comment for t1
- task :t1 do
- end
- # no comment or task because there's a blank line
- task :t2 do
- end
- desc "override comment for t3"
- # this is not the description
- multitask :t3 do
- end
- # this is not the description
- desc "override comment for t4"
- file :t4 do
- end
- COMMENTS
- end
- def rakefile_default
- rakefile <<-DEFAULT
- if ENV['TESTTOPSCOPE']
- puts "TOPSCOPE"
- end
- task :default do
- puts "DEFAULT"
- end
- task :other => [:default] do
- puts "OTHER"
- end
- task :task_scope do
- if ENV['TESTTASKSCOPE']
- puts "TASKSCOPE"
- end
- end
- DEFAULT
- end
- def rakefile_dryrun
- rakefile <<-DRYRUN
- task :default => ["temp_main"]
- file "temp_main" => [:all_apps] do touch "temp_main" end
- task :all_apps => [:one, :two]
- task :one => ["temp_one"]
- task :two => ["temp_two"]
- file "temp_one" do |t|
- touch "temp_one"
- end
- file "temp_two" do |t|
- touch "temp_two"
- end
- task :clean do
- ["temp_one", "temp_two", "temp_main"].each do |file|
- rm_f file
- end
- end
- DRYRUN
- FileUtils.touch 'temp_main'
- FileUtils.touch 'temp_two'
- end
- def rakefile_extra
- rakefile 'task :default'
- FileUtils.mkdir_p 'rakelib'
- open File.join('rakelib', 'extra.rake'), 'w' do |io|
- io << <<-EXTRA_RAKE
- # Added for testing
- namespace :extra do
- desc "An Extra Task"
- task :extra do
- puts "Read all about it"
- end
- end
- EXTRA_RAKE
- end
- end
- def rakefile_file_creation
- rakefile <<-'FILE_CREATION'
- N = 2
- task :default => :run
- BUILD_DIR = 'build'
- task :clean do
- rm_rf 'build'
- rm_rf 'src'
- end
- task :run
- TARGET_DIR = 'build/copies'
- FileList['src/*'].each do |src|
- directory TARGET_DIR
- target = File.join TARGET_DIR, File.basename(src)
- file target => [src, TARGET_DIR] do
- cp src, target
- # sleep 3 if src !~ /foo#{N-1}$/ # I'm commenting out this sleep, it doesn't seem to do anything.
- end
- task :run => target
- end
- task :prep => :clean do
- mkdir_p 'src'
- N.times do |n|
- touch "src/foo#{n}"
- end
- end
- FILE_CREATION
- end
- def rakefile_imports
- rakefile <<-IMPORTS
- require 'rake/loaders/makefile'
- task :default
- task :other do
- puts "OTHER"
- end
- file "dynamic_deps" do |t|
- open(t.name, "w") do |f| f.puts "puts 'DYNAMIC'" end
- end
- import "dynamic_deps"
- import "static_deps"
- import "static_deps"
- import "deps.mf"
- puts "FIRST"
- IMPORTS
- open 'deps.mf', 'w' do |io|
- io << <<-DEPS
- default: other
- DEPS
- end
- open "static_deps", "w" do |f|
- f.puts 'puts "STATIC"'
- end
- end
- def rakefile_multidesc
- rakefile <<-MULTIDESC
- task :b
- desc "A"
- task :a
- desc "B"
- task :b
- desc "A2"
- task :a
- task :c
- desc "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- task :d
- MULTIDESC
- end
- def rakefile_namespace
- rakefile <<-NAMESPACE
- desc "copy"
- task :copy do
- puts "COPY"
- end
- namespace "nest" do
- desc "nest copy"
- task :copy do
- puts "NEST COPY"
- end
- task :xx => :copy
- end
- anon_ns = namespace do
- desc "anonymous copy task"
- task :copy do
- puts "ANON COPY"
- end
- end
- desc "Top level task to run the anonymous version of copy"
- task :anon => anon_ns[:copy]
- namespace "very" do
- namespace "nested" do
- task "run" => "rake:copy"
- end
- end
- namespace "a" do
- desc "Run task in the 'a' namespace"
- task "run" do
- puts "IN A"
- end
- end
- namespace "b" do
- desc "Run task in the 'b' namespace"
- task "run" => "a:run" do
- puts "IN B"
- end
- end
- namespace "file1" do
- file "xyz.rb" do
- puts "XYZ1"
- end
- end
- namespace "file2" do
- file "xyz.rb" do
- puts "XYZ2"
- end
- end
- namespace "scopedep" do
- task :prepare do
- touch "scopedep.rb"
- puts "PREPARE"
- end
- file "scopedep.rb" => [:prepare] do
- puts "SCOPEDEP"
- end
- end
- NAMESPACE
- end
- def rakefile_nosearch
- FileUtils.touch 'dummy'
- end
- def rakefile_rakelib
- FileUtils.mkdir_p 'rakelib'
- Dir.chdir 'rakelib' do
- open 'test1.rb', 'w' do |io|
- io << <<-TEST1
- task :default do
- puts "TEST1"
- end
- TEST1
- end
- open 'test2.rake', 'w' do |io|
- io << <<-TEST1
- task :default do
- puts "TEST2"
- end
- TEST1
- end
- end
- end
- def rakefile_rbext
- open 'rakefile.rb', 'w' do |io|
- io << 'task :default do puts "OK" end'
- end
- end
- def rakefile_unittest
- rakefile '# Empty Rakefile for Unit Test'
- readme = File.join 'subdir', 'README'
- FileUtils.mkdir_p File.dirname readme
- FileUtils.touch readme
- end
- def rakefile_verbose
- rakefile <<-VERBOSE
- task :standalone_verbose_true do
- verbose true
- sh "#{RUBY} -e '0'"
- end
- task :standalone_verbose_false do
- verbose false
- sh "#{RUBY} -e '0'"
- end
- task :inline_verbose_default do
- sh "#{RUBY} -e '0'"
- end
- task :inline_verbose_false do
- sh "#{RUBY} -e '0'", :verbose => false
- end
- task :inline_verbose_true do
- sh "#{RUBY} -e '0'", :verbose => true
- end
- task :block_verbose_true do
- verbose(true) do
- sh "#{RUBY} -e '0'"
- end
- end
- task :block_verbose_false do
- verbose(false) do
- sh "#{RUBY} -e '0'"
- end
- end
- VERBOSE
- end
- end
|