Fixed notification option

Only print notification "Install message" once
Added GUARD_NOTIFY=false env variable support
Fixes #28
This commit is contained in:
Thibaud Guillaume-Gentil 2011-05-06 23:19:31 +02:00
parent fa44ef31bc
commit 2f94f9e22f
7 changed files with 93 additions and 62 deletions

View File

@ -91,6 +91,8 @@ Notifications (growl/libnotify) can be disabled with:
$ guard --notify false $ guard --notify false
$ guard -n false # shortcut $ guard -n false # shortcut
Notifications can also be disabled by setting a `GUARD_NOTIFY` environment variable to `false`
The guards to start can be specified by group (see the Guardfile DSL below) specifying the <tt>--group</tt> (or <tt>-g</tt>) option: The guards to start can be specified by group (see the Guardfile DSL below) specifying the <tt>--group</tt> (or <tt>-g</tt>) option:
$ guard --group group_name another_group_name $ guard --group group_name another_group_name

View File

@ -16,7 +16,8 @@ module Guard
@listener = Listener.select_and_init @listener = Listener.select_and_init
@guards = [] @guards = []
Notifier.turn_off unless options[:notify] options[:notify] = false if ENV["GUARD_NOTIFY"] == 'false'
options[:notify] ? Notifier.turn_on : Notifier.turn_off
self self
end end

View File

@ -9,24 +9,30 @@ module Guard
end end
def self.turn_on def self.turn_on
@disable = nil @disable = false
case Config::CONFIG['target_os']
when /darwin/i
require_growl
when /linux/i
require_libnotify
end
end end
def self.notify(message, options = {}) def self.notify(message, options = {})
unless @disable || ENV["GUARD_ENV"] == "test" unless @disable
image = options[:image] || :success image = options[:image] || :success
title = options[:title] || "Guard" title = options[:title] || "Guard"
case Config::CONFIG['target_os'] case Config::CONFIG['target_os']
when /darwin/i when /darwin/i
if growl_installed?
Growl.notify message, :title => title, :icon => image_path(image), :name => "Guard" Growl.notify message, :title => title, :icon => image_path(image), :name => "Guard"
end
when /linux/i when /linux/i
if libnotify_installed?
Libnotify.show :body => message, :summary => title, :icon_path => image_path(image) Libnotify.show :body => message, :summary => title, :icon_path => image_path(image)
end end
end end
end end
def self.disabled?
@disable
end end
private private
@ -46,24 +52,18 @@ module Guard
end end
end end
def self.growl_installed? def self.require_growl
@installed ||= begin
require 'growl' require 'growl'
true
rescue LoadError rescue LoadError
@disable = true
UI.info "Please install growl gem for Mac OS X notification support and add it to your Gemfile" UI.info "Please install growl gem for Mac OS X notification support and add it to your Gemfile"
false
end
end end
def self.libnotify_installed? def self.require_libnotify
@installed ||= begin
require 'libnotify' require 'libnotify'
true
rescue LoadError rescue LoadError
@disable = true
UI.info "Please install libnotify gem for Linux notification support and add it to your Gemfile" UI.info "Please install libnotify gem for Linux notification support and add it to your Gemfile"
false
end
end end
end end

View File

@ -4,13 +4,10 @@ describe Guard::Notifier do
subject { Guard::Notifier } subject { Guard::Notifier }
describe ".notify" do describe ".notify" do
before(:each) do before(:each) { subject.turn_on }
@saved_guard_env = ENV["GUARD_ENV"]
ENV["GUARD_ENV"] = 'dont_mute_notify'
subject.turn_on
end
if mac? && Guard::Notifier.growl_installed? if mac?
if growl_installed?
it "uses Growl on Mac OS X" do it "uses Growl on Mac OS X" do
Growl.should_receive(:notify).with("great", Growl.should_receive(:notify).with("great",
:title => "Guard", :title => "Guard",
@ -19,9 +16,13 @@ describe Guard::Notifier do
) )
subject.notify 'great', :title => 'Guard' subject.notify 'great', :title => 'Guard'
end end
else
it { should be_disabled }
end
end end
if linux? && Guard::Notifier.libnotify_installed? if linux?
if libnotify_installed?
it "uses Libnotify on Linux" do it "uses Libnotify on Linux" do
Libnotify.should_receive(:show).with( Libnotify.should_receive(:show).with(
:body => "great", :body => "great",
@ -30,27 +31,28 @@ describe Guard::Notifier do
) )
subject.notify 'great', :title => 'Guard' subject.notify 'great', :title => 'Guard'
end end
else
it { should be_disabled }
end
end
end end
describe ".turn_off" do describe ".turn_off" do
before(:each) { subject.turn_off } if mac? && growl_installed?
if mac? && Guard::Notifier.growl_installed?
it "does nothing" do it "does nothing" do
Growl.should_not_receive(:notify) Growl.should_not_receive(:notify)
subject.notify 'great', :title => 'Guard' subject.notify 'great', :title => 'Guard'
end end
end end
if linux? && Guard::Notifier.libnotify_installed? if linux? && libnotify_installed?
it "does nothing" do it "does nothing" do
Libnotify.should_not_receive(:show) Libnotify.should_not_receive(:show)
subject.notify 'great', :title => 'Guard' subject.notify 'great', :title => 'Guard'
end end
end end
end
after(:each) { ENV["GUARD_ENV"] = @saved_guard_env } it { should be_disabled }
end end
end end

View File

@ -23,10 +23,22 @@ describe Guard do
::Guard.listener.should be_kind_of(Guard::Listener) ::Guard.listener.should be_kind_of(Guard::Listener)
end end
it "should turn on by default" do
::Guard::Notifier.should_receive(:turn_on)
::Guard.setup(:notify => true)
end
it "should turn off notifier if notify option is false" do it "should turn off notifier if notify option is false" do
::Guard::Notifier.should_receive(:turn_off) ::Guard::Notifier.should_receive(:turn_off)
::Guard.setup(:notify => false) ::Guard.setup(:notify => false)
end end
it "should turn off notifier if env[GUARD_NOTIFY] is false" do
::Guard::Notifier.should_receive(:turn_off)
ENV["GUARD_NOTIFY"] = 'false'
::Guard.setup(:notify => true)
ENV["GUARD_NOTIFY"] = nil
end
end end
describe ".get_guard_class" do describe ".get_guard_class" do

View File

@ -15,6 +15,7 @@ RSpec.configure do |config|
config.run_all_when_everything_filtered = true config.run_all_when_everything_filtered = true
config.before(:each) do config.before(:each) do
Guard::Notifier.turn_off
@fixture_path = Pathname.new(File.expand_path('../fixtures/', __FILE__)) @fixture_path = Pathname.new(File.expand_path('../fixtures/', __FILE__))
end end

View File

@ -0,0 +1,13 @@
def growl_installed?
require 'growl'
true
rescue LoadError
false
end
def libnotify_installed?
require 'libnotify'
true
rescue LoadError
false
end