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
@listener = Listener.select_and_init
@guards = []
Notifier.turn_off unless options[:notify]
self
end

View File

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

View File

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

View File

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

View File

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

View File

@ -2,10 +2,10 @@ require 'spec_helper'
describe Guard::Notifier do
subject { Guard::Notifier }
describe "notify" do
before(:each) { ENV["GUARD_ENV"] = 'special_test' }
if mac?
require 'growl'
it "should use Growl on Mac OS X" do
@ -17,7 +17,7 @@ describe Guard::Notifier do
subject.notify 'great', :title => 'Guard'
end
end
if linux?
require 'libnotify'
it "should use Libnotify on Linux" do
@ -29,8 +29,28 @@ describe Guard::Notifier do
subject.notify 'great', :title => 'Guard'
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' }
end
end

View File

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