aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/test_unit
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib/rails/test_unit')
-rw-r--r--railties/lib/rails/test_unit/railtie.rb5
-rw-r--r--railties/lib/rails/test_unit/sub_test_task.rb120
-rw-r--r--railties/lib/rails/test_unit/testing.rake160
3 files changed, 156 insertions, 129 deletions
diff --git a/railties/lib/rails/test_unit/railtie.rb b/railties/lib/rails/test_unit/railtie.rb
index ed89ce4128..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|
@@ -5,7 +9,6 @@ module Rails
fixture_replacement: nil
c.integration_tool :test_unit
- c.performance_tool :test_unit
end
rake_tasks do
diff --git a/railties/lib/rails/test_unit/sub_test_task.rb b/railties/lib/rails/test_unit/sub_test_task.rb
index 87b6f9b5a4..6fa96d2ced 100644
--- a/railties/lib/rails/test_unit/sub_test_task.rb
+++ b/railties/lib/rails/test_unit/sub_test_task.rb
@@ -1,6 +1,124 @@
+require 'rake/testtask'
+
module Rails
+ 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 # :nodoc:
+ 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 # :nodoc:
+ def initialize(tasks)
+ @tasks = tasks
+ @files = nil
+ end
+
+ def files
+ @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_creator(tasks)
+ info = TestInfo.new(tasks)
+ TestCreator.new(info)
+ 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 9ad3a4e6d6..254ea9ecf6 100644
--- a/railties/lib/rails/test_unit/testing.rake
+++ b/railties/lib/rails/test_unit/testing.rake
@@ -1,160 +1,66 @@
-require 'rbconfig'
require 'rake/testtask'
require 'rails/test_unit/sub_test_task'
-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
- tests = []
- source_dir = File.dirname(path).split("/")
- source_file = File.basename(path, '.rb')
-
- # 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/controllers/account_controller.rb
- test = "#{modified_test_path}/#{source_file}_test.rb"
- tests.push test if File.exist?(test)
-
- # For modified files in app, run tests in subdirs too. ex. /test/controllers/account/*_test.rb
- test = "#{modified_test_path}/#{File.basename(path, '.rb').sub("_controller","")}"
- FileList["#{test}/*_test.rb"].each { |f| tests.push f } if File.exist?(test)
-
- return tests
-
- end
- end.flatten.compact
-end
-
-
-# Recreated here from Active Support because :uncommitted needs it before Rails is available
-module Kernel
- remove_method :silence_stderr # Removing old method to prevent method redefined warning
- def silence_stderr
- old_stderr = STDERR.dup
- STDERR.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
- STDERR.sync = true
- yield
- ensure
- STDERR.reopen(old_stderr)
- end
-end
-
task default: :test
-desc 'Runs test:units, test:functionals, test:integration together (also available: test:benchmark, test:profile)'
+desc "Runs all tests in test folder"
task :test do
- Rake::Task[ENV['TEST'] ? 'test:single' : 'test:run'].invoke
+ Rails::TestTask.test_creator(Rake.application.top_level_tasks).invoke_rake_task
end
namespace :test do
task :prepare do
- # Placeholder task for other Railtie and plugins to enhance. See Active Record for an example.
+ # Placeholder task for other Railtie and plugins to enhance.
+ # If used with Active Record, this task runs before the database schema is synchronized.
end
- task :run do
- errors = %w(test:units test:functionals test:integration).collect do |task|
- begin
- Rake::Task[task].invoke
- nil
- rescue => e
- { task: task, exception: e }
- end
- end.compact
-
- if errors.any?
- puts errors.map { |e| "Errors running #{e[:task]}! #{e[:exception].inspect}" }.join("\n")
- abort
- end
+ Rails::TestTask.new(:run) do |t|
+ t.pattern = "test/**/*_test.rb"
end
- Rake::TestTask.new(recent: "test:prepare") do |t|
- since = TEST_CHANGES_SINCE
- touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
- recent_tests('app/models/**/*.rb', 'test/models', since) +
- recent_tests('app/models/**/*.rb', 'test/unit', since) +
- recent_tests('app/controllers/**/*.rb', 'test/controllers', since) +
- recent_tests('app/controllers/**/*.rb', 'test/functional', since)
+ desc "Run tests quickly, but also reset db"
+ task :db => %w[db:test:prepare test]
- t.libs << 'test'
- t.test_files = touched.uniq
+ desc "Run tests quickly by merging all types and not resetting db"
+ Rails::TestTask.new(:all) do |t|
+ t.pattern = "test/**/*_test.rb"
end
- Rake::Task['test:recent'].comment = "Test recent changes"
-
- Rake::TestTask.new(uncommitted: "test:prepare") do |t|
- def t.file_list
- if File.directory?(".svn")
- changed_since_checkin = silence_stderr { `svn status` }.split.map { |path| path.chomp[7 .. -1] }
- elsif system "git rev-parse --git-dir 2>&1 >/dev/null"
- changed_since_checkin = silence_stderr { `git ls-files --modified --others --exclude-standard` }.split.map { |path| path.chomp }
- else
- abort "Not a Subversion or Git checkout."
- end
- models = changed_since_checkin.select { |path| path =~ /app[\\\/]models[\\\/].*\.rb$/ }
- controllers = changed_since_checkin.select { |path| path =~ /app[\\\/]controllers[\\\/].*\.rb$/ }
-
- unit_tests = models.map { |model| "test/models/#{File.basename(model, '.rb')}_test.rb" } +
- models.map { |model| "test/unit/#{File.basename(model, '.rb')}_test.rb" } +
- functional_tests = controllers.map { |controller| "test/controllers/#{File.basename(controller, '.rb')}_test.rb" } +
- 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 = "Test changes since last checkin (only Subversion and Git)"
-
- Rake::TestTask.new(single: "test:prepare") do |t|
- t.libs << "test"
+ Rake::Task["test:all"].enhance do
+ Rake::Task["test:deprecate_all"].invoke
end
- Rails::SubTestTask.new(models: "test:prepare") do |t|
- t.libs << "test"
- t.pattern = 'test/models/**/*_test.rb'
+ task :deprecate_all do
+ ActiveSupport::Deprecation.warn "rake test:all is deprecated and will be removed in Rails 5. " \
+ "Use rake test to run all tests in test directory."
end
- Rails::SubTestTask.new(helpers: "test:prepare") do |t|
- t.libs << "test"
- t.pattern = 'test/helpers/**/*_test.rb'
- end
+ namespace :all do
+ desc "Run tests quickly, but also reset db"
+ task :db => %w[db:test:prepare test:all]
- Rails::SubTestTask.new(units: "test:prepare") do |t|
- t.libs << "test"
- t.pattern = 'test/{models,helpers,unit}/**/*_test.rb'
- end
-
- Rails::SubTestTask.new(controllers: "test:prepare") do |t|
- t.libs << "test"
- t.pattern = 'test/controllers/**/*_test.rb'
+ Rake::Task["test:all:db"].enhance do
+ Rake::Task["test:deprecate_all"].invoke
+ end
end
- Rails::SubTestTask.new(mailers: "test:prepare") do |t|
- t.libs << "test"
- t.pattern = 'test/mailers/**/*_test.rb'
- end
+ Rails::TestTask.new(single: "test:prepare")
- Rails::SubTestTask.new(functionals: "test:prepare") do |t|
- t.libs << "test"
- t.pattern = 'test/{controllers,mailers,functional}/**/*_test.rb'
+ ["models", "helpers", "controllers", "mailers", "integration", "jobs"].each do |name|
+ Rails::TestTask.new(name => "test:prepare") do |t|
+ t.pattern = "test/#{name}/**/*_test.rb"
+ end
end
- Rails::SubTestTask.new(integration: "test:prepare") do |t|
- t.libs << "test"
- t.pattern = 'test/integration/**/*_test.rb'
+ Rails::TestTask.new(generators: "test:prepare") do |t|
+ t.pattern = "test/lib/generators/**/*_test.rb"
end
- Rails::SubTestTask.new(benchmark: 'test:prepare') do |t|
- t.libs << 'test'
- t.pattern = 'test/performance/**/*_test.rb'
- t.options = '-- --benchmark'
+ Rails::TestTask.new(units: "test:prepare") do |t|
+ t.pattern = 'test/{models,helpers,unit}/**/*_test.rb'
end
- Rails::SubTestTask.new(profile: 'test:prepare') do |t|
- t.libs << 'test'
- t.pattern = 'test/performance/**/*_test.rb'
+ Rails::TestTask.new(functionals: "test:prepare") do |t|
+ t.pattern = 'test/{controllers,mailers,functional}/**/*_test.rb'
end
end