Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Watchr: A Flexible, Generic Alternative to AutoTest

By Ric Roberts / September 24, 2009

watcherWatchr is a continuous-testing tool by Martin Aumont in the vein of Autotest (part of the ZenTest package).

At its heart, Watchr basically watches any (or all!) of your project's files, then executes arbitrary Ruby code of your choice when things change.  Watchr configuration takes such a form:

watch('pattern') { |match_data_object| command_to_run }

For example, to produce Autotest-like functionality, you'd just specify Watchr to run the tests whenever a test or some library code changes, like this (taken from Martin's blog post):

watch('test/test_.*.rb') { |md| system "ruby #{md[0]}"}
watch('lib/(.*).rb')     { |md| system "ruby test/test_#{md[1]}.rb"}

...but Watchr can be used for much more than just testing. You could use it to automatically generate documentation, build your gem or any other tasks that you can script in Ruby.

To get started with Watchr, just install the gem (from Gemcutter), and run the watchr command from your project root, passing the location of the configuration script:

$ gem install watchr --source=http://gemcutter.org
$ cd to/your/project/root
$ watchr path/to/script

The source and some documentation (including example scripts) is available on Github, and you can read more on Martin's blog too.

If an even more simplistic route appeals to you, Ruby Inside's editor, Peter Cooper, has a basic Rake task that can run a script of your choice when files change within a project.

Comments

  1. Kerry Buckley says:

    Unlike Autotest, Watchr doesn't depend on rubygems…

    Cool.

    …To get started with Watchr, just install the gem

    Wait, what?

  2. Peter Cooper says:

    Good catch, Kerry - I've fixed it up.

  3. mynyml says:

    oops :)

    Yeah I agree the Watchr docs don't make it too clear either. It doesn't depend on rubygems as in if you install Watchr with rip (rip install git://github.com/mynyml/watchr.git), rubygems will never be loaded. Just trying to be a good gemizen. Though of course, a gem is still provided for convenience.

  4. Adrian Veidt says:

    But who watches the Watchr?

  5. Ric Roberts says:

    Regarding the rubygems thing - I was trying to express what Martin explains above. i.e. you don't need rubygems if you don't want it.

Other Posts to Enjoy

Twitter Mentions