Installing GrowlNotify and Autotest for BDD use with Rspec on Leopard

Ruby on Rails Add comments

I’m a big fan of Behavior Driven Development (BDD). It really illustrates how one little change in your code can have significant impact on the rest of you application, which you would never be able to catch without testing. Not to to mention the benefit of being able to write code and make sure it works even if you don’t have production data available.

So, upon completing a clean upgrade to Leopard I noticed that after installing Growl and growlnotify, that growlnotify would not work even through Growl itself was working.

1) First let’s set up the .autotest file. For this you’ll need the gems rspec (1.1.3), ZenTest (3.9.1), and redgreen since ZenTest change how it handled exceptions in 3.9.

sudo gem install rspec
sudo gem install ZenTest
sudo gem install redgreen

Now open up your ~/.autotest file in Textmate

mate ~/.autotest

Paste in the following code. Notice the exceptions at the bottom of the file that really helps speed up autotests as well as keeps your cpu usage low. My Macbook would get really hot really fast prior to using this execptions.

require ‘autotest/redgreen’

module Autotest::Growl
def self.growl title, msg, img, pri=0, stick=”"
system “growlnotify -n autotest –image #{img} -p #{pri} -m #{ msg.inspect} #{title} #{stick}”
end

Autotest.add_hook :ran_command do |autotest|
filtered = autotest.results.grep(/\d+\s.*examples?/)
output = filtered.empty? ? ” : filtered.last.slice(/(\d+)\s.*examples?,\s(\d+)\s.*failures?(?:,\s(\d+)\s.*pending)?/)
if output =~ /[1-9]\sfailures?/
growl “Test Results”, “#{output}”, “~/Library/autotest/rails_fail.png”
elsif output =~ /pending/
growl “Test Results”, “#{output}”, “~/Library/autotest/rails_pending.png”
else
growl “Test Results”, “#{output}”, “~/Library/autotest/rails_ok.png”
end
end

end

Autotest.add_hook :initialize do |at|
%w{.svn .hg .git vendor}.each {|exception| at.add_exception(exception)}
end

Next, you’ll want to download the Pass, Fail, and Pending images below and place them in ~/Library/autotest:

rails_ok.png rails_pending.png rails_fail.png

First, the prerequisite to growlnotify is Growl. You can download it from http://growl.info/. It’s a graphical installer, so you shouldn’t have an issues.

Once you’ve installed Growl, pop open the terminal and enter the following commands.

cd /Volumes/Growl 1.1.2/Extras/growlnotify
sudo sh install.sh

Give it a test run:

growlnotify -m “Testing growlnotify” Test

So if that doesn’t work, it’s probably because of an issue with the default permissions and Mac OS X 10.5 Leopard. Here’s what my permssions were. (I have no idea why the @ symbol is there)

cd /usr/local/bin
ls -l
-rwxr-xr-x@ 1 ryan 501 130288 Jan 22 17:38 growlnotify

To fix the permissions, I ran the following:

sudo chown root:admin growlnotify

Resulting in the following permssions:

cd /usr/local/bin
ls -l
-rwxr-xr-x@ 1 root admin 130288 Jan 22 17:38 growlnotify

Give it a test run again:

growlnotify -m “Testing growlnotify” Test

Now you can change directories to your rails app and run autotest and growlnotify should be working now..

cd ~/myrailsapp
autotest

Growlnotify Green

Growlnofity Red

References:
http://blog.codefront.net/2007/04/01/get-your-testing-results-via-growl-notifications/
http://www.danielfischer.com/2007/05/14/ruby-on-rails-bdd-with-autotest-growl-rspec/
http://railsontherun.com/2008/1/30/misc-tips-and-tricks/

