Added a command line option (-n false) to disable notifications (growl/libnotify). closed #28

This commit is contained in:
Thibaud Guillaume-Gentil 2011-04-10 22:32:29 +02:00
parent 31c43f7c13
commit 42c27242e1
7 changed files with 87 additions and 54 deletions

View File

@ -17,6 +17,9 @@ module Guard
@options = options @options = options
@listener = Listener.select_and_init @listener = Listener.select_and_init
@guards = [] @guards = []
Notifier.turn_off unless options[:notify]
self self
end end

View File

@ -4,22 +4,23 @@ require 'guard/version'
module Guard module Guard
class CLI < Thor class CLI < Thor
default_task :start default_task :start
method_option :clear, :type => :boolean, :default => false, :aliases => '-c', :banner => "Auto clear shell before each change/run_all/reload" method_option :clear, :type => :boolean, :default => false, :aliases => '-c', :banner => "Auto clear shell before each change/run_all/reload"
method_option :debug, :type => :boolean, :default => false, :aliases => '-d', :banner => "Print debug messages" method_option :notify, :type => :boolean, :default => true, :aliases => '-n', :banner => "Notifications feature (growl/libnotify)"
method_option :group, :type => :array, :default => [], :aliases => '-g', :banner => "Run only the passed groups" method_option :debug, :type => :boolean, :default => false, :aliases => '-d', :banner => "Print debug messages"
method_option :group, :type => :array, :default => [], :aliases => '-g', :banner => "Run only the passed groups"
desc "start", "Starts Guard" desc "start", "Starts Guard"
def start def start
::Guard.start(options) ::Guard.start(options)
end end
desc "version", "Prints Guard's version information" desc "version", "Prints Guard's version information"
def version def version
::Guard::UI.info "Guard version #{Guard::VERSION}" ::Guard::UI.info "Guard version #{Guard::VERSION}"
end end
map %w(-v --version) => :version map %w(-v --version) => :version
desc "init [GUARD]", "Generates a Guardfile into the current working directory, or insert the given GUARD" desc "init [GUARD]", "Generates a Guardfile into the current working directory, or insert the given GUARD"
def init(guard_name = nil) def init(guard_name = nil)
if !File.exist?("Guardfile") if !File.exist?("Guardfile")
@ -29,12 +30,12 @@ module Guard
::Guard::UI.error "Guardfile already exists at #{Dir.pwd}/Guardfile" ::Guard::UI.error "Guardfile already exists at #{Dir.pwd}/Guardfile"
exit 1 exit 1
end end
if guard_name if guard_name
guard_class = ::Guard.get_guard_class(guard_name) guard_class = ::Guard.get_guard_class(guard_name)
guard_class.init(guard_name) guard_class.init(guard_name)
end end
end end
end end
end end

View File

@ -3,9 +3,13 @@ require 'pathname'
module Guard module Guard
module Notifier module Notifier
def self.turn_off
@disable = true
end
def self.notify(message, options = {}) def self.notify(message, options = {})
unless ENV["GUARD_ENV"] == "test" unless @disable || ENV["GUARD_ENV"] == "test"
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']
@ -20,9 +24,9 @@ module Guard
end end
end end
end end
private private
def self.image_path(image) def self.image_path(image)
images_path = Pathname.new(File.dirname(__FILE__)).join('../../images') images_path = Pathname.new(File.dirname(__FILE__)).join('../../images')
case image case image
@ -37,7 +41,7 @@ module Guard
image image
end end
end end
def self.growl_installed? def self.growl_installed?
@installed ||= begin @installed ||= begin
require 'growl' require 'growl'
@ -47,7 +51,7 @@ module Guard
false false
end end
end end
def self.libnotify_installed? def self.libnotify_installed?
@installed ||= begin @installed ||= begin
require 'libnotify' require 'libnotify'
@ -57,6 +61,6 @@ module Guard
false false
end end
end end
end end
end end

View File

