Autotest RSpec Notifications for Ubuntu

Get a popup every time autotest runs your tests with the current result.


Install:

sudo gem install ZenTest
sudo apt-get install libnotify-bin

.autotest

require 'autotest/redgreen'

module Autotest::Notify
  def self.notify title, msg, img, pri='low', time=3000
    `notify-send -i #{img} -u #{pri} -t #{time} '#{msg}'`
  end

  Autotest.add_hook :ran_command do |autotest|
    results = [autotest.results].flatten.join("\n")
    output = results.slice(/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+pending)?/)
    folder = "~/Pictures/rails/"
    if output  =~ /[1-9]\d*\sfailures?/
      notify "FAIL:", "#{output}", folder+"rails_fail.png", 'critical', 10000
    elsif output  =~ /[1-9]\d*\spending?/
      notify "PENDING:", "#{output}", folder+"rails_pending.png", 'normal', 10000
    else
      notify "PASS:", "#{output}", folder+"rails_ok.png"
    end
  end
end
"

The icons go into your ~/Pictures/rails folder

Alternatively, one could use build-in pictures “gtk-dialog-error” / “gtk-dialog-info”…

Advertisements

6 thoughts on “Autotest RSpec Notifications for Ubuntu

  1. Thanks for this: most helpful.

    I found that I was getting the red icon because the regexp was recognizing “0 failures” as indicating that there WERE failures, so I rewrote the regexp as follows to make it work:

    
    if output  =~ /([123456789]|[\d]{2,})\sfailures?/ 
          notify "FAIL:", "#{output}", folder+"rails_fail.png", 'critical', 10000
    
    

    Hope it helps!

  2. Great article… very consise. I very recently moved from macOSX to Ubuntu, and I’ve been missing my Autotest + Growl setup. From what I’ve seen so far (I’ve only run a few tests with the new setup) it all works great!

    Thanks for the code!

  3. Just want to say thanks — this works perfectly and is just what I have been looking for. Getting hard to do rails when you aren’t one of the cool kids with a mac! btw, to anyone else, the require statement is not needed anymore with the latest zentest gem, since redgreen is in zentest now. The images also aren’t linking any more, but grabbing the rails logo from your public/images and colorizing it with gimp takes 5 seconds.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s