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?(,\s(\d+)\spending?|)/)
    full_sentence, green, failures, garbage, pending = $~.to_a.map(&:to_i)
    if output
      if failures > 0
        notify "FAIL", "#{output}", "#{image_root}/fail.png"
      elsif pending > 0
        notify "Pending", "#{output}", "#{image_root}/pending.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.

Update: The script now supports pending examples. Thanks to Andy.

Update: Removed an extra end. Thanks to Andy again.

  • Another possible resolution when a bunch of warning messages appear:

    in ~/autotest line 2, I commented out

    #require ‘redgreen’

    running autotest now works without having to type in bundle exec autotest

  • I would like to second the statement made in the above comment:
    After commenting out the line *require redgreen*
    you are able to run the test by just typing *autotest* in the root of your project.

    My system: Kubuntu 10.10

  • I agree. Even though I did ‘gem install redgreen’ – I get a bunch of errors which seem to be telling me that it can’t find redgreen to load it. I tried changing the require line to include the version of redgreen which was loaded (redgreen-1.2.2) but still no joy. when I comment out the require redgreen line – it works.

  • Same thing found on Ubuntu 10.10; commenting out the redgreen line makes it work.

    Thanks for posting this!

  • Thanks for the post and the comments above! I commented require ‘redgreen’ and it worked on linux mint :) Thank you!

  • commenting #require redgreen worked for me

  • Thanks..

  • Worked as is on Fedora 14 Gnome. Took out ‘require redgreen’ and worked also. Gracias.

  • This didn’t work on Ubuntu 11.04. I get the following error:

    /home/egervari/.autotest:1: warning: regex literal in condition
    /usr/local/lib/ruby/gems/1.9.1/gems/ZenTest-4.5.0/lib/autotest.rb:311:in `load’: /home/egervari/.autotest:1: unknown regexp options – rby (SyntaxError)
    from /usr/local/lib/ruby/gems/1.9.1/gems/ZenTest-4.5.0/lib/autotest.rb:311:in `block in initialize’
    from /usr/local/lib/ruby/gems/1.9.1/gems/ZenTest-4.5.0/lib/autotest.rb:310:in `each’
    from /usr/local/lib/ruby/gems/1.9.1/gems/ZenTest-4.5.0/lib/autotest.rb:310:in `initialize’
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.2/lib/autotest/rspec2.rb:11:in `initialize’
    from /usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.6.0/lib/autotest/rails_rspec2.rb:31:in `initialize’
    from /usr/local/lib/ruby/gems/1.9.1/gems/ZenTest-4.5.0/lib/autotest.rb:241:in `new’
    from /usr/local/lib/ruby/gems/1.9.1/gems/ZenTest-4.5.0/lib/autotest.rb:241:in `run’
    from /usr/local/lib/ruby/gems/1.9.1/gems/ZenTest-4.5.0/bin/autotest:6:in `’
    from /usr/local/bin/autotest:19:in `load’
    from /usr/local/bin/autotest:19:in `’

  • I started getting the error:

    uninitialized constant Test::Unit::UI::Console::Diff (NameError)

    when I upgraded to ruby 1.9.2-p180 (Ubuntu 10.4). The solution was to make sure autotest knows to use the test-unit gem. I just added the following to my .autotest file:

    #!/bin/ruby
    gem ‘test-unit’

    And now autotest is working again :)

  • I am a total noob in rails and am having trouble with getting the notifications to work, i installed the gems and placed the file in my home directory along with the images but how to i get the notifications to appear?

  • I can only get autotest to run if I type /var/lib/gems/1.8/bin/autotest. I then runs but does nothing except tell me how long it has been waiting every time I save. Does not tell me anything else.

  • How do you set this up when you are using RVM.

    loading autotest/rails_rspec2
    /home/mak/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:54:in `require’: no such file to load — test/unit/ui/console/testrunner (LoadError)

  • Got this to work on ubuntu 11.04, rvm 1.6.24, by commenting out both require lines #2, and #3, but after the notification goes up the terminal does not return to the directory.

  • Thanks a lot, this worked for me as well. Not sure if it’s needed, but right before I also installed the autotest-notification gem. Then, like Mak recommended(thanks!), I commented both lines #2 and #3 out. I’m on Ubuntu 11.04(with most recent updates), Ruby 1.9.2, using RVM, and Rails 3.1.

  • This worked for me on Ubuntu 11.10, Ruby 1.8.7, using RVM, Rails 3.0.7. I never installed the redgreen gem, since it seemed to cause problems for most commenters.

    Also, I was getting a ‘No such file’ gems/rspec-core-2.7.0/bin/rspec error. I had to run bundle install -binstubs in my app directory and copy the bin directory that it created into gems/rspec-core-2.7.0 … I believe that issue will be fixed with the next release of the rspec-core gem.

    Thanks!

  • Thanks folks. This worked on openSUSE 11.3, after commenting out ‘redgreen’

  • Thanks! Worked for me. Ubuntu 11.10.

  • I love u People for the post and the comments
    Thanks.

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

Trackbacks / Pingbacks