From 2f94f9e22f152b5c5cf8d65a7fba4f598250c12b Mon Sep 17 00:00:00 2001 From: Thibaud Guillaume-Gentil Date: Fri, 6 May 2011 23:19:31 +0200 Subject: [PATCH] Fixed notification option Only print notification "Install message" once Added GUARD_NOTIFY=false env variable support Fixes #28 --- README.markdown | 2 + lib/guard.rb | 3 +- lib/guard/notifier.rb | 48 +++++++++++------------ spec/guard/notifier_spec.rb | 76 +++++++++++++++++++------------------ spec/guard_spec.rb | 12 ++++++ spec/spec_helper.rb | 1 + spec/support/gems_helper.rb | 13 +++++++ 7 files changed, 93 insertions(+), 62 deletions(-) create mode 100644 spec/support/gems_helper.rb diff --git a/README.markdown b/README.markdown index d38176c..1a8d72a 100644 --- a/README.markdown +++ b/README.markdown @@ -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 --group (or -g) option: $ guard --group group_name another_group_name diff --git a/lib/guard.rb b/lib/guard.rb index 2e1f869..12c35a9 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -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 diff --git a/lib/guard/notifier.rb b/lib/guard/notifier.rb index 52204e2..27e92df 100644 --- a/lib/guard/notifier.rb +++ b/lib/guard/notifier.rb @@ -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 diff --git a/spec/guard/notifier_spec.rb b/spec/guard/notifier_spec.rb index 900684d..e09622c 100644 --- a/spec/guard/notifier_spec.rb +++ b/spec/guard/notifier_spec.rb @@ -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 diff --git a/spec/guard_spec.rb b/spec/guard_spec.rb index f635a4f..bf3faa8 100644 --- a/spec/guard_spec.rb +++ b/spec/guard_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1520849..f69ed16 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/support/gems_helper.rb b/spec/support/gems_helper.rb new file mode 100644 index 0000000..2ccf7fe --- /dev/null +++ b/spec/support/gems_helper.rb @@ -0,0 +1,13 @@ +def growl_installed? + require 'growl' + true +rescue LoadError + false +end + +def libnotify_installed? + require 'libnotify' + true +rescue LoadError + false +end \ No newline at end of file