1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Making the rake file for tests easier to read.

I'm defining a new class which modularizes how the `rake test` tasks are
defined and invoked.
This commit is contained in:
wangjohn 2013-06-26 22:06:13 -07:00
parent 6d7477e87d
commit 512a64f37f
2 changed files with 45 additions and 15 deletions

View file

@ -2,13 +2,53 @@ require 'rake/testtask'
module Rails module Rails
class TestTask < Rake::TestTask # :nodoc: all class TestTask < Rake::TestTask # :nodoc: all
# A utility class which is used primarily in "rails/test_unit/testing.rake"
# to help define rake tasks corresponding to <tt>rake test</tt>.
#
# This class takes a TestInfo class and defines the appropriate rake task
# based on the information, then invokes it.
class TestCreator
def initialize(info)
@info = info
end
def invoke_rake_task
if @info.files.any?
create_and_run_single_test
reset_application_tasks
else
Rake::Task[ENV['TEST'] ? 'test:single' : 'test:run'].invoke
end
end
private
def create_and_run_single_test
Rails::TestTask.new('test:single') { |t|
t.test_files = @info.files
}
ENV['TESTOPTS'] ||= @info.opts
Rake::Task['test:single'].invoke
end
def reset_application_tasks
Rake.application.top_level_tasks.replace @info.tasks
end
end
# This is a utility class used by the <tt>TestTask::TestCreator</tt> class.
# This class takes a set of test tasks and checks to see if they correspond
# to test files (or can be transformed into test files). Calling <tt>files</tt>
# provides the set of test files and is used when initializing tests after
# a call to <tt>rake test</tt>.
class TestInfo class TestInfo
def initialize(tasks) def initialize(tasks)
@tasks = tasks @tasks = tasks
@files = nil
end end
def files def files
@tasks.map { |task| @files ||= @tasks.map { |task|
[task, translate(task)].find { |file| test_file?(file) } [task, translate(task)].find { |file| test_file?(file) }
}.compact }.compact
end end
@ -53,8 +93,9 @@ module Rails
end end
end end
def self.test_info(tasks) def self.test_creator(tasks)
TestInfo.new tasks info = TestInfo.new(tasks)
TestCreator.new(info)
end end
def initialize(name = :test) def initialize(name = :test)

View file

@ -7,18 +7,7 @@ task default: :test
desc 'Runs test:units, test:functionals, test:integration together' desc 'Runs test:units, test:functionals, test:integration together'
task :test do task :test do
info = Rails::TestTask.test_info Rake.application.top_level_tasks Rails::TestTask.test_creator(Rake.application.top_level_tasks).invoke_rake_task
if info.files.any?
Rails::TestTask.new('test:single') { |t|
t.test_files = info.files
}
ENV['TESTOPTS'] ||= info.opts
Rake.application.top_level_tasks.replace info.tasks
Rake::Task['test:single'].invoke
else
Rake::Task[ENV['TEST'] ? 'test:single' : 'test:run'].invoke
end
end end
namespace :test do namespace :test do