add support for growl_notify

This commit is contained in:
John Bintz 2011-06-28 16:15:14 -04:00
parent 59b1ea2c96
commit e53036ad05
2 changed files with 128 additions and 36 deletions

View File

@ -45,9 +45,19 @@ module Guard
def self.notify_mac(title, message, image, options) def self.notify_mac(title, message, image, options)
require_growl # need for guard-rspec formatter that is called out of guard scope require_growl # need for guard-rspec formatter that is called out of guard scope
default_options = { :title => title, :icon => image_path(image), :name => "Guard" } default_options = { :title => title, :icon => image_path(image), :name => "Guard" }
default_options.merge!(options)
if defined?(GrowlNotify)
default_options[:description] = message
default_options[:application_name] = default_options.delete(:name)
GrowlNotify.send_notification(default_options) if enabled?
else
Growl.notify message, default_options.merge(options) if enabled? Growl.notify message, default_options.merge(options) if enabled?
end end
end
def self.notify_linux(title, message, image, options) def self.notify_linux(title, message, image, options)
require_libnotify # need for guard-rspec formatter that is called out of guard scope require_libnotify # need for guard-rspec formatter that is called out of guard scope
@ -90,10 +100,19 @@ module Guard
end end
def self.require_growl def self.require_growl
begin
require 'growl_notify'
GrowlNotify.config do |c|
c.notifications = c.default_notifications = [ "Guard" ]
c.application_name = c.notifications.first
end
rescue LoadError
require 'growl' require 'growl'
end
rescue LoadError rescue LoadError
turn_off turn_off
UI.info "Please install growl gem for Mac OS X notification support and add it to your Gemfile" UI.info "Please install growl or growl_notify gem for Mac OS X notification support and add it to your Gemfile"
end end
def self.require_libnotify def self.require_libnotify

View File

@ -20,8 +20,27 @@ describe Guard::Notifier do
RbConfig::CONFIG.should_receive(:[]).with('target_os').and_return 'darwin' RbConfig::CONFIG.should_receive(:[]).with('target_os').and_return 'darwin'
end end
context "with the GrowlNotify library available" do
before do
module ::GrowlNotify
def self.config ; end
end
end
it "loads the library and enables the notifications" do
subject.should_receive(:require).with('growl_notify').and_return true
subject.turn_on
subject.should be_enabled
end
after do
Object.send(:remove_const, :GrowlNotify)
end
end
context "with the Growl library available" do context "with the Growl library available" do
it "loads the library and enables the notifications" do it "loads the library and enables the notifications" do
subject.should_receive(:require).with('growl_notify').and_raise LoadError
subject.should_receive(:require).with('growl').and_return true subject.should_receive(:require).with('growl').and_return true
subject.turn_on subject.turn_on
subject.should be_enabled subject.should be_enabled
@ -30,6 +49,7 @@ describe Guard::Notifier do
context "without the Growl library available" do context "without the Growl library available" do
it "disables the notifications" do it "disables the notifications" do
subject.should_receive(:require).with('growl_notify').and_raise LoadError
subject.should_receive(:require).with('growl').and_raise LoadError subject.should_receive(:require).with('growl').and_raise LoadError
subject.turn_on subject.turn_on
subject.should_not be_enabled subject.should_not be_enabled
@ -89,6 +109,10 @@ describe Guard::Notifier do
before do before do
RbConfig::CONFIG.should_receive(:[]).with('target_os').and_return 'darwin' RbConfig::CONFIG.should_receive(:[]).with('target_os').and_return 'darwin'
subject.stub(:require_growl) subject.stub(:require_growl)
end
context 'with growl gem' do
before do
Object.send(:remove_const, :Growl) if defined?(Growl) Object.send(:remove_const, :Growl) if defined?(Growl)
Growl = Object.new Growl = Object.new
end end
@ -132,6 +156,55 @@ describe Guard::Notifier do
end end
end end
context 'with growl_notify gem' do
before do
Object.send(:remove_const, :GrowlNotify) if defined?(GrowlNotify)
GrowlNotify = Object.new
end
after do
Object.send(:remove_const, :GrowlNotify)
end
it "passes the notification to Growl" do
GrowlNotify.should_receive(:send_notification).with(
:title => "Guard",
:icon => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s,
:application_name => "Guard",
:description => 'great'
)
subject.notify 'great', :title => 'Guard'
end
it "don't passes the notification to Growl if library is not available" do
GrowlNotify.should_not_receive(:send_notification)
subject.should_receive(:enabled?).and_return(true, false)
subject.notify 'great', :title => 'Guard'
end
it "allows additional notification options" do
GrowlNotify.should_receive(:send_notification).with(
:title => "Guard",
:icon => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s,
:application_name => "Guard",
:description => 'great',
:priority => 1
)
subject.notify 'great', :title => 'Guard', :priority => 1
end
it "allows to overwrite a default notification option" do
GrowlNotify.should_receive(:send_notification).with(
:title => "Guard",
:icon => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s,
:application_name => "Guard-Cucumber",
:description => 'great'
)
subject.notify 'great', :title => 'Guard', :name => "Guard-Cucumber"
end
end
end
context "on Linux" do context "on Linux" do
before do before do
RbConfig::CONFIG.should_receive(:[]).with('target_os').and_return 'linux' RbConfig::CONFIG.should_receive(:[]).with('target_os').and_return 'linux'