Growl, Autotest and Rspec

May 30, 2008

Idea behind this is a working environment that spends more time in the editor, rather than switching to the terminal …

Autotest

Autotest (which is part of ZenTest) is something you run on your Rails project. What it does is detect saved changes and automatically runs test.

Growl

Growl is a universal notifier for OSX. What it does is pop up messages in temporary windows to tell you about things.

RSpec

Is the testing framework I am using - blogged about in other articles

Installing Growl

Growl has a clever .dmg which makes installation easy. However we also need to install growlnotify. A script to do was located in /Volumes/Growl\ 1.1.2/Extras/ when the .dmg was opened. When successful running in terminal which growlnotify will return something e.g. /usr/local/bin/growlnotify

Configuring Autotest

Had to trawl a few different sources to find code to do this on my system. Basically put following code in ~/.autotest and then restarted autotest. Also down loaded images and put them in the path shown in the code.

module Autotest::Growl

  def self.growl title, msg, img, pri=0, sticky=""
    system "growlnotify -n autotest --image #{img} -p #{pri} -m #{msg.inspect} #{title} #{sticky}"
  end

  Autotest.add_hook :ran_command do |at|
    results = [at.results].flatten.join("\n")
    output = results.slice(/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+pending)?/)
    if output
      if $~[2].to_i > 0
        growl "Test Results", "#{output}", "~/.autotest_images/rails_fail.png", 2
      else
        growl "Test Results", "#{output}", "~/.autotest_images/rails_ok.png"
      end
    end
  end
end

If this doesn’t work debug the code by examining output e.g. put puts output before the if output line.