2004-11-23 20:04:44 -05:00
|
|
|
require 'rake'
|
|
|
|
require 'rake/testtask'
|
|
|
|
require 'rake/rdoctask'
|
|
|
|
|
|
|
|
$VERBOSE = nil
|
2005-02-17 15:12:27 -05:00
|
|
|
TEST_CHANGES_SINCE = Time.now - 600
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
desc "Run all the tests on a fresh test database"
|
2005-02-07 09:08:38 -05:00
|
|
|
task :default => [ :test_units, :test_functional ]
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2005-02-17 15:12:27 -05:00
|
|
|
|
|
|
|
desc 'Require application environment.'
|
|
|
|
task :environment do
|
|
|
|
unless defined? RAILS_ROOT
|
|
|
|
require File.dirname(__FILE__) + '/config/environment'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
desc "Generate API documentatio, show coding stats"
|
|
|
|
task :doc => [ :appdoc, :stats ]
|
|
|
|
|
|
|
|
|
2005-02-17 15:12:27 -05:00
|
|
|
# Look up tests for recently modified sources.
|
|
|
|
def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago)
|
|
|
|
FileList[source_pattern].map do |path|
|
|
|
|
if File.mtime(path) > touched_since
|
|
|
|
test = "#{test_path}/#{File.basename(path, '.rb')}_test.rb"
|
|
|
|
test if File.exists?(test)
|
|
|
|
end
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Test recent changes.'
|
|
|
|
Rake::TestTask.new('recent') do |t|
|
|
|
|
since = TEST_CHANGES_SINCE
|
|
|
|
touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
|
|
|
|
recent_tests('app/models/*.rb', 'test/unit', since) +
|
|
|
|
recent_tests('app/controllers/*.rb', 'test/functional', since)
|
|
|
|
|
|
|
|
t.libs << 'test'
|
|
|
|
t.verbose = true
|
|
|
|
t.test_files = touched.uniq
|
|
|
|
end
|
|
|
|
task :test_recent => [ :clone_structure_to_test ]
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
desc "Run the unit tests in test/unit"
|
|
|
|
Rake::TestTask.new("test_units") { |t|
|
|
|
|
t.libs << "test"
|
2005-02-07 08:14:05 -05:00
|
|
|
t.pattern = 'test/unit/**/*_test.rb'
|
2004-11-23 20:04:44 -05:00
|
|
|
t.verbose = true
|
|
|
|
}
|
2005-02-07 09:08:38 -05:00
|
|
|
task :test_units => [ :clone_structure_to_test ]
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
desc "Run the functional tests in test/functional"
|
|
|
|
Rake::TestTask.new("test_functional") { |t|
|
|
|
|
t.libs << "test"
|
2005-02-07 08:14:05 -05:00
|
|
|
t.pattern = 'test/functional/**/*_test.rb'
|
2004-11-23 20:04:44 -05:00
|
|
|
t.verbose = true
|
|
|
|
}
|
2005-02-07 09:08:38 -05:00
|
|
|
task :test_functional => [ :clone_structure_to_test ]
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
desc "Generate documentation for the application"
|
|
|
|
Rake::RDocTask.new("appdoc") { |rdoc|
|
|
|
|
rdoc.rdoc_dir = 'doc/app'
|
|
|
|
rdoc.title = "Rails Application Documentation"
|
|
|
|
rdoc.options << '--line-numbers --inline-source'
|
|
|
|
rdoc.rdoc_files.include('doc/README_FOR_APP')
|
|
|
|
rdoc.rdoc_files.include('app/**/*.rb')
|
|
|
|
}
|
|
|
|
|
|
|
|
desc "Generate documentation for the Rails framework"
|
|
|
|
Rake::RDocTask.new("apidoc") { |rdoc|
|
|
|
|
rdoc.rdoc_dir = 'doc/api'
|
|
|
|
rdoc.title = "Rails Framework Documentation"
|
|
|
|
rdoc.options << '--line-numbers --inline-source'
|
|
|
|
rdoc.rdoc_files.include('README')
|
2004-12-16 19:42:53 -05:00
|
|
|
rdoc.rdoc_files.include('CHANGELOG')
|
2004-12-12 07:57:08 -05:00
|
|
|
rdoc.rdoc_files.include('vendor/railties/lib/breakpoint.rb')
|
2004-11-23 20:04:44 -05:00
|
|
|
rdoc.rdoc_files.include('vendor/railties/CHANGELOG')
|
|
|
|
rdoc.rdoc_files.include('vendor/railties/MIT-LICENSE')
|
|
|
|
rdoc.rdoc_files.include('vendor/activerecord/README')
|
|
|
|
rdoc.rdoc_files.include('vendor/activerecord/CHANGELOG')
|
|
|
|
rdoc.rdoc_files.include('vendor/activerecord/lib/active_record/**/*.rb')
|
|
|
|
rdoc.rdoc_files.exclude('vendor/activerecord/lib/active_record/vendor/*')
|
|
|
|
rdoc.rdoc_files.include('vendor/actionpack/README')
|
|
|
|
rdoc.rdoc_files.include('vendor/actionpack/CHANGELOG')
|
|
|
|
rdoc.rdoc_files.include('vendor/actionpack/lib/action_controller/**/*.rb')
|
|
|
|
rdoc.rdoc_files.include('vendor/actionpack/lib/action_view/**/*.rb')
|
|
|
|
rdoc.rdoc_files.include('vendor/actionmailer/README')
|
|
|
|
rdoc.rdoc_files.include('vendor/actionmailer/CHANGELOG')
|
|
|
|
rdoc.rdoc_files.include('vendor/actionmailer/lib/action_mailer/base.rb')
|
|
|
|
}
|
|
|
|
|
|
|
|
desc "Report code statistics (KLOCs, etc) from the application"
|
|
|
|
task :stats do
|
2005-02-17 15:12:27 -05:00
|
|
|
require 'code_statistics'
|
2004-11-23 20:04:44 -05:00
|
|
|
CodeStatistics.new(
|
|
|
|
["Helpers", "app/helpers"],
|
2004-12-22 08:08:26 -05:00
|
|
|
["Controllers", "app/controllers"],
|
|
|
|
["Functionals", "test/functional"],
|
2004-11-23 20:04:44 -05:00
|
|
|
["Models", "app/models"],
|
2004-12-22 08:08:26 -05:00
|
|
|
["Units", "test/unit"]
|
2004-11-23 20:04:44 -05:00
|
|
|
).to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Recreate the test databases from the development structure"
|
2005-01-10 19:07:35 -05:00
|
|
|
task :clone_structure_to_test => [ :db_structure_dump, :purge_test_database ] do
|
2005-01-24 08:12:40 -05:00
|
|
|
abcs = ActiveRecord::Base.configurations
|
|
|
|
case abcs["test"]["adapter"]
|
|
|
|
when "mysql"
|
|
|
|
ActiveRecord::Base.establish_connection(:test)
|
|
|
|
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
|
|
|
|
IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
|
|
|
|
ActiveRecord::Base.connection.execute(table)
|
|
|
|
end
|
|
|
|
when "postgresql"
|
|
|
|
`psql -U #{abcs["test"]["username"]} -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}`
|
|
|
|
when "sqlite", "sqlite3"
|
|
|
|
`#{abcs[RAILS_ENV]["adapter"]} #{abcs["test"]["dbfile"]} < db/#{RAILS_ENV}_structure.sql`
|
|
|
|
else
|
|
|
|
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Dump the database structure to a SQL file"
|
2005-02-17 15:12:27 -05:00
|
|
|
task :db_structure_dump => :environment do
|
2005-01-24 08:12:40 -05:00
|
|
|
abcs = ActiveRecord::Base.configurations
|
|
|
|
case abcs[RAILS_ENV]["adapter"]
|
|
|
|
when "mysql"
|
|
|
|
ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
|
|
|
|
File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
|
|
|
|
when "postgresql"
|
|
|
|
`pg_dump -U #{abcs[RAILS_ENV]["username"]} -s -f db/#{RAILS_ENV}_structure.sql #{abcs[RAILS_ENV]["database"]}`
|
|
|
|
when "sqlite", "sqlite3"
|
|
|
|
`#{abcs[RAILS_ENV]["adapter"]} #{abcs[RAILS_ENV]["dbfile"]} .schema > db/#{RAILS_ENV}_structure.sql`
|
|
|
|
else
|
|
|
|
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-01-24 08:12:40 -05:00
|
|
|
desc "Empty the test database"
|
2005-02-17 15:12:27 -05:00
|
|
|
task :purge_test_database => :environment do
|
2005-01-24 08:12:40 -05:00
|
|
|
abcs = ActiveRecord::Base.configurations
|
|
|
|
case abcs["test"]["adapter"]
|
|
|
|
when "mysql"
|
|
|
|
ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
|
|
|
|
ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"])
|
|
|
|
when "postgresql"
|
|
|
|
`dropdb -U #{abcs["test"]["username"]} #{abcs["test"]["database"]}`
|
|
|
|
`createdb -U #{abcs["test"]["username"]} #{abcs["test"]["database"]}`
|
|
|
|
when "sqlite","sqlite3"
|
|
|
|
File.delete(abcs["test"]["dbfile"]) if File.exist?(abcs["test"]["dbfile"])
|
|
|
|
else
|
|
|
|
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2004-12-22 08:08:26 -05:00
|
|
|
end
|