Skip to content

Commit

Permalink
Merge pull request #99 from johnbintz/also-use-growl_notify
Browse files Browse the repository at this point in the history
Option to also use growl_notify gem
  • Loading branch information
thibaudgg committed Aug 13, 2011
2 parents f45f598 + 7f87411 commit 3c32661
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -12,7 +12,7 @@ require 'rbconfig'

if RbConfig::CONFIG['target_os'] =~ /darwin/i
gem 'rb-fsevent', '>= 0.4.0', :require => false
gem 'growl', '~> 1.0.3', :require => false
gem 'growl_notify', :require => false
end
if RbConfig::CONFIG['target_os'] =~ /linux/i
gem 'rb-inotify', '>= 0.8.5', :require => false
Expand Down
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -50,17 +50,17 @@ Install the rb-fsevent gem for [FSEvent](http://en.wikipedia.org/wiki/FSEvents)
$ gem install rb-fsevent
```

Install the Growl gem if you want notification support:
Install the growl_notify gem if you want notification support:

``` bash
$ gem install growl
$ gem install growl_notify
```

And add them to your Gemfile:
And add it to your Gemfile:

``` ruby
gem 'rb-fsevent'
gem 'growl'
gem 'growl_notify'
```

### On Linux
Expand Down
19 changes: 15 additions & 4 deletions lib/guard/notifier.rb
Expand Up @@ -4,6 +4,7 @@

module Guard
module Notifier
APPLICATION_NAME = "Guard"

def self.turn_off
ENV["GUARD_NOTIFY"] = 'false'
Expand Down Expand Up @@ -45,8 +46,11 @@ def self.enabled?

def self.notify_mac(title, message, image, options)
require_growl # need for guard-rspec formatter that is called out of guard scope
default_options = { :title => title, :icon => image_path(image), :name => "Guard" }
Growl.notify message, default_options.merge(options) if enabled?

options = { :description => message, :title => title, :icon => image_path(image), :application_name => APPLICATION_NAME }.merge(options)
options.delete(:name)

GrowlNotify.send_notification(options) if enabled?
end

def self.notify_linux(title, message, image, options)
Expand Down Expand Up @@ -90,10 +94,17 @@ def self.image_level(image)
end

def self.require_growl
require 'growl'
require 'growl_notify'

if GrowlNotify.application_name != APPLICATION_NAME
GrowlNotify.config do |c|
c.notifications = c.default_notifications = [ APPLICATION_NAME ]
c.application_name = c.notifications.first
end
end
rescue LoadError
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_notify gem for Mac OS X notification support and add it to your Gemfile"
end

def self.require_libnotify
Expand Down
95 changes: 57 additions & 38 deletions spec/guard/notifier_spec.rb
Expand Up @@ -20,17 +20,28 @@
RbConfig::CONFIG.should_receive(:[]).with('target_os').and_return 'darwin'
end

context "with the Growl library available" do
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').and_return true
subject.should_receive(:require).with('growl_notify').and_return true
GrowlNotify.should_receive(:application_name).and_return ''
subject.turn_on
subject.should be_enabled
end

after do
Object.send(:remove_const, :GrowlNotify)
end
end

context "without the Growl library available" do
context "without the GrowlNofity library available" do
it "disables the notifications" do
subject.should_receive(:require).with('growl').and_raise LoadError
subject.should_receive(:require).with('growl_notify').and_raise LoadError
subject.turn_on
subject.should_not be_enabled
end
Expand Down Expand Up @@ -89,46 +100,54 @@
before do
RbConfig::CONFIG.should_receive(:[]).with('target_os').and_return 'darwin'
subject.stub(:require_growl)
Object.send(:remove_const, :Growl) if defined?(Growl)
Growl = Object.new
end

after do
Object.send(:remove_const, :Growl)
end
context 'with growl_notify gem' do
before do
Object.send(:remove_const, :GrowlNotify) if defined?(GrowlNotify)
GrowlNotify = Object.new
end

it "passes the notification to Growl" do
Growl.should_receive(:notify).with("great",
:title => "Guard",
:icon => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s,
:name => "Guard"
)
subject.notify 'great', :title => 'Guard'
end
after do
Object.send(:remove_const, :GrowlNotify)
end

it "don't passes the notification to Growl if library is not available" do
Growl.should_not_receive(:notify)
subject.should_receive(:enabled?).and_return(true, false)
subject.notify 'great', :title => 'Guard'
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 "allows additional notification options" do
Growl.should_receive(:notify).with("great",
:title => "Guard",
:icon => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s,
:name => "Guard",
:priority => 1
)
subject.notify 'great', :title => 'Guard', :priority => 1
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 to overwrite a default notification option" do
Growl.should_receive(:notify).with("great",
:title => "Guard",
:icon => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s,
:name => "Guard-Cucumber"
)
subject.notify 'great', :title => 'Guard', :name => "Guard-Cucumber"
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 "throws out the application name since Guard should only use one Growl App Name while running" 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', :name => "Guard-Cucumber"
end
end
end

Expand Down

0 comments on commit 3c32661

Please sign in to comment.