Skip to content

Commit

Permalink
Added a command line option (-n false) to disable notifications (grow…
Browse files Browse the repository at this point in the history
…l/libnotify). closed guard#28
  • Loading branch information
thibaudgg committed Apr 10, 2011
1 parent 31c43f7 commit 42c2724
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 54 deletions.
3 changes: 3 additions & 0 deletions lib/guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def setup(options = {})
@options = options
@listener = Listener.select_and_init
@guards = []

Notifier.turn_off unless options[:notify]

self
end

Expand Down
19 changes: 10 additions & 9 deletions lib/guard/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
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")
Expand All @@ -29,12 +30,12 @@ def init(guard_name = nil)
::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
18 changes: 11 additions & 7 deletions lib/guard/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

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']
Expand All @@ -20,9 +24,9 @@ def self.notify(message, options = {})
end
end
end

private

def self.image_path(image)
images_path = Pathname.new(File.dirname(__FILE__)).join('../../images')
case image
Expand All @@ -37,7 +41,7 @@ def self.image_path(image)
image
end
end

def self.growl_installed?
@installed ||= begin
require 'growl'
Expand All @@ -47,7 +51,7 @@ def self.growl_installed?
false
end
end

def self.libnotify_installed?
@installed ||= begin
require 'libnotify'
Expand All @@ -57,6 +61,6 @@ def self.libnotify_installed?
false
end
end

end
end
18 changes: 9 additions & 9 deletions lib/guard/ui.rb
Original file line number Diff line number Diff line change
@@ -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
48 changes: 24 additions & 24 deletions spec/guard/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,39 @@

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("
Expand All @@ -44,58 +44,58 @@
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("
guard 'test' 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'
Expand All @@ -106,11 +106,11 @@
subject.evaluate_guardfile
end
end

private

def mock_guardfile_content(content)
File.stub!(:read).with(File.expand_path('../../../Guardfile', __FILE__)) { content }
end

end
30 changes: 25 additions & 5 deletions spec/guard/notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

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
Expand All @@ -17,7 +17,7 @@
subject.notify 'great', :title => 'Guard'
end
end

if linux?
require 'libnotify'
it "should use Libnotify on Linux" do
Expand All @@ -29,8 +29,28 @@
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
5 changes: 5 additions & 0 deletions spec/guard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
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
Expand Down

0 comments on commit 42c2724

Please sign in to comment.