1
TEST_CHANGES_SINCE = Time.now - 600
3
# Look up tests for recently modified sources.
4
def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago)
5
FileList[source_pattern].map do |path|
6
if File.mtime(path) > touched_since
8
source_dir = File.dirname(path).split("/")
9
source_file = File.basename(path, '.rb')
11
# Support subdirs in app/models and app/controllers
12
modified_test_path = source_dir.length > 2 ? "#{test_path}/" << source_dir[1..source_dir.length].join('/') : test_path
14
# For modified files in app/ run the tests for it. ex. /test/functional/account_controller.rb
15
test = "#{modified_test_path}/#{source_file}_test.rb"
16
tests.push test if File.exist?(test)
18
# For modified files in app, run tests in subdirs too. ex. /test/functional/account/*_test.rb
19
test = "#{modified_test_path}/#{File.basename(path, '.rb').sub("_controller","")}"
20
FileList["#{test}/*_test.rb"].each { |f| tests.push f } if File.exist?(test)
29
# Recreated here from ActiveSupport because :uncommitted needs it before Rails is available
32
old_stderr = STDERR.dup
33
STDERR.reopen(RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'NUL:' : '/dev/null')
37
STDERR.reopen(old_stderr)
41
desc 'Run all unit, functional and integration tests'
43
errors = %w(test:units test:functionals test:integration).collect do |task|
45
Rake::Task[task].invoke
51
abort "Errors running #{errors.to_sentence(:locale => :en)}!" if errors.any?
55
Rake::TestTask.new(:recent => "db:test:prepare") do |t|
56
since = TEST_CHANGES_SINCE
57
touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
58
recent_tests('app/models/**/*.rb', 'test/unit', since) +
59
recent_tests('app/controllers/**/*.rb', 'test/functional', since)
63
t.test_files = touched.uniq
65
Rake::Task['test:recent'].comment = "Test recent changes"
67
Rake::TestTask.new(:uncommitted => "db:test:prepare") do |t|
69
if File.directory?(".svn")
70
changed_since_checkin = silence_stderr { `svn status` }.map { |path| path.chomp[7 .. -1] }
71
elsif File.directory?(".git")
72
changed_since_checkin = silence_stderr { `git ls-files --modified --others` }.map { |path| path.chomp }
74
abort "Not a Subversion or Git checkout."
77
models = changed_since_checkin.select { |path| path =~ /app[\\\/]models[\\\/].*\.rb$/ }
78
controllers = changed_since_checkin.select { |path| path =~ /app[\\\/]controllers[\\\/].*\.rb$/ }
80
unit_tests = models.map { |model| "test/unit/#{File.basename(model, '.rb')}_test.rb" }
81
functional_tests = controllers.map { |controller| "test/functional/#{File.basename(controller, '.rb')}_test.rb" }
83
unit_tests.uniq + functional_tests.uniq
89
Rake::Task['test:uncommitted'].comment = "Test changes since last checkin (only Subversion and Git)"
91
Rake::TestTask.new(:units => "db:test:prepare") do |t|
93
t.pattern = 'test/unit/**/*_test.rb'
96
Rake::Task['test:units'].comment = "Run the unit tests in test/unit"
98
Rake::TestTask.new(:functionals => "db:test:prepare") do |t|
100
t.pattern = 'test/functional/**/*_test.rb'
103
Rake::Task['test:functionals'].comment = "Run the functional tests in test/functional"
105
Rake::TestTask.new(:integration => "db:test:prepare") do |t|
107
t.pattern = 'test/integration/**/*_test.rb'
110
Rake::Task['test:integration'].comment = "Run the integration tests in test/integration"
112
Rake::TestTask.new(:benchmark => 'db:test:prepare') do |t|
114
t.pattern = 'test/performance/**/*_test.rb'
116
t.options = '-- --benchmark'
118
Rake::Task['test:benchmark'].comment = 'Benchmark the performance tests'
120
Rake::TestTask.new(:profile => 'db:test:prepare') do |t|
122
t.pattern = 'test/performance/**/*_test.rb'
125
Rake::Task['test:profile'].comment = 'Profile the performance tests'
127
Rake::TestTask.new(:plugins => :environment) do |t|
131
t.pattern = "vendor/plugins/#{ENV['PLUGIN']}/test/**/*_test.rb"
133
t.pattern = 'vendor/plugins/*/**/test/**/*_test.rb'
138
Rake::Task['test:plugins'].comment = "Run the plugin tests in vendor/plugins/*/**/test (or specify with PLUGIN=name)"