20 Responses to “Installing GrowlNotify and Autotest for BDD use with Rspec on Leopard”

  1. Toby Says:

    How’s it going Micah? Couldn’t find you on fb so found your blog. Nice write up. Growl is pretty cool. I am going to play with its windows equivalent-Snarl.

  2. Toby Says:

    Instructions for snarl: http://wiki.futuretoby.com/Autotest_popup_notifications_with_Snarl

  3. RAILroading » Blog Archive » links for 2008-04-12 Says:

    [...] Midnight Oil » Blog Archive » Installing GrowlNotify and Autotest for BDD use with Rspec on Leopar… (tags: growl autotest) [...]

  4. Jon Crawford Says:

    Hey thanks for that permissions tip. I’d actually given up on growlnotify for months because I thought it simply didn’t work.

    !jon

  5. Matt Schick Says:

    I’ve got something really weird happening, I’ve followed your instruction to the T but growl notifications don’t work. So I wanted to make sure the Autotest.add_hook :ran_command was running, so I put in the classic “puts ‘here’” at the top of the hook and oddly the growl notifications show up. But if I pull out the puts statement growl notifications stop working again.

    this is too weird…any thoughts?

  6. Ryan Says:

    Matt,

    Are you doing the autotest with rSpec? I’ve had problems with it running test unit.

    If you are using with with test unit, check this one out:

    http://blog.codefront.net/2007/04/01/get-your-testing-results-via-growl-notifications/

    Autotest.add_hook :ran_command do |at|
    output = at.results.last.slice(/(\d+)\s.*specifications?,\s(\d+)\s.*failures?/)
    if output =~ /[1-9]\sfailures?/
    growl “Test Results”, “#{output}”, “/Data/Pictures/Icons/rails_fail.png”, 2, “-s”
    else
    growl “Test Results”, “#{output}”, “/Data/Pictures/Icons/rails_ok.png”
    end
    end

    If not, let me know..

    Ryan

  7. Matt Schick Says:

    Nope, I’m using rspec. Can’t seems to figure this one out. Also the growl plugin that is built into rspec that you can use via “require ‘autotest/growl’” isn’t working for me either. I double checked the permissions on growlnotify and they are just as you have them listed above. Calling growlnotify from the command line works just fine.

    So needless to say, I’m a bit stumped.

  8. Notional Slurry » links for 2008-04-25 Says:

    [...] Midnight Oil » Blog Archive » Installing GrowlNotify and Autotest for BDD use with Rspec on Leopar… Installation path works fine with PeepCode graphics and ~/.autotest script as well. (tags: BDD Leopard Growl growlnotify autotest ZenTest rspec Ruby) [...]

  9. Alexander Says:

    if there are currently no specs the following is needed:

    filtered = autotest.results.grep(/\d+\s.*examples?/)
    output = filtered.empty? ? “” : filtered.last.slice(/(\d+)\s.*examples?,\s(\d+)\s.*failures?(?:,\s(\d+)\s.*pending)?/)

  10. Ryan Says:

    Alexander,

    Thanks for the comment… I’ve update my post with your code..

  11. Keith Says:

    Awesome tip thanks! I’ll pass it along to my friends.

    One small hiccup. The double-quotes in the regex are actually smart quotes and trip up the regex parser. Instead of:

    output = filtered.empty? ? “”

    replace the quotes and get this instead

    output = filtered.empty? ? “”

    (Hopefully my straight quotes didn’t get parsed out.)

  12. Wiktor Gworek Says:

    Muchos kudos for the tip! It works like a charm.

    Maybe there is too much configuration (by too much I mean installing growlnotify) but rest is OK.

    Cheers!

  13. Peter Baker Says:

    fwiw I had to add “ENV['RSPEC'] ||= ‘true’” to my environment.rb file to make autotest work in the latest rspec head…

  14. Sanja Curgus Says:

    Thank you so much. This worked while growl glue didn’t. I’m running OSX 10.5. thanx

  15. Micah Says:

    Finally got around to setting this up. Worked the first time :)

  16. Bob Walsh Says:

    Nice! And thanks!

  17. jpemberthy Says:

    Awesome, thanks!

  18. j0ban Says:

    Thanks for the information

  19. justin Says:

    Mac OS X supports multiple permission models – there’s the standard set of owner/group/other and read/write/execute, and a richer set that allows more fine-grained control.

    ls uses the @ sign to indicate files are using the richer set and therefore what you’re seeing is only part of the picture. You can use the -e switch to display the extended permissions.

  20. Ed Moss Says:

    FYI, I don’t recommend including ‘vendor’ in your .autotest file. Why? Well if you have a vendor.rb model – vendors table in your db – it will miss your unit test. Yes, I found out the hard way. Also, in finding out the hard way, I noticed that the vendor folder is already skipped properly. Here is my exception block (with some additional ignores for other gems I use):

    %w{.svn .hg .git .sass-cache .DS_Store .braids}.each {|exception| at.add_exception(exception)}

    If you want confirmation that ‘vendor’ is already ignroed, look in your Zentest gem folder, file rails.rb:

    add_exception %r%^\./(?:db|doc|log|public|script|tmp|vendor)%

    which when used in autotest.rb here:

    Find.prune if f =~ self.exceptions

    looks like this:

    /(?-mix:^\.\/(?:db|doc|log|public|script|tmp|vendor))|\.svn|\.hg|\.git/

    Hope this makes sense.

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in