runeleaf
6/20/2009 - 4:17 PM

gistfile1.rb

# -*- ruby -*-
 require 'autotest/redgreen'
module Autotest::Growl
  def self.growl title, msg, img="~/.rails_ok.png", pri=0, sticky=""
    msg += " at #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}"
    system "growlnotify -n autotest --image #{img} -p #{pri} -m #{msg.inspect} #{title} #{sticky}"
  end

  Autotest.add_hook :ran_command do |at|
    results = [at.results].flatten.join
    ex = results[/(\d+)\s+example?/].to_i
    fa = results[/(\d+)\s+failure?/].to_i
    er = results[/(\d+)\s+error?/].to_i
    num = fa + er
    if ex >= 0
      if num.to_i > 0
        output = "#{fa} failures, #{er} errors"
        growl "RSpec Failed", "#{output}", "~/.rails_fail.png", 2, "-s"
      else
        growl "RSpec Passed", "#{ex} examples passed", "~/.rails_ok.png", -2
      end
    else
      growl "RSpec Errored", "errors", "~/.rails_fail.png", 2, "-s"
    end
  end

  Autotest.add_hook :ran_features do |at|
    results = [at.results].flatten.join
    sc = results[/(\d+)\s+scenario?/].to_i
    sk = results[/(\d+)\s+skipped?/].to_i
    pe = results[/(\d+)\s+pending?/].to_i
    un = results[/(\d+)\s+undefined?/].to_i
    fa = results[/(\d+)\s+failed?/].to_i
    pa = results[/(\d+)\s+passwd?/].to_i
    st = results[/(\d+)\s+step?/].to_i
    num = sk + pe + fa + un
    if sc >= 0
      if num.to_i > 0
        output = "#{sk} skipped, #{un} undefined, #{pe} pending, #{fa} failed"
        growl "Cucumber Failed", "#{output}", "~/.rails_fail.png", 2, "-s"
      else
        growl "Cucumber Passed", "#{sc} scenarios, #{st} steps passed", "~/.rails_ok.png", -2
      end
    else
      growl "Cucumber Errored", "errors", "~/.rails_fail.png", 2, "-s"
    end
  end
end