2010-08-23 22:09:35 -04:00
|
|
|
require 'rbconfig'
|
2010-04-26 03:36:13 -04:00
|
|
|
require 'rake/testtask'
|
2011-12-21 14:43:41 -05:00
|
|
|
require 'rails/test_unit/sub_test_task'
|
2010-06-09 17:14:47 -04:00
|
|
|
|
2005-09-03 11:13:06 -04:00
|
|
|
TEST_CHANGES_SINCE = Time.now - 600
|
|
|
|
|
|
|
|
# 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
|
2006-05-12 00:41:23 -04:00
|
|
|
tests = []
|
|
|
|
source_dir = File.dirname(path).split("/")
|
|
|
|
source_file = File.basename(path, '.rb')
|
2008-11-17 22:55:56 -05:00
|
|
|
|
2006-05-12 00:41:23 -04:00
|
|
|
# Support subdirs in app/models and app/controllers
|
|
|
|
modified_test_path = source_dir.length > 2 ? "#{test_path}/" << source_dir[1..source_dir.length].join('/') : test_path
|
|
|
|
|
|
|
|
# For modified files in app/ run the tests for it. ex. /test/functional/account_controller.rb
|
|
|
|
test = "#{modified_test_path}/#{source_file}_test.rb"
|
2007-12-17 13:54:55 -05:00
|
|
|
tests.push test if File.exist?(test)
|
2006-05-12 00:41:23 -04:00
|
|
|
|
|
|
|
# For modified files in app, run tests in subdirs too. ex. /test/functional/account/*_test.rb
|
|
|
|
test = "#{modified_test_path}/#{File.basename(path, '.rb').sub("_controller","")}"
|
2007-12-17 13:54:55 -05:00
|
|
|
FileList["#{test}/*_test.rb"].each { |f| tests.push f } if File.exist?(test)
|
2008-11-17 22:55:56 -05:00
|
|
|
|
2006-05-12 00:41:23 -04:00
|
|
|
return tests
|
|
|
|
|
2005-09-03 11:13:06 -04:00
|
|
|
end
|
2006-05-12 00:41:23 -04:00
|
|
|
end.flatten.compact
|
2005-09-03 11:13:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2010-06-14 17:21:53 -04:00
|
|
|
# Recreated here from Active Support because :uncommitted needs it before Rails is available
|
2006-03-04 13:04:55 -05:00
|
|
|
module Kernel
|
2011-10-29 06:15:04 -04:00
|
|
|
remove_method :silence_stderr # Removing old method to prevent method redefined warning
|
2006-03-04 13:04:55 -05:00
|
|
|
def silence_stderr
|
|
|
|
old_stderr = STDERR.dup
|
2010-08-23 22:09:35 -04:00
|
|
|
STDERR.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
|
2006-03-04 13:04:55 -05:00
|
|
|
STDERR.sync = true
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
STDERR.reopen(old_stderr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-07 16:25:03 -04:00
|
|
|
task :default => :test
|
|
|
|
|
2012-01-02 15:49:18 -05:00
|
|
|
desc 'Runs test:units, test:functionals, test:integration together (also available: test:benchmark, test:profile)'
|
2011-12-21 12:10:35 -05:00
|
|
|
task :test do
|
|
|
|
Rake::Task[ENV['TEST'] ? 'test:single' : 'test:run'].invoke
|
|
|
|
end
|
2005-09-03 11:13:06 -04:00
|
|
|
|
2006-02-26 23:38:39 -05:00
|
|
|
namespace :test do
|
2010-04-08 23:40:48 -04:00
|
|
|
task :prepare do
|
|
|
|
# Placeholder task for other Railtie and plugins to enhance. See Active Record for an example.
|
|
|
|
end
|
|
|
|
|
2011-12-21 12:10:35 -05:00
|
|
|
task :run => %w(test:units test:functionals test:integration)
|
|
|
|
|
2010-04-08 23:40:48 -04:00
|
|
|
Rake::TestTask.new(:recent => "test:prepare") do |t|
|
2006-02-26 23:38:39 -05:00
|
|
|
since = TEST_CHANGES_SINCE
|
|
|
|
touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
|
2006-05-12 00:41:23 -04:00
|
|
|
recent_tests('app/models/**/*.rb', 'test/unit', since) +
|
|
|
|
recent_tests('app/controllers/**/*.rb', 'test/functional', since)
|
2005-10-28 15:52:28 -04:00
|
|
|
|
2006-02-26 23:38:39 -05:00
|
|
|
t.libs << 'test'
|
|
|
|
t.test_files = touched.uniq
|
2005-10-28 15:52:28 -04:00
|
|
|
end
|
2006-09-05 11:35:50 -04:00
|
|
|
Rake::Task['test:recent'].comment = "Test recent changes"
|
2008-11-17 22:55:56 -05:00
|
|
|
|
2010-04-08 23:40:48 -04:00
|
|
|
Rake::TestTask.new(:uncommitted => "test:prepare") do |t|
|
2006-03-12 22:21:09 -05:00
|
|
|
def t.file_list
|
2008-04-19 01:42:26 -04:00
|
|
|
if File.directory?(".svn")
|
2011-07-23 04:28:23 -04:00
|
|
|
changed_since_checkin = silence_stderr { `svn status` }.split.map { |path| path.chomp[7 .. -1] }
|
2008-04-19 01:42:26 -04:00
|
|
|
elsif File.directory?(".git")
|
2011-07-23 03:50:10 -04:00
|
|
|
changed_since_checkin = silence_stderr { `git ls-files --modified --others` }.split.map { |path| path.chomp }
|
2008-04-19 01:42:26 -04:00
|
|
|
else
|
|
|
|
abort "Not a Subversion or Git checkout."
|
|
|
|
end
|
2006-03-18 01:47:46 -05:00
|
|
|
|
2008-04-19 01:42:26 -04:00
|
|
|
models = changed_since_checkin.select { |path| path =~ /app[\\\/]models[\\\/].*\.rb$/ }
|
|
|
|
controllers = changed_since_checkin.select { |path| path =~ /app[\\\/]controllers[\\\/].*\.rb$/ }
|
2006-03-18 01:47:46 -05:00
|
|
|
|
2011-11-08 23:16:06 -05:00
|
|
|
unit_tests = models.map { |model| "test/unit/#{File.basename(model, '.rb')}_test.rb" }
|
|
|
|
functional_tests = controllers.map { |controller| "test/functional/#{File.basename(controller, '.rb')}_test.rb" }
|
|
|
|
(unit_tests + functional_tests).uniq.select { |file| File.exist?(file) }
|
2006-03-12 22:21:09 -05:00
|
|
|
end
|
2008-11-17 22:55:56 -05:00
|
|
|
|
2006-03-01 19:17:53 -05:00
|
|
|
t.libs << 'test'
|
|
|
|
end
|
2008-04-19 01:42:26 -04:00
|
|
|
Rake::Task['test:uncommitted'].comment = "Test changes since last checkin (only Subversion and Git)"
|
2005-10-28 15:52:28 -04:00
|
|
|
|
2010-10-08 08:46:40 -04:00
|
|
|
Rake::TestTask.new(:single => "test:prepare") do |t|
|
|
|
|
t.libs << "test"
|
|
|
|
end
|
|
|
|
|
2011-12-21 10:15:36 -05:00
|
|
|
Rails::SubTestTask.new(:units => "test:prepare") do |t|
|
2006-02-26 23:38:39 -05:00
|
|
|
t.libs << "test"
|
|
|
|
t.pattern = 'test/unit/**/*_test.rb'
|
|
|
|
end
|
|
|
|
|
2011-12-21 10:15:36 -05:00
|
|
|
Rails::SubTestTask.new(:functionals => "test:prepare") do |t|
|
2006-02-26 23:38:39 -05:00
|
|
|
t.libs << "test"
|
|
|
|
t.pattern = 'test/functional/**/*_test.rb'
|
|
|
|
end
|
|
|
|
|
2011-12-21 10:15:36 -05:00
|
|
|
Rails::SubTestTask.new(:integration => "test:prepare") do |t|
|
2006-02-28 13:57:32 -05:00
|
|
|
t.libs << "test"
|
|
|
|
t.pattern = 'test/integration/**/*_test.rb'
|
|
|
|
end
|
|
|
|
|
2011-12-21 10:15:36 -05:00
|
|
|
Rails::SubTestTask.new(:benchmark => 'test:prepare') do |t|
|
2008-06-13 03:21:03 -04:00
|
|
|
t.libs << 'test'
|
|
|
|
t.pattern = 'test/performance/**/*_test.rb'
|
|
|
|
t.options = '-- --benchmark'
|
|
|
|
end
|
|
|
|
|
2011-12-21 10:15:36 -05:00
|
|
|
Rails::SubTestTask.new(:profile => 'test:prepare') do |t|
|
2008-06-13 03:21:03 -04:00
|
|
|
t.libs << 'test'
|
|
|
|
t.pattern = 'test/performance/**/*_test.rb'
|
|
|
|
end
|
2006-03-18 01:47:46 -05:00
|
|
|
end
|