Refactored Guard::Dsl#callback and updated specs, improved inline docs for Guard::Hook#hook, added ENV["GUARD_ENV"] = 'development' in Guardfile so we see hooks firing! Run specs on REE too.

Signed-off-by: Rémy Coutable <rymai@rymai.me>
This commit is contained in:
Rémy Coutable 2011-04-30 00:40:12 +02:00
parent 134cbdb007
commit e853009528
14 changed files with 71 additions and 51 deletions

View File

@ -4,11 +4,10 @@ gemspec
require 'rbconfig' require 'rbconfig'
if Config::CONFIG['target_os'] =~ /darwin/i if Config::CONFIG['host_os'] =~ /darwin/i
gem 'rb-fsevent', '>= 0.3.9' gem 'rb-fsevent', '>= 0.3.9'
gem 'growl', '~> 1.0.3' gem 'growl', '~> 1.0'
end elsif Config::CONFIG['host_os'] =~ /linux/i
if Config::CONFIG['target_os'] =~ /linux/i
gem 'rb-inotify', '>= 0.5.1' gem 'rb-inotify', '>= 0.5.1'
gem 'libnotify', '~> 0.1.3' gem 'libnotify', '~> 0.1'
end end

View File

@ -1,3 +1,5 @@
ENV["GUARD_ENV"] = 'development'
guard('rspec', :cli => '-f doc', :version => 2) do guard('rspec', :cli => '-f doc', :version => 2) do
watch(%r{^spec/(.*)_spec\.rb}) watch(%r{^spec/(.*)_spec\.rb})
watch(%r{^lib/(.*)\.rb}) { |m| "spec/#{m[1]}_spec.rb" } watch(%r{^lib/(.*)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }

View File

@ -8,13 +8,13 @@ task :default => :spec
namespace(:spec) do namespace(:spec) do
desc "Run all specs on multiple ruby versions (requires rvm)" desc "Run all specs on multiple ruby versions (requires rvm)"
task(:portability) do task(:portability) do
%w[1.8.6 1.8.7 1.9.2].each do |version| %w[1.8.6 1.8.7 1.9.2 ree].each do |version|
system <<-BASH system <<-BASH
bash -c 'source ~/.rvm/scripts/rvm; bash -c 'source ~/.rvm/scripts/rvm;
rvm #{version}; rvm #{version};
echo "--------- version #{version} ----------\n"; echo "--------- version #{version} ----------\n";
bundle install; bundle install;
rake spec' rake spec;'
BASH BASH
end end
end end

View File

@ -43,11 +43,7 @@ module Guard
end end
def callback(*args, &listener) def callback(*args, &listener)
listener, events = if args.size > 1 listener, events = args.size > 1 ? args : [listener, args[0]]
args
else
[listener, args[0]]
end
@callbacks << { :events => events, :listener => listener } @callbacks << { :events => events, :listener => listener }
end end

View File

@ -1,14 +1,32 @@
module Guard module Guard
module Hook module Hook
def self.included(base) def self.included(base)
base.send :include, InstanceMethods base.send :include, InstanceMethods
end end
module InstanceMethods module InstanceMethods
# When passed a sybmol, #hook will generate a hook name # When +event+ is a Symbol, #hook will generate a hook name
# from the symbol and calling method name. When passed # by concatenating the method name from where #hook is called
# a string, #hook will turn the string into a symbol # with the given Symbol.
# directly. # Example:
# def run_all
# hook :foo
# end
# Here, when #run_all is called, #hook will notify callbacks
# registered for the "run_all_foo" event.
#
# When +event+ is a String, #hook will directly turn the String
# into a Symbol.
# Example:
# def run_all
# hook "foo_bar"
# end
# Here, when #run_all is called, #hook will notify callbacks
# registered for the "foo_bar" event.
#
# +args+ parameter is passed as is to the callbacks registered
# for the given event.
def hook(event, *args) def hook(event, *args)
hook_name = if event.is_a? Symbol hook_name = if event.is_a? Symbol
calling_method = caller[0][/`([^']*)'/, 1] calling_method = caller[0][/`([^']*)'/, 1]
@ -51,5 +69,6 @@ module Guard
@callbacks = nil @callbacks = nil
end end
end end
end end
end end

View File

@ -2,21 +2,21 @@ 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]

View File

@ -1,8 +1,11 @@
require 'spec_helper' require 'spec_helper'
require 'guard/guard'
describe Guard::Dsl do describe Guard::Dsl do
subject { Guard::Dsl } subject { Guard::Dsl }
class Guard::Dummy < Guard::Guard; end
before(:each) do before(:each) do
::Guard.stub!(:add_guard) ::Guard.stub!(:add_guard)
end end
@ -114,18 +117,21 @@ describe Guard::Dsl do
describe "#callback" do describe "#callback" do
it "creates callbacks for the guard" do it "creates callbacks for the guard" do
class MyCustomCallback class MyCustomCallback
def self.call(guard_class, event, args)
# do nothing
end
end end
mock_guardfile_content(" mock_guardfile_content('
guard 'test' do guard :dummy do
callback(:start_end) { 'Guard::Test started!' } callback(:start_end) { |guard_class, event, args| "#{guard_class} executed \'#{event}\' hook with #{args}!" }
callback(MyCustomCallback, [:start_begin, :run_all_begin]) callback(MyCustomCallback, [:start_begin, :run_all_begin])
end") end')
::Guard.should_receive(:add_guard).with('test', anything, anything, {}) do |name, watchers, callbacks, options| ::Guard.should_receive(:add_guard).with(:dummy, anything, anything, {}) do |name, watchers, callbacks, options|
callbacks.should have(2).items callbacks.should have(2).items
callbacks[0][:events].should == :start_end callbacks[0][:events].should == :start_end
callbacks[0][:listener].call.should == proc { 'Guard::Test started!' }.call callbacks[0][:listener].call(Guard::Dummy, :start_end, 'foo').should == "Guard::Dummy executed 'start_end' hook with foo!"
callbacks[1][:events].should == [:start_begin, :run_all_begin] callbacks[1][:events].should == [:start_begin, :run_all_begin]
callbacks[1][:listener].should == MyCustomCallback callbacks[1][:listener].should == MyCustomCallback
end end

View File

@ -19,9 +19,8 @@ describe Guard::Notifier do
) )
subject.notify 'great', :title => 'Guard' subject.notify 'great', :title => 'Guard'
end end
end
if linux? elsif linux?
require 'libnotify' require 'libnotify'
it "uses Libnotify on Linux" do it "uses Libnotify on Linux" do
Libnotify.should_receive(:show).with( Libnotify.should_receive(:show).with(
@ -42,9 +41,8 @@ describe Guard::Notifier do
Growl.should_not_receive(:notify) Growl.should_not_receive(:notify)
subject.notify 'great', :title => 'Guard' subject.notify 'great', :title => 'Guard'
end end
end
if linux? elsif linux?
require 'libnotify' require 'libnotify'
it "does nothing" do it "does nothing" do
Libnotify.should_not_receive(:show) Libnotify.should_not_receive(:show)