lib/test_notifier.rb in fnando-test_notifier-0.0.10 vs lib/test_notifier.rb in fnando-test_notifier-0.1.0
- old
+ new
@@ -1,7 +1,5 @@
-require 'snarl' if RUBY_PLATFORM =~ /mswin/
-
module TestNotifier
# create a .test_notifier at your home
# directory and save the images as passed.png,
# failure.png and error.png for custom images
PASSED_IMAGE = "passed.png"
@@ -11,25 +9,69 @@
PASSED_TITLE = "Passed"
RSPEC_REGEX = /(\d+) examples?, (\d+) failures?(, (\d+) pendings?)?/
TEST_UNIT_REGEX = /(\d+)\stests,\s(\d+)\sassertions,\s(\d+)\sfailures,\s(\d+)\serrors/
+ GROWL_REGISTER_FILE = File.expand_path("~/.test_notifier-growl")
+
def self.notify(image, title, message)
image ||= "none.png"
custom_image = File.join(File.expand_path("~/.test_notifier"), image)
image = File.exists?(custom_image) ? custom_image : File.join(File.dirname(__FILE__), "test_notifier", "icons", image)
if RUBY_PLATFORM =~ /darwin/
- system("growlnotify -n test_notifier --image #{image} -p 2 -m \"#{message}\" -t \"#{title}\"")
+ if `ps -Al | grep GrowlHelper` && `which growlnotify` && $? == 0
+ unless File.file?(GROWL_REGISTER_FILE)
+ script = File.dirname(__FILE__) + "/test_notifier/register-growl.scpt"
+ system "osascript #{script} > #{GROWL_REGISTER_FILE}"
+ end
+
+ system("growlnotify -n test_notifier --image #{image} -p 2 -m \"#{message}\" -t \"#{title}\"")
+ else
+ puts "No compatible popup notification system installed."
+ puts "Try installing these:\n* growl"
+ end
elsif RUBY_PLATFORM =~ /mswin/
- Snarl.show_message(title, message, image)
+ begin
+ require 'snarl'
+ rescue LoadError
+ puts 'To be notified by a popup please install Snarl and a ruby gem "snarl".'
+ else
+ Snarl.show_message(title, message, image)
+ end
elsif RUBY_PLATFORM =~ /(linux|freebsd)/
- system("notify-send -i #{image} #{title} \"#{message}\"")
+ # if osd_cat is avaible
+ if `which osd_cat` && $? == 0
+ color = case image
+ when /#{PASSED_IMAGE}/
+ 'green'
+ when /#{FAILURE_IMAGE}/
+ 'orange'
+ when /#{ERROR_IMAGE}/
+ 'red'
+ else
+ 'white'
+ end
+ OsdCat.send "#{title} \n #{message}", color
+ # if dcop server is running
+ elsif `ps -Al | grep dcop` && $? == 0
+ def self.knotify title, msg
+ system "dcop knotify default notify " +
+ "eventname \'#{title}\' \'#{msg}\' '' '' 16 2"
+ end
+ knotify title, message
+ # if notify-send is avaible
+ elsif `which notify-send` && $? == 0
+ system("notify-send -i #{image} #{title} \"#{message}\"")
+ else
+ puts "No popup notification software installed."
+ puts "Try installing one of this:\n * osd_cat (apt-get install xosd-bin),\n * knotify (use KDE),\n * notify-send (apt-get install libnotify-bin)"
+ end
end
end
-
+
def self.rspec?(content)
(RSPEC_REGEX =~ content)
end
def self.notification_for_rspec(content)
@@ -74,5 +116,28 @@
message = "#{t} tests, #{a} assertions, #{f} failures, #{e} errors"
notify(image, title, message)
end
end
+
+# Provides a method for popup notifications using osd_cat
+# Extracted from http://theadmin.org/articles/2008/2/10/fail-loudly-with-osd_cat-and-autotest
+module OsdCat
+ # TODO move this module to a separate file
+
+ # Use xlsfonts to find the different fonts
+ FONT = "-bitstream-charter-bold-r-normal--33-240-100-100-p-206-iso8859-1"
+
+ # Will display the message on the top of the screen centered, adjust these numbers to suit.
+ POSITION = "top" # top|middle|bottom
+ POSITION_OFFSET = "0" # Pixels from position to display (think CSS margin)
+ ALIGN = "center" # left|right|center
+
+ def self.send msg, color='green'
+ osd_command = "echo #{msg.inspect} | osd_cat --font=#{FONT} --shadow=0 --pos=#{POSITION} -o #{POSITION_OFFSET} --delay=4 --outline=4 --align=#{ALIGN} -c #{color}"
+
+ # osd_cat blocks so start a new thread, otherwise Autotest will wait
+ Thread.new do
+ `#{osd_command}`
+ end
+ end
+end
\ No newline at end of file