@ -1,46 +1,46 @@
module Guard module Guard
module UI module UI
class << self class << self
def info(message, options = {}) def info(message, options = {})
unless ENV["GUARD_ENV"] == "test" unless ENV["GUARD_ENV"] == "test"
reset_line if options[:reset] reset_line if options[:reset]
puts reset_color(message) if message != '' puts reset_color(message) if message != ''
end end
end end
def error(message, options = {}) def error(message, options = {})
unless ENV["GUARD_ENV"] == "test" unless ENV["GUARD_ENV"] == "test"
reset_line if options[:reset] reset_line if options[:reset]
puts "ERROR: #{message}" puts "ERROR: #{message}"
end end
end end
def debug(message, options = {}) def debug(message, options = {})
unless ENV["GUARD_ENV"] == "test" unless ENV["GUARD_ENV"] == "test"
reset_line if options[:reset] reset_line if options[:reset]
puts "DEBUG: #{message}" if ::Guard.options && ::Guard.options[:debug] puts "DEBUG: #{message}" if ::Guard.options && ::Guard.options[:debug]
end end
end end
def reset_line def reset_line
print "\r\e " print "\r\e "
end end
def clear def clear
system("clear;") system("clear;")
end end
private private
def reset_color(text) def reset_color(text)
color(text, "\e[0m") color(text, "\e[0m")
end end
def color(text, color_code) def color(text, color_code)
"#{color_code}#{text}\e[0m" "#{color_code}#{text}\e[0m"
end end
end end
end end
end end

View File

@ -3,39 +3,39 @@ require 'guard/dsl'
describe Guard::Dsl do describe Guard::Dsl do
subject { Guard::Dsl } subject { Guard::Dsl }
before(:each) do before(:each) do
::Guard.stub!(:add_guard) ::Guard.stub!(:add_guard)
end end
it "should write an error message when no Guardfile is found" do it "should write an error message when no Guardfile is found" do
Dir.stub!(:pwd).and_return("no_guardfile_here") Dir.stub!(:pwd).and_return("no_guardfile_here")
Guard::UI.should_receive(:error).with("No Guardfile in current folder, please create one.") Guard::UI.should_receive(:error).with("No Guardfile in current folder, please create one.")
lambda { subject.evaluate_guardfile }.should raise_error lambda { subject.evaluate_guardfile }.should raise_error
end end
it "should write an error message when Guardfile is not valid" do it "should write an error message when Guardfile is not valid" do
mock_guardfile_content("This Guardfile is invalid!") mock_guardfile_content("This Guardfile is invalid!")
Guard::UI.should_receive(:error).with(/Invalid Guardfile, original error is:\n/) Guard::UI.should_receive(:error).with(/Invalid Guardfile, original error is:\n/)
lambda { subject.evaluate_guardfile }.should raise_error lambda { subject.evaluate_guardfile }.should raise_error
end end
describe ".guardfile_include?" do describe ".guardfile_include?" do
it "should detect a guard specified as a string" do it "should detect a guard specified as a string" do
mock_guardfile_content("guard 'test'") mock_guardfile_content("guard 'test'")
subject.guardfile_include?('test').should be_true subject.guardfile_include?('test').should be_true
end end
it "should detect a guard specified as a symbol" do it "should detect a guard specified as a symbol" do
mock_guardfile_content("guard :test") mock_guardfile_content("guard :test")
subject.guardfile_include?('test').should be_true subject.guardfile_include?('test').should be_true
end end
end end
describe "#group" do describe "#group" do
before do before do
mock_guardfile_content(" mock_guardfile_content("
@ -44,50 +44,50 @@ describe Guard::Dsl do
watch('c') watch('c')
end end
end end
group 'y' do group 'y' do
guard 'another' do guard 'another' do
watch('c') watch('c')
end end
end") end")
end end
it "should evaluates only the specified group" do it "should evaluates only the specified group" do
::Guard.should_receive(:add_guard).with('test', anything, {}) ::Guard.should_receive(:add_guard).with('test', anything, {})
::Guard.should_not_receive(:add_guard).with('another', anything, {}) ::Guard.should_not_receive(:add_guard).with('another', anything, {})
subject.evaluate_guardfile(:group => ['x']) subject.evaluate_guardfile(:group => ['x'])
end end
it "should evaluates only the specified groups" do it "should evaluates only the specified groups" do
::Guard.should_receive(:add_guard).with('test', anything, {}) ::Guard.should_receive(:add_guard).with('test', anything, {})
::Guard.should_receive(:add_guard).with('another', anything, {}) ::Guard.should_receive(:add_guard).with('another', anything, {})
subject.evaluate_guardfile(:group => ['x', 'y']) subject.evaluate_guardfile(:group => ['x', 'y'])
end end
end end
describe "#guard" do describe "#guard" do
it "should load a guard specified as a string from the DSL" do it "should load a guard specified as a string from the DSL" do
mock_guardfile_content("guard 'test'") mock_guardfile_content("guard 'test'")
::Guard.should_receive(:add_guard).with('test', [], {}) ::Guard.should_receive(:add_guard).with('test', [], {})
subject.evaluate_guardfile subject.evaluate_guardfile
end end
it "should load a guard specified as a symbol from the DSL" do it "should load a guard specified as a symbol from the DSL" do
mock_guardfile_content("guard :test") mock_guardfile_content("guard :test")
::Guard.should_receive(:add_guard).with(:test, [], {}) ::Guard.should_receive(:add_guard).with(:test, [], {})
subject.evaluate_guardfile subject.evaluate_guardfile
end end
it "should receive options when specified" do it "should receive options when specified" do
mock_guardfile_content("guard 'test', :opt_a => 1, :opt_b => 'fancy'") mock_guardfile_content("guard 'test', :opt_a => 1, :opt_b => 'fancy'")
::Guard.should_receive(:add_guard).with('test', anything, { :opt_a => 1, :opt_b => 'fancy' }) ::Guard.should_receive(:add_guard).with('test', anything, { :opt_a => 1, :opt_b => 'fancy' })
subject.evaluate_guardfile subject.evaluate_guardfile
end end
end end
describe "#watch" do describe "#watch" do
it "should receive watchers when specified" do it "should receive watchers when specified" do
mock_guardfile_content(" mock_guardfile_content("
@ -95,7 +95,7 @@ describe Guard::Dsl do
watch('a') { 'b' } watch('a') { 'b' }
watch('c') watch('c')
end") end")
::Guard.should_receive(:add_guard).with('test', anything, {}) do |name, watchers, options| ::Guard.should_receive(:add_guard).with('test', anything, {}) do |name, watchers, options|
watchers.size.should == 2 watchers.size.should == 2
watchers[0].pattern.should == 'a' watchers[0].pattern.should == 'a'
@ -106,11 +106,11 @@ describe Guard::Dsl do
subject.evaluate_guardfile subject.evaluate_guardfile
end end
end end
private private
def mock_guardfile_content(content) def mock_guardfile_content(content)
File.stub!(:read).with(File.expand_path('../../../Guardfile', __FILE__)) { content } File.stub!(:read).with(File.expand_path('../../../Guardfile', __FILE__)) { content }
end end
end end

