Automate Everything: Ruby, Linux and other hints, tips and tricks.

Gnome and Autospec Notifications

I’m very keen on DBB with RSpec now and I wanted to share how I set up autospec notifications on Ubuntu (Jaunty 9.04) as long as the post I followed to do it is no longer available.

I’m using XCFE but should work perfectly on Gnome as well. This is an screenshot of what you’ll get in the end:

autospec notification XFCE/Gnome

autospec notification XFCE/Gnome

First you need the ZenTest gem, you probably already do:

$ sudo gem install ZenTest
$ sudo gem install redgreen

Then install the libnotify-bin package:

$ sudo apt-get install libnotify-bin

Here is the trick. Create a file called ~/.autotest with this:

#!/bin/ruby
require 'redgreen'
require 'autotest/timestamp'
 
module Autotest::GnomeNotify
  def self.notify title, msg, img
    system "notify-send '#{title}' '#{msg}' -i #{img} -t 3000"
  end
 
  Autotest.add_hook :ran_command do |at|
    image_root = "~/.autotest_images"
    results = [at.results].flatten.join("\n")
    results.gsub!(/\\e\[\d+m/,'')
    output = results.slice(/(\d+)\sexamples?,\s(\d+)\sfailures?/)
    puts output.inspect
    if output
      if $~[2].to_i > 0
        notify "FAIL", "#{output}", "#{image_root}/fail.png"
      else
        notify "Pass", "#{output}", "#{image_root}/pass.png"
      end
    end
  end
 
end

As you can see I use the fail.png and pass.png images to show those cheesy smileys : ). You can download them here and copy them to ~/.autotest_images/.

autotest_images.zip

Make your tests go green and have a beer! : )

Update: Added redgreen gem and changed “require redgreen” as Rodrigo Flores and rakk suggested. Thanks!

Update: Changed mistakenly escaped >. Thanks to Martin y Hunter for the comments.

  • You need to install the redgreen gem also. The file .autotest only work if instead of “autotest/redgreen”, you put “redgreen/autotest”.

    Thanks for the hints

  • Hi Rodrigo,

    Thanks for the comment. I don’t have any gem called ‘redgreen’ installed. It seems to come with Zentest:

    /usr/lib/ruby/gems/1.8/gems/ZenTest-4.0.0/lib/autotest/redgreen.rb

    I can also do this:

    $ irb
    >> require 'autotest'
    => true
    >> require 'autotest/redgreen'
    => true
    >> require 'autotest/timestamp'
    => true
    >>
    

    Maybe we have a version mismatch.

  • sudo gem install redgreen

    - require ‘autotest/redgreen’
    + require ‘redgreen’

    for ZenTest (4.1.4)

  • Right rakk! I had ZenTest 4.0.0. Updating the post now.

  • I couldn’t find the images in the zip file. here is a link to a page with red and green smiley faces. http://getsatisfaction.com/railstutorial/topics/gnome_and_autospec

  • Hi Jesse. The images should be inside a hidden directory called “.autotest_images”. Maybe you missed them because the dir is hidden.

  • I am quite new to rails, did exactly what you did, but neither autotest nor autospec opens the graphical display. Autotest reacts to changes on the file system as expected, but still, no gui. I have gnome and
    autotest-rails (4.1.0)
    redgreen (1.2.2)
    rspec (1.3.0)
    rspec-rails (1.3.2, 1.3.0)
    ZenTest (4.3.1)
    any idea? Thx a lot.

  • ah, something else: it says
    ./.autotest:17: warning: `&’ interpreted as argument prefix
    ./.autotest:17: warning: unused literal ignored
    thx again

  • Martin:
    I had the same problem, but I got it working by replacing the “>” on line 17 with a >

  • Martin:
    What I meant to say was I fixed it by replacing the “& g t” on line 17 with a >. (As my previous post demonstrated, “& g t” is the character entity for >)

  • I can confirm that Hunter’s solution of replacing & g t with > works to fix the issue.

  • ./.autotest:17: warning: `&’ interpreted as argument prefix
    ./.autotest:17: warning: unused literal ignored
    ./.autotest:6: warning: redefine notify

    are still warnings I get.

    Line 17 is if $~[2].to_i > 0

    and Line 6 def self.notify title, msg, img

    Otherwise it seems to work, I get the pictures ;-) But how am I supposed to use autospec? Just start a terminal and run it and ignore the terminal afterwards? or how do I ideally run it continuously?

  • Hi Benjamin, you can follow http://www.railstutorial.org/ to see autotest in action :-)

  • you can also see screenshots from my environment here
    http://uczareac.blogspot.com/2010/06/ruby-autotest-setup-notes-autospec.html

  • im running ubuntu 10.04 i followed all the directions above and my autospec isnt giving me popups … any idea?

  • The post’s script didn’t work for me, found a super-simple way of installing notifications – http://github.com/nuxlli/autotest-notification

  • Great! Thanks!

  • $ autotest
    loading autotest/rails
    /home/deim/.rvm/gems/ruby-1.9.2-head/gems/test-unit-2.1.1/lib/test/unit/ui/console/testrunner.rb:295:in `’: uninitialized constant Test::Unit::UI::Console::Diff (NameError)

    Whats wrong?

  • @mystdeim
    I have the same problem with you,
    you can change the require ‘redgreen’ to gem ‘redgreen’
    at your ~/.autotest

You can follow any responses to this entry through the RSS 2.0 feed.