watcher init now has an addition param that allows the user to return any obj type. The spec has been restored to its original and then more specs added for object support.

This commit is contained in:
Kevin Krauss 2011-10-07 10:36:47 -07:00
parent d6b47f6448
commit 33542acec9
2 changed files with 105 additions and 26 deletions

View File

@ -6,15 +6,15 @@ module Guard
#
class Watcher
attr_accessor :pattern, :action
attr_accessor :pattern, :action, :any_rtn
# Initialize a file watcher.
#
# @param [String, Regexp] pattern the pattern to be watched by the guard
# @param [Block] action the action to execute before passing the result to the Guard
#
def initialize(pattern, action = nil)
@pattern, @action = pattern, action
def initialize(pattern, action = nil, any_rtn = false)
@pattern, @action, @any_rtn = pattern, action, any_rtn
@@warning_printed ||= false
# deprecation warning
@ -38,21 +38,26 @@ module Guard
#
# @param [Guard::Guard] guard the guard which watchers are used
# @param [Array<String>] files the changed files
# @return [Array<String>] the matched files
# @optional_return [Anything] return whatever you want to path
# @return [Array<Object>] the matched watcher response
def self.match_files(guard, files)
guard.watchers.inject([]) do |paths, watcher|
wa = watcher.any_rtn
files.each do |file|
if matches = watcher.match_file?(file)
if watcher.action
result = watcher.call_action(matches)
paths << result
if wa
paths << result
elsif result.respond_to?(:empty?) && ! result.empty?
paths << Array(result)
end
else
paths << matches[0]
end
end
end
paths
wa ? paths : paths.flatten.map{|p| p.to_s}
end
end

View File

@ -65,8 +65,8 @@ describe Guard::Watcher do
end
end
end
context "with a watcher action without parameter" do
context "with a watcher action without parameter for a watcher that matches file strings" do
before(:all) do
@guard.watchers = [
described_class.new('spec_helper.rb', lambda { 'spec' }),
@ -78,6 +78,43 @@ describe Guard::Watcher do
]
end
it "returns a single file specified within the action" do
described_class.match_files(@guard, ['spec_helper.rb']).should == ['spec']
end
it "returns multiple files specified within the action" do
described_class.match_files(@guard, ['hash.rb']).should == ['foo', 'bar']
end
it "returns multiple files by combining the results of different actions" do
described_class.match_files(@guard, ['spec_helper.rb', 'array.rb']).should == ['spec', 'foo', 'bar']
end
it "returns nothing if the action returns something other than a string or an array of strings" do
described_class.match_files(@guard, ['addition.rb']).should == []
end
it "returns nothing if the action response is empty" do
described_class.match_files(@guard, ['blank.rb']).should == []
end
it "returns nothing if the action returns nothing" do
described_class.match_files(@guard, ['uptime.rb']).should == []
end
end
context 'with a watcher action without parameter for a watcher that matches information objects' do
before(:all) do
@guard.watchers = [
described_class.new('spec_helper.rb', lambda { 'spec' }, true),
described_class.new('addition.rb', lambda { 1 + 1 }, true),
described_class.new('hash.rb', lambda { Hash[:foo, 'bar'] }, true),
described_class.new('array.rb', lambda { ['foo', 'bar'] }, true),
described_class.new('blank.rb', lambda { '' }, true),
described_class.new(/^uptime\.rb/, lambda { `uptime > /dev/null` }, true)
]
end
it "returns a single file specified within the action" do
described_class.match_files(@guard, ['spec_helper.rb']).class.should == Array
described_class.match_files(@guard, ['spec_helper.rb']).empty?.should == false
@ -104,16 +141,53 @@ describe Guard::Watcher do
described_class.match_files(@guard, ['uptime.rb']).should == ['']
end
end
context "with a watcher action that takes a parameter for a watcher that matches file strings" do
before(:all) do
@guard.watchers = [
described_class.new(%r{lib/(.*)\.rb}, lambda { |m| "spec/#{m[1]}_spec.rb" }),
described_class.new(/addition(.*)\.rb/, lambda { |m| 1 + 1 }),
described_class.new('hash.rb', lambda { Hash[:foo, 'bar'] }),
described_class.new(/array(.*)\.rb/, lambda { |m| ['foo', 'bar'] }),
described_class.new(/blank(.*)\.rb/, lambda { |m| '' }),
described_class.new(/uptime(.*)\.rb/, lambda { |m| `uptime > /dev/null` })
]
end
context "with a watcher action that takes a parameter" do
it "returns a substituted single file specified within the action" do
described_class.match_files(@guard, ['lib/my_wonderful_lib.rb']).should == ['spec/my_wonderful_lib_spec.rb']
end
it "returns multiple files specified within the action" do
described_class.match_files(@guard, ['hash.rb']).should == ['foo', 'bar']
end
it "returns multiple files by combining the results of different actions" do
described_class.match_files(@guard, ['lib/my_wonderful_lib.rb', 'array.rb']).should == ['spec/my_wonderful_lib_spec.rb', 'foo', 'bar']
end
it "returns nothing if the action returns something other than a string or an array of strings" do
described_class.match_files(@guard, ['addition.rb']).should == []
end
it "returns nothing if the action response is empty" do
described_class.match_files(@guard, ['blank.rb']).should == []
end
it "returns nothing if the action returns nothing" do
described_class.match_files(@guard, ['uptime.rb']).should == []
end
end
context "with a watcher action that takes a parameter for a watcher that matches information objects" do
before(:all) do
@guard.watchers = [
described_class.new(%r{lib/(.*)\.rb}, lambda { |m| "spec/#{m[1]}_spec.rb" }),
described_class.new(/addition(.*)\.rb/, lambda { |m| (1 + 1).to_s + m[0] }),
described_class.new('hash.rb', lambda {|m| Hash[:foo, 'bar', :file_name, m[0]] }),
described_class.new(/array(.*)\.rb/, lambda { |m| ['foo', 'bar', m[0]] }),
described_class.new(/blank(.*)\.rb/, lambda { |m| '' }),
described_class.new(/uptime(.*)\.rb/, lambda { |m| `uptime > /dev/null` })
described_class.new(%r{lib/(.*)\.rb}, lambda { |m| "spec/#{m[1]}_spec.rb" }, true),
described_class.new(/addition(.*)\.rb/, lambda { |m| (1 + 1).to_s + m[0] }, true),
described_class.new('hash.rb', lambda {|m| Hash[:foo, 'bar', :file_name, m[0]] }, true),
described_class.new(/array(.*)\.rb/, lambda { |m| ['foo', 'bar', m[0]] }, true),
described_class.new(/blank(.*)\.rb/, lambda { |m| '' }, true),
described_class.new(/uptime(.*)\.rb/, lambda { |m| `uptime > /dev/null` }, true)
]
end
@ -143,18 +217,18 @@ describe Guard::Watcher do
end
context "with an exception that is raised" do
before(:all) { @guard.watchers = [described_class.new('evil.rb', lambda { raise "EVIL" })] }
before(:all) { @guard.watchers = [described_class.new('evil.rb', lambda { raise "EVIL" })] }
it "displays the error and backtrace" do
Guard::UI.should_receive(:error) { |msg|
msg.should include("Problem with watch action!")
msg.should include("EVIL")
}
it "displays the error and backtrace" do
Guard::UI.should_receive(:error) { |msg|
msg.should include("Problem with watch action!")
msg.should include("EVIL")
}
described_class.match_files(@guard, ['evil.rb'])
end
end
end
described_class.match_files(@guard, ['evil.rb'])
end
end
end
describe ".match_files?" do
before(:all) do