View File

@ -2,10 +2,10 @@ require 'spec_helper'
describe Guard::Notifier do describe Guard::Notifier do
subject { Guard::Notifier } subject { Guard::Notifier }
describe "notify" do describe "notify" do
before(:each) { ENV["GUARD_ENV"] = 'special_test' } before(:each) { ENV["GUARD_ENV"] = 'special_test' }
if mac? if mac?
require 'growl' require 'growl'
it "should use Growl on Mac OS X" do it "should use Growl on Mac OS X" do
@ -17,7 +17,7 @@ describe Guard::Notifier do
subject.notify 'great', :title => 'Guard' subject.notify 'great', :title => 'Guard'
end end
end end
if linux? if linux?
require 'libnotify' require 'libnotify'
it "should use Libnotify on Linux" do it "should use Libnotify on Linux" do
@ -29,8 +29,28 @@ describe Guard::Notifier do
subject.notify 'great', :title => 'Guard' subject.notify 'great', :title => 'Guard'
end end
end end
context "turned off" do
before(:each) { subject.turn_off }
if mac?
require 'growl'
it "should do nothing" do
Growl.should_not_receive(:notify)
subject.notify 'great', :title => 'Guard'
end
end
if linux?
require 'libnotify'
it "should do nothing" do
Libnotify.should_not_receive(:show)
subject.notify 'great', :title => 'Guard'
end
end
end
after(:each) { ENV["GUARD_ENV"] = 'test' } after(:each) { ENV["GUARD_ENV"] = 'test' }
end end
end end

View File

@ -22,6 +22,11 @@ describe Guard do
it "should init listener" do it "should init listener" do
::Guard.listener.should be_kind_of(Guard::Listener) ::Guard.listener.should be_kind_of(Guard::Listener)
end end
it "should turn off notifier if notify option is false" do
::Guard::Notifier.should_receive(:turn_off)
::Guard.setup(:notify => false)
end
end end
describe ".get_guard_class" do describe ".get_guard_class" do