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 -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:
$ guard --group group_name another_group_name

View File

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

View File

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

View File

@ -4,53 +4,55 @@ describe Guard::Notifier do
subject { Guard::Notifier }
describe ".notify" do
before(:each) do
@saved_guard_env = ENV["GUARD_ENV"]
ENV["GUARD_ENV"] = 'dont_mute_notify'
subject.turn_on
before(:each) { subject.turn_on }
if mac?
if growl_installed?
it "uses Growl on Mac OS X" 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
else
it { should be_disabled }
end
end
if mac? && Guard::Notifier.growl_installed?
it "uses Growl on Mac OS X" do
Growl.should_receive(:notify).with("great",
:title => "Guard",
:icon => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s,
:name => "Guard"
)
if linux?
if libnotify_installed?
it "uses Libnotify on Linux" do
Libnotify.should_receive(:show).with(
:body => "great",
:summary => 'Guard',
:icon_path => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s
)
subject.notify 'great', :title => 'Guard'
end
else
it { should be_disabled }
end
end
end
describe ".turn_off" do
if mac? && growl_installed?
it "does nothing" do
Growl.should_not_receive(:notify)
subject.notify 'great', :title => 'Guard'
end
end
if linux? && Guard::Notifier.libnotify_installed?
it "uses Libnotify on Linux" do
Libnotify.should_receive(:show).with(
:body => "great",
:summary => 'Guard',
:icon_path => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s
)
if linux? && libnotify_installed?
it "does nothing" do
Libnotify.should_not_receive(:show)
subject.notify 'great', :title => 'Guard'
end
end
describe ".turn_off" do
before(:each) { subject.turn_off }
if mac? && Guard::Notifier.growl_installed?
it "does nothing" do
Growl.should_not_receive(:notify)
subject.notify 'great', :title => 'Guard'
end
end
if linux? && Guard::Notifier.libnotify_installed?
it "does nothing" do
Libnotify.should_not_receive(:show)
subject.notify 'great', :title => 'Guard'
end
end
end
after(:each) { ENV["GUARD_ENV"] = @saved_guard_env }
it { should be_disabled }
end
end

View File

@ -23,10 +23,22 @@ describe Guard do
::Guard.listener.should be_kind_of(Guard::Listener)
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
::Guard::Notifier.should_receive(:turn_off)
::Guard.setup(:notify => false)
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
describe ".get_guard_class" do

View File

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