aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/test_unit/testing.rake
blob: f1f20293f06afe32020f446f5e9117044d16b8e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require 'rbconfig'
require 'rake/testtask'
require 'rails/test_unit/sub_test_task'
require 'active_support/deprecation'

# 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'
task :test do
  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
    Rake::Task[ENV['TEST'] ? 'test:single' : 'test:run'].invoke
  end
end

namespace :test do
  task :prepare do
    # Placeholder task for other Railtie and plugins to enhance. See Active Record for an example.
  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"
  Rails::TestTask.new(:all) do |t|
    t.pattern = "test/**/*_test.rb"
  end

  namespace :all do
    desc "Run tests quickly, but also reset db"
    task :db => %w[db:test:prepare test:all]
  end

  # Display deprecation message
  task :deprecated do
    ActiveSupport::Deprecation.warn "`rake #{ARGV.first}` is deprecated with no replacement."
  end

  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

  Rails::TestTask.new(units: "test:prepare") do |t|
    t.pattern = 'test/{models,helpers,unit}/**/*_test.rb'
  end

  Rails::TestTask.new(functionals: "test:prepare") do |t|
    t.pattern = 'test/{controllers,mailers,functional}/**/*_test.rb'
  end
end