~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/railties/lib/tasks/testing.rake

  • Committer: Michael Forrest
  • Date: 2010-10-15 16:28:50 UTC
  • Revision ID: michael.forrest@canonical.com-20101015162850-tj2vchanv0kr0dun
refrozeĀ gems

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
TEST_CHANGES_SINCE = Time.now - 600
 
2
 
 
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
 
7
      tests = []
 
8
      source_dir = File.dirname(path).split("/")
 
9
      source_file = File.basename(path, '.rb')
 
10
 
 
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
 
13
 
 
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)
 
17
 
 
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)
 
21
 
 
22
      return tests
 
23
 
 
24
    end
 
25
  end.flatten.compact
 
26
end
 
27
 
 
28
 
 
29
# Recreated here from ActiveSupport because :uncommitted needs it before Rails is available
 
30
module Kernel
 
31
  def silence_stderr
 
32
    old_stderr = STDERR.dup
 
33
    STDERR.reopen(RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'NUL:' : '/dev/null')
 
34
    STDERR.sync = true
 
35
    yield
 
36
  ensure
 
37
    STDERR.reopen(old_stderr)
 
38
  end
 
39
end
 
40
 
 
41
desc 'Run all unit, functional and integration tests'
 
42
task :test do
 
43
  errors = %w(test:units test:functionals test:integration).collect do |task|
 
44
    begin
 
45
      Rake::Task[task].invoke
 
46
      nil
 
47
    rescue => e
 
48
      task
 
49
    end
 
50
  end.compact
 
51
  abort "Errors running #{errors.to_sentence(:locale => :en)}!" if errors.any?
 
52
end
 
53
 
 
54
namespace :test do
 
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)
 
60
 
 
61
    t.libs << 'test'
 
62
    t.verbose = true
 
63
    t.test_files = touched.uniq
 
64
  end
 
65
  Rake::Task['test:recent'].comment = "Test recent changes"
 
66
 
 
67
  Rake::TestTask.new(:uncommitted => "db:test:prepare") do |t|
 
68
    def t.file_list
 
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 }
 
73
      else
 
74
        abort "Not a Subversion or Git checkout."
 
75
      end
 
76
 
 
77
      models      = changed_since_checkin.select { |path| path =~ /app[\\\/]models[\\\/].*\.rb$/ }
 
78
      controllers = changed_since_checkin.select { |path| path =~ /app[\\\/]controllers[\\\/].*\.rb$/ }
 
79
 
 
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" }
 
82
 
 
83
      unit_tests.uniq + functional_tests.uniq
 
84
    end
 
85
 
 
86
    t.libs << 'test'
 
87
    t.verbose = true
 
88
  end
 
89
  Rake::Task['test:uncommitted'].comment = "Test changes since last checkin (only Subversion and Git)"
 
90
 
 
91
  Rake::TestTask.new(:units => "db:test:prepare") do |t|
 
92
    t.libs << "test"
 
93
    t.pattern = 'test/unit/**/*_test.rb'
 
94
    t.verbose = true
 
95
  end
 
96
  Rake::Task['test:units'].comment = "Run the unit tests in test/unit"
 
97
 
 
98
  Rake::TestTask.new(:functionals => "db:test:prepare") do |t|
 
99
    t.libs << "test"
 
100
    t.pattern = 'test/functional/**/*_test.rb'
 
101
    t.verbose = true
 
102
  end
 
103
  Rake::Task['test:functionals'].comment = "Run the functional tests in test/functional"
 
104
 
 
105
  Rake::TestTask.new(:integration => "db:test:prepare") do |t|
 
106
    t.libs << "test"
 
107
    t.pattern = 'test/integration/**/*_test.rb'
 
108
    t.verbose = true
 
109
  end
 
110
  Rake::Task['test:integration'].comment = "Run the integration tests in test/integration"
 
111
 
 
112
  Rake::TestTask.new(:benchmark => 'db:test:prepare') do |t|
 
113
    t.libs << 'test'
 
114
    t.pattern = 'test/performance/**/*_test.rb'
 
115
    t.verbose = true
 
116
    t.options = '-- --benchmark'
 
117
  end
 
118
  Rake::Task['test:benchmark'].comment = 'Benchmark the performance tests'
 
119
 
 
120
  Rake::TestTask.new(:profile => 'db:test:prepare') do |t|
 
121
    t.libs << 'test'
 
122
    t.pattern = 'test/performance/**/*_test.rb'
 
123
    t.verbose = true
 
124
  end
 
125
  Rake::Task['test:profile'].comment = 'Profile the performance tests'
 
126
 
 
127
  Rake::TestTask.new(:plugins => :environment) do |t|
 
128
    t.libs << "test"
 
129
 
 
130
    if ENV['PLUGIN']
 
131
      t.pattern = "vendor/plugins/#{ENV['PLUGIN']}/test/**/*_test.rb"
 
132
    else
 
133
      t.pattern = 'vendor/plugins/*/**/test/**/*_test.rb'
 
134
    end
 
135
 
 
136
    t.verbose = true
 
137
  end
 
138
  Rake::Task['test:plugins'].comment = "Run the plugin tests in vendor/plugins/*/**/test (or specify with PLUGIN=name)"
 
139
end