Since DHH released commands some months ago I was thinking about how to properly automated this so one does not have to manually run test "unit" and similar every time you change a file.

The result is AutomatedCommands. A combination of a simple rake task and named pipes to communicate with your rails instance. So how does it work, exactly?

First commands: commands enables you to run your TestUnit tests from within your rails console, if you are running the console in test environment. This removes the need to launch rails, speeding up your tests and removing the need for tools like spork or zeus. Sadly it doesn’t work with rspec and the likes because rspec defaults to using executing ruby in a separate process.

Next up: AutomatedCommands. When you run

$ bundle exec rake automated_commands:watch

from inside your rails application directory I’m doing two things:

  1. create a named pipe called test_pipe
  2. Listen.to changes inside your test directory

If you now start up a rails console in test mode and execute

irb(main):001:0> start_test_listener

I’m blocking the console thread, waiting for messages to arrive over the named pipe. Once you make changes to any of your test files your tests are going to be executed - instantly.

If you want to stop just press STRG + C and drop back into your rails console.

AutomatedCommands is far from perfect as it won’t work on Windows and there are properly some issues with the current implementation, but those might be removed in future versions. For now, it just works.

Note aside: make sure to disable cache_classes inside your test.rb environment. Otherwise, only changes made to your tests are picked up, and you need to restart your rails console for changes in your controllers to have an effect:

config.cache_classes = false

Feedback is welcome & Happy hacking!


comments powered by Disqus