diff options
Diffstat (limited to 'railties/lib/rails/test_unit')
-rw-r--r-- | railties/lib/rails/test_unit/railtie.rb | 4 | ||||
-rw-r--r-- | railties/lib/rails/test_unit/sub_test_task.rb | 79 | ||||
-rw-r--r-- | railties/lib/rails/test_unit/testing.rake | 45 |
3 files changed, 105 insertions, 23 deletions
diff --git a/railties/lib/rails/test_unit/railtie.rb b/railties/lib/rails/test_unit/railtie.rb index f52c4c44b7..75180ff978 100644 --- a/railties/lib/rails/test_unit/railtie.rb +++ b/railties/lib/rails/test_unit/railtie.rb @@ -1,3 +1,7 @@ +if defined?(Rake.application) && Rake.application.top_level_tasks.grep(/^(default$|test(:|$))/).any? + ENV['RAILS_ENV'] ||= 'test' +end + module Rails class TestUnitRailtie < Rails::Railtie config.app_generators do |c| diff --git a/railties/lib/rails/test_unit/sub_test_task.rb b/railties/lib/rails/test_unit/sub_test_task.rb index 87b6f9b5a4..a463380e2d 100644 --- a/railties/lib/rails/test_unit/sub_test_task.rb +++ b/railties/lib/rails/test_unit/sub_test_task.rb @@ -1,6 +1,83 @@ +require 'rake/testtask' + module Rails + class TestTask < Rake::TestTask # :nodoc: all + class TestInfo + def initialize(tasks) + @tasks = tasks + end + + def files + @tasks.map { |task| + [task, translate(task)].find { |file| test_file?(file) } + }.compact + end + + def translate(file) + if file =~ /^app\/(.*)$/ + "test/#{$1.sub(/\.rb$/, '')}_test.rb" + else + "test/#{file}_test.rb" + end + end + + def tasks + @tasks - test_file_tasks - opt_names + end + + def opts + opts = opt_names + if opts.any? + "-n #{opts.join ' '}" + end + end + + private + + def test_file_tasks + @tasks.find_all { |task| + [task, translate(task)].any? { |file| test_file?(file) } + } + end + + def test_file?(file) + file =~ /^test/ && File.file?(file) && !File.directory?(file) + end + + def opt_names + (@tasks - test_file_tasks).reject { |t| task_defined? t } + end + + def task_defined?(task) + Rake::Task.task_defined? task + end + end + + def self.test_info(tasks) + TestInfo.new tasks + end + + def initialize(name = :test) + super + @libs << "test" # lib *and* test seem like a better default + end + + def define + task @name do + if ENV['TESTOPTS'] + ARGV.replace Shellwords.split ENV['TESTOPTS'] + end + libs = @libs - $LOAD_PATH + $LOAD_PATH.unshift(*libs) + file_list.each { |fl| + FileList[fl].to_a.each { |f| require File.expand_path f } + } + end + end + end + # Silence the default description to cut down on `rake -T` noise. - class SubTestTask < Rake::TestTask + class SubTestTask < Rake::TestTask # :nodoc: def desc(string) # Ignore the description. end diff --git a/railties/lib/rails/test_unit/testing.rake b/railties/lib/rails/test_unit/testing.rake index 3c247f32c0..9263b9b81d 100644 --- a/railties/lib/rails/test_unit/testing.rake +++ b/railties/lib/rails/test_unit/testing.rake @@ -48,10 +48,17 @@ task default: :test desc 'Runs test:units, test:functionals, test:integration together' task :test do - if ENV['TEST'] - exec "bundle exec rails test #{ENV['TEST'].inspect}" + info = Rails::TestTask.test_info Rake.application.top_level_tasks + 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 - exec 'bundle exec rails test' + Rake::Task[ENV['TEST'] ? 'test:single' : 'test:run'].invoke end end @@ -60,15 +67,11 @@ namespace :test do # Placeholder task for other Railtie and plugins to enhance. See Active Record for an example. end - task :run do - ActiveSupport::Deprecation.warn "`rake test:run` is deprecated. Please use `rails test`." - exec 'bundle exec rails test' - end + task :run => ['test:units', 'test:functionals', 'test:integration'] # Inspired by: http://ngauthier.com/2012/02/quick-tests-with-bash.html desc "Run tests quickly by merging all types and not resetting db" - Rake::TestTask.new(:all) do |t| - t.libs << "test" + Rails::TestTask.new(:all) do |t| t.pattern = "test/**/*_test.rb" end @@ -90,7 +93,6 @@ namespace :test do recent_tests('app/controllers/**/*.rb', 'test/controllers', since) + recent_tests('app/controllers/**/*.rb', 'test/functional', since) - t.libs << 'test' t.test_files = touched.uniq end Rake::Task['test:recent'].comment = "Deprecated; Test recent changes" @@ -114,23 +116,22 @@ namespace :test do controllers.map { |controller| "test/functional/#{File.basename(controller, '.rb')}_test.rb" } (unit_tests + functional_tests).uniq.select { |file| File.exist?(file) } end - - t.libs << 'test' end Rake::Task['test:uncommitted'].comment = "Deprecated; Test changes since last checkin (only Subversion and Git)" - desc "Deprecated; Please use `rails test \"#{ENV['TEST']}\"`" - task :single do - ActiveSupport::Deprecation.warn "`rake test:single` is deprecated. Please use `rails test \"#{ENV['TEST']}\"`." - exec "bundle exec rails test #{test_suit_name}" + Rails::TestTask.new(single: "test:prepare") + + ["models", "helpers", "controllers", "mailers", "integration"].each do |name| + Rails::TestTask.new(name => "test:prepare") do |t| + t.pattern = "test/#{name}/**/*_test.rb" + end end - [:models, :helpers, :units, :controllers, :functionals, :integration].each do |test_suit_name| - desc "Deprecated; Please use `rails test #{test_suit_name}`" - task test_suit_name do - ActiveSupport::Deprecation.warn "`rake test:#{test_suit_name}` is deprecated. Please use `rails test #{test_suit_name}`." + Rails::TestTask.new(units: "test:prepare") do |t| + t.pattern = 'test/{models,helpers,unit}/**/*_test.rb' + end - exec "bundle exec rails test #{test_suit_name}" - end + Rails::TestTask.new(functionals: "test:prepare") do |t| + t.pattern = 'test/{controllers,mailers,functional}/**/*_test.rb' end end |