Skip to content

Instantly share code, notes, and snippets.

@bmabey
Created May 20, 2009 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bmabey/115035 to your computer and use it in GitHub Desktop.
Save bmabey/115035 to your computer and use it in GitHub Desktop.
#
# .autotest
# Autotest Growl Notifications for Rspec and Test::Unit
#
# Created by Rein Henrichs on 2007-09-12.
# See http://reinh.com/2007/9/12/the-autotest-rosetta-stone
# Copyright 2007 Rein Henrichs.
# http://pastie.caboo.se/96573/download
#
require 'logger'
logfile = File.join(File.dirname(__FILE__), '.autotest.log')
$logger = Logger.new(logfile)
# Sound effects for autotest
module Autotest::Sound
@@sound_path = '~/Library/autotest/sound/'
@@sound_app = '/opt/local/bin/qtplay'
def self.playsound file
cmd = "#{@@sound_app} #{@@sound_path + file} > /dev/null 2>&1 &"
system cmd
end
end
Autotest.add_hook :initialize do |at|
%w[.svn .git].each { |dir| at.add_exception(dir) }
at.add_exception(/^(?:\.\/)?(?:db|doc|log|public|script|tmp|filestore|vendor)/)
end
module Autotest::Growl
#Autotest.add_hook :run_command do |at|
#Autotest::Sound.playsound "run_command.wav"
#end
Autotest.add_hook :ran_command do |at|
input = Input.new( at.results )
Growler.display_notification( input )
end
end
class String
def remove_color_codes!
self.gsub!(/\e\[\d+m/,'')
self.strip!
end
end
class Growler
class << self
def display_notification( input )
growler = RspecGrowler if input.from_rspec?
growler = TestUnitGrowler if input.from_test_unit?
growler.display_notification( input.result_line )
rescue => e
$logger.fatal e
notify_system_error( "Unexpected Error", "Please check your ~/.autotest.log and report anything strange to reinh@reinh.com" )
end
def notify(title, msg, img, pri=0, stick="" )
system "osascript ~/bin/growlNotify.scpt '#{title}' '#{msg.inspect}' '#{img}'"
#puts ""
#puts "---------------------------------------------------------------------------"
#puts "growlnotify -n autotest --image #{img} -p #{pri} -m #{msg.inspect} #{title} #{stick}"
#puts "---------------------------------------------------------------------------"
#puts ""
#system "growlnotify -n autotest --image #{img} -p #{pri} -m #{msg.inspect} #{title} #{stick}"
end
def notify_system_error( title = "System Error", err = "")
Growler.notify( title, err, '~/Library/autotest/fail.png')
end
end
end
class Input
def initialize(input)
@input = input
end
def empty?; @input.empty?; end
# TODO: Make these work
def failing?; end
def passing?; end
def error?; end
def from_rspec?; !!rspec_result_line || empty?; end
def from_test_unit?; !!test_unit_result_line; end
def rspec_result_line; @input.grep( /\d+\sexample/ ).first; end
def test_unit_result_line; @input.grep( /\d+\sassertion/ ).first; end
def result_line
@line ||= case
when from_rspec? then rspec_result_line
when from_test_unit? then test_unit_result_line
else nil
end
@line.remove_color_codes! if @line
end
end
class AutoGrowler < Growler
class << self
def notify_failure(input)
notify( "Examples Failed", input, '~/Library/autotest/fail.png', 2 )
#Autotest::Sound.playsound "red.wav"
end
def notify_success(input)
notify( "Examples Passed", input, '~/Library/autotest/pass.png', 0 )
#Autotest::Sound.playsound "green.wav"
end
end
end
class RspecGrowler < AutoGrowler
class << self
def notify_pending(input)
notify( "Examples Pending", input, '~/Library/autotest/pending.png', 1 )
#Autotest::Sound.playsound "ran_command.wav"
end
def notify_error
notify( "Syntax Error", "It looks like there was a syntax error. Check your autotest results.", '~/Library/autotest/fail.png')
#Autotest::Sound.playsound "red.wav"
end
def display_notification(input)
notify_error and return unless input
# TODO: replace with some object oriented goodness. elsif FTL!
examples, failures, pending = input.split(", ")
if failures.to_i > 0
notify_failure input
elsif pending.to_i > 0
notify_pending input
else
notify_success input
end
end
end
end
class TestUnitGrowler < AutoGrowler
class << self
def notify_error(input)
notify( "Tests Errored", input, '~/Library/autotest/fail.png', 2 )
#Autotest::Sound.playsound "red.wav"
end
def display_notification( input )
# TODO: replace with some object oriented goodness. elsif FTL!
tests, assertions, failures, errors = input.split(", ")
if errors.to_i > 0
notify_error input
elsif failures.to_i > 0
notify_failure input
else
notify_success input
end
end
end
end
-- growlNotify.applescript
-- Created by Mike Hagedorn on 2007-10-31.
-- Copyright (c) 2007 Silverchair Solutions. All rights reserved. http://www.silverchairsolutions.com
-- based on script from the growl website
-- works around the fact that growlnotify doesnt work on leopard
-- pass in like osascript growlNotify.applescript
on run argv
set rspectitle to item 1 of argv
set rspecmessage to item 2 of argv
set rspecimage to item 3 of argv
tell application "GrowlHelperApp"
-- Make a list of all the notification types
-- that this script will ever send:
set the allNotificationsList to ¬
{"RSpec Notification"}
-- Make a list of the notifications
-- that will be enabled by default.
-- Those not enabled by default can be enabled later
-- in the ‘Applications’ tab of the growl prefpane.
set the enabledNotificationsList to ¬
{"RSpec Notification"}
-- Register our script with growl.
-- You can optionally (as here) set a default icon
-- for this script’s notifications.
register as application ¬
"Growl-RSpec AppleScript Notifications" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application "Script Editor"
-- Send a Notification…
notify with name ¬
"RSpec Notification" title rspectitle ¬
description rspecmessage ¬
application name ¬
"Growl-RSpec AppleScript Notifications" image from location rspecimage
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment