WezM.net Menu

Automatically Build and Test With Guard

Published on Wed, 26 January 2011

I’m currently working on a node.js module (node-genx) with a C++ component. Each time a change was made to the code its necessary to rebuild the module then run the tests. This was getting tiring so I decided to use guard to automate the process.

guard is a ruby gem that provides a DSL for performing actions when a set of watched files change. There a bunch of “guards” available that provide out of the box support for things like rspec, jammit, sass and CoffeeScript. For simple custom cases like mine there is also guard-shell, which will run a shell command when one of its watched files changes.

guard uses a Guardfile akin to a Makefile in you project directory to configure the guards. Mine looks like this:

guard 'shell' do
  watch(%r".*\.(?:cc|h)") { |m| `node-waf build && jasbin` }
  watch(%r"spec/.*_spec\..*") {|m| `jasbin --verbose` }
end

This runs node-waf build && jasbin to rebuild the module and run the tests when any of the C++ source files or headers change. It just runs the the tests when any of the spec files change.

Once your Guardfile is setup simply run guard in you project directory and it will start watching for changes.

Comment icon Something to Add?

Contact me by email or Twitter if you have a comment, correction or just want to get in touch.