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

Ticket #6578 (closed enhancement: worksforme)

Opened 3 years ago

Last modified 2 years ago

[PATCH] For ticket 3709 - make the :test rake task not fail silently on exceptions

Reported by: peter_marklund Assigned to: David
Priority: normal Milestone: 1.2.3
Component: Railties Version: edge
Severity: normal Keywords:
Cc:

Description

If one of the Rake tasks that the :test (default) task depends on fails, you want to be alerted of that as it helps you find the root of failure (the first error/exception is usually what you want to look at). Currently rake :test will swallow exceptions thrown for example by a task such as db:test:prepare which makes debugging very hard in certain cases.

This simple patch only affects the :test task and makes sure you get the stack trace to help debugging whilst retaining the behaviour of the current code to not abort testing but running all three of test:units, test:functionals, and test:integration even if an exception is thrown. The new :test task should be functionally equivalent to the old one with the only difference being that exceptions are now reported when they occur.

The new :test task after the patch should be:

desc 'Test all units and functionals'
task :test do
  test_tasks = ["test:units", "test:functionals"]
  test_tasks << "test:integration" if File.exist?("test/integration")

  test_tasks.each do |test_task|
    begin
      Rake::Task[test_task].invoke
    rescue => e
      got_error = true
      puts("Rake task '#{test_task} threw exception #{e} - #{e.backtrace.join("\n  ")}")
    end
  end

  raise "Test failures" if got_error
end

See ticket 3709: http://dev.rubyonrails.org/ticket/3709

The patch is:

Index: railties/lib/tasks/testing.rake
===================================================================
--- railties/lib/tasks/testing.rake	(revision 5468)
+++ railties/lib/tasks/testing.rake	(working copy)
@@ -40,11 +40,16 @@
 
 desc 'Test all units and functionals'
 task :test do
-  Rake::Task["test:units"].invoke       rescue got_error = true
-  Rake::Task["test:functionals"].invoke rescue got_error = true
-  
-  if File.exist?("test/integration")
-    Rake::Task["test:integration"].invoke rescue got_error = true
+  test_tasks = ["test:units", "test:functionals"]
+  test_tasks << "test:integration" if File.exist?("test/integration")
+
+  test_tasks.each do |test_task|
+    begin
+      Rake::Task[test_task].invoke
+    rescue => e
+      got_error = true
+      puts("Rake task '#{test_task} threw exception #{e} - #{e.backtrace.join("\n  ")}")
+    end
   end
 
   raise "Test failures" if got_error

Attachments

make_rake_test_not_fail_silently.diff (0.9 kB) - added by peter_marklund on 11/08/06 17:05:29.

Change History

11/08/06 17:05:29 changed by peter_marklund

  • attachment make_rake_test_not_fail_silently.diff added.

11/08/06 17:13:05 changed by peter_marklund

The patch was changed slightly. The uploaded file has the most recent edits.

12/03/06 17:15:05 changed by david

  • status changed from new to closed.
  • resolution set to duplicate.

Rake has this functionality built in with the -t option.

12/03/06 21:55:27 changed by peter_marklund

  • status changed from closed to reopened.
  • resolution deleted.

David, I did try using the rake -t option but it doesn't solve the problem. For example, try triggering an exception in a task such as db:test:clone_structure and then run rake -t. You will find that your exception is not reported. It is not reported because we actively silence the exception in the Rails code in the :test task. Rake cannot save us here.

I am not the only programmer who has been bitten by those silent failures, see http://marklunds.com/articles/one/317. I am curious to know if you think failing silently is acceptable or even preferrable here.

03/07/07 00:08:02 changed by dysinger

  • status changed from reopened to closed.
  • resolution set to worksforme.

looks like this is fixed on trunk