Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/railties/lib/tasks/testing.rake

Revision 5016, 4.0 kB (checked in by david, 4 years ago)

Proper description syntax for test tasks (closes #5308)

Line 
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.exists?(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.exists?(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/ ? 'NUL:' : '/dev/null')
34     STDERR.sync = true
35     yield
36   ensure
37     STDERR.reopen(old_stderr)
38   end
39 end
40
41 desc 'Test all units and functionals'
42 task :test do
43   Rake::Task["test:units"].invoke       rescue got_error = true
44   Rake::Task["test:functionals"].invoke rescue got_error = true
45  
46   if File.exist?("test/integration")
47     Rake::Task["test:integration"].invoke rescue got_error = true
48   end
49
50   raise "Test failures" if got_error
51 end
52
53 namespace :test do
54   Rake::TestTask.new(:recent => "db:test:prepare") do |t|
55     since = TEST_CHANGES_SINCE
56     touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
57       recent_tests('app/models/**/*.rb', 'test/unit', since) +
58       recent_tests('app/controllers/**/*.rb', 'test/functional', since)
59
60     t.libs << 'test'
61     t.verbose = true
62     t.test_files = touched.uniq
63   end
64   Rake::Task['test:recent'].comment = "Test recent changes"
65  
66   Rake::TestTask.new(:uncommitted => "db:test:prepare") do |t|
67     def t.file_list
68       changed_since_checkin = silence_stderr { `svn status` }.map { |path| path.chomp[7 .. -1] }
69
70       models      = changed_since_checkin.select { |path| path =~ /app\/models\/.*\.rb/ }
71       controllers = changed_since_checkin.select { |path| path =~ /app\/controllers\/.*\.rb/ } 
72
73       unit_tests       = models.map { |model| "test/unit/#{File.basename(model, '.rb')}_test.rb" }
74       functional_tests = controllers.map { |controller| "test/functional/#{File.basename(controller, '.rb')}_test.rb" }
75
76       unit_tests.uniq + functional_tests.uniq
77     end
78    
79     t.libs << 'test'
80     t.verbose = true
81   end
82   Rake::Task['test:uncommitted'].comment = "Test changes since last checkin (only Subversion)"
83
84   Rake::TestTask.new(:units => "db:test:prepare") do |t|
85     t.libs << "test"
86     t.pattern = 'test/unit/**/*_test.rb'
87     t.verbose = true
88   end
89   Rake::Task['test:units'].comment = "Run the unit tests in test/unit"
90
91   Rake::TestTask.new(:functionals => "db:test:prepare") do |t|
92     t.libs << "test"
93     t.pattern = 'test/functional/**/*_test.rb'
94     t.verbose = true
95   end
96   Rake::Task['test:functionals'].comment = "Run the functional tests in test/functional"
97
98   Rake::TestTask.new(:integration => "db:test:prepare") do |t|
99     t.libs << "test"
100     t.pattern = 'test/integration/**/*_test.rb'
101     t.verbose = true
102   end
103   Rake::Task['test:integration'].comment = "Run the integration tests in test/integration"
104
105   Rake::TestTask.new(:plugins => :environment) do |t|
106     t.libs << "test"
107
108     if ENV['PLUGIN']
109       t.pattern = "vendor/plugins/#{ENV['PLUGIN']}/test/**/*_test.rb"
110     else
111       t.pattern = 'vendor/plugins/**/test/**/*_test.rb'
112     end
113
114     t.verbose = true
115   end
116   Rake::Task['test:plugins'].comment = "Run the plugin tests in vendor/plugins/**/test (or specify with PLUGIN=name)"
117 end
Note: See TracBrowser for help on using the browser.