Home > ruby > Getting notifications of autotest results on Linux (Gnome)

Getting notifications of autotest results on Linux (Gnome)

If you watched the PeepCode episode about RSpec (the first of the trilogy), you may want something like growl on linux to get informed about the results of autotest. Well, there is such thing, but it is quite complicated.

You can use libnotify on Gnome, though. Those little alerts Ubuntu shows you (3 new updates, It’s time to restart your computer, Connected to Wireless Network etc), all uses libnotify. Let’s move on!

1) Install the libnotify-bin package via apt-get

2) Go to a terminal and type, remembering to change ZenTest-3.6.0 to your current version.

sudo gedit /usr/lib/ruby/gems/1.8/gems/ZenTest-3.6.0/lib/autotest/gnomenotify.rb

3) Paste the following on the new file

# -*- ruby -*-
# Adapted from PeepCode RSpec screencast no 1,
# who got it from someone else!
# Adapted with tip from http://ph7spot.com/articles/getting_started_with_autotest
# by DitoCujo (http://ditoinfo.wordpress.com)
module Autotest::GnomeNotify
  EXPIRATION_IN_SECONDS = 5
  ERROR_ICON = "gtk-dialog-error"
  SUCCESS_ICON = "gtk-dialog-info"
  def self.notify(title, msg, icon)
    options = "-t #{EXPIRATION_IN_SECONDS * 1000} -i #{icon}"
    system "notify-send #{options} '#{title}' '#{msg}'"
  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+not implemented)?/)
    if output
      if $~[2].to_i > 0
        notify "FAIL", "#{output}", ERROR_ICON
      else
        notify "Success!", "#{output}", SUCCESS_ICON
      end
    end
  end
end

4) Now save the file and open your .autotest file (/home/_____/.autotest – Create one if it does not exist) and require the file you just created by placing this line on the top:

require ‘autotest/gnomenotify’

Done! You should now get beautiful notifications on your screen like this one here

Gnome Notify for Autotest

Categories: ruby Tags:
  1. glenn
    October 9th, 2007 at 06:43 | #1

    hey there – do you mean /var/lib/gem…/Zen… instead of /usr/

  2. October 10th, 2007 at 18:49 | #2

    Well, this path I’ve placed is valid for Ubuntu, downloading ruby-gems from source on their website. Which distro are you using, there, glenn?

  3. October 23rd, 2007 at 19:18 | #3

    I have a rather stream-of-consciousness howto via the native ruby wrappers in ruby-libnotify with the necessary hook code to do some more sophisticated stuff like customize the icon and attach the notification to a system tray icon here: http://dcberner.blogspot.com/2007/10/ruby-libnotify.html

  4. August 25th, 2008 at 08:33 | #4

    The code supplied contains syntax errors in line 15! The closing bracket is missing and the quotation marks will not be understood by the ruby interpreter…

  1. April 8th, 2008 at 02:33 | #1