From 097ce1f17e6f5fd94dbf1d9b45a48931ad4feffb Mon Sep 17 00:00:00 2001 From: brainopia Date: Sat, 16 Apr 2011 23:43:50 +0400 Subject: [PATCH 1/2] Fix Guard.locate_guard to return path to gem folder --- lib/guard.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/guard.rb b/lib/guard.rb index 38d90cc..65dd42a 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -91,7 +91,7 @@ module Guard end def locate_guard(name) - `gem which guard/#{name}`.chomp + Gem.source_index.find_name("guard-#{name}").last.full_gem_path rescue UI.error "Could not find 'guard-#{name}' gem path." end From 9b981f5459a8b7dc779f9bc14ddf3b548aac86df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Sat, 16 Apr 2011 23:13:29 +0200 Subject: [PATCH 2/2] Run guards for new modified files only if any guard match any file (preventing from clearing the screen when no guard will run!). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- Guardfile | 2 +- lib/guard.rb | 7 +-- lib/guard/guard.rb | 1 + lib/guard/listener.rb | 1 - spec/guard/dsl_spec.rb | 21 ++++--- spec/guard/listener_spec.rb | 12 ++-- spec/guard/listeners/darwin_spec.rb | 20 +++--- spec/guard/listeners/linux_spec.rb | 10 +-- spec/guard/listeners/polling_spec.rb | 58 +++++++++--------- spec/guard/notifier_spec.rb | 21 ++++--- spec/guard/watcher_spec.rb | 92 +++++++++++++++------------- spec/guard_spec.rb | 20 +++--- spec/spec_helper.rb | 8 +-- 13 files changed, 147 insertions(+), 126 deletions(-) diff --git a/Guardfile b/Guardfile index d5c6bd9..c7303d5 100644 --- a/Guardfile +++ b/Guardfile @@ -1,4 +1,4 @@ -guard('rspec', :version => 2) do +guard('rspec', :cli => '-f doc', :version => 2) do watch(%r{^spec/(.*)_spec\.rb}) watch(%r{^lib/(.*)\.rb}) { |m| "spec/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } diff --git a/lib/guard.rb b/lib/guard.rb index 38d90cc..8e0e2b4 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -33,9 +33,7 @@ module Guard UI.error "No guards found in Guardfile, please add at least one." else listener.on_change do |files| - if Watcher.match_files?(guards, files) - run { run_on_change_for_all_guards(files) } - end + run { run_on_change_for_all_guards(files) } if Watcher.match_files?(guards, files) end UI.info "Guard is now watching at '#{Dir.pwd}'" @@ -49,11 +47,12 @@ module Guard paths = Watcher.match_files(guard, files) supervised_task(guard, :run_on_change, paths) unless paths.empty? end + # Reparse the whole directory to catch new files modified during the guards run new_modified_files = listener.modified_files([Dir.pwd + '/'], :all => true) listener.update_last_event unless new_modified_files.empty? - run { run_on_change_for_all_guards(new_modified_files) } + run { run_on_change_for_all_guards(new_modified_files) } if Watcher.match_files?(guards, files) end end diff --git a/lib/guard/guard.rb b/lib/guard/guard.rb index a6b546c..812f72e 100644 --- a/lib/guard/guard.rb +++ b/lib/guard/guard.rb @@ -1,5 +1,6 @@ module Guard class Guard + attr_accessor :watchers, :options def initialize(watchers = [], options = {}) diff --git a/lib/guard/listener.rb b/lib/guard/listener.rb index 89d90f1..3675489 100644 --- a/lib/guard/listener.rb +++ b/lib/guard/listener.rb @@ -46,7 +46,6 @@ module Guard false end - def self.mac? Config::CONFIG['target_os'] =~ /darwin/i end diff --git a/spec/guard/dsl_spec.rb b/spec/guard/dsl_spec.rb index 097ff23..86127ea 100644 --- a/spec/guard/dsl_spec.rb +++ b/spec/guard/dsl_spec.rb @@ -1,5 +1,4 @@ require 'spec_helper' -require 'guard/dsl' describe Guard::Dsl do subject { Guard::Dsl } @@ -8,14 +7,14 @@ describe Guard::Dsl do ::Guard.stub!(:add_guard) end - it "should write an error message when no Guardfile is found" do + it "displays 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 + it "displays 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/) @@ -23,15 +22,23 @@ describe Guard::Dsl do end describe ".guardfile_include?" do - it "should detect a guard specified as a string" do + it "detects a guard specified by a string with simple quotes" 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") + it "detects a guard specified by a string with double quotes" do + mock_guardfile_content('guard "test"') + subject.guardfile_include?('test').should be_true + end + it "detects a guard specified by a symbol" do + mock_guardfile_content("guard :test") + subject.guardfile_include?('test').should be_true + end + + it "detects a guard wrapped in parentheses" do + mock_guardfile_content("guard(:test)") subject.guardfile_include?('test').should be_true end end diff --git a/spec/guard/listener_spec.rb b/spec/guard/listener_spec.rb index 7fd8a79..dfbface 100644 --- a/spec/guard/listener_spec.rb +++ b/spec/guard/listener_spec.rb @@ -1,27 +1,27 @@ require 'spec_helper' describe Guard::Listener do - subject { described_class } + subject { Guard::Listener } describe ".select_and_init" do before(:each) { @target_os = Config::CONFIG['target_os'] } after(:each) { Config::CONFIG['target_os'] = @target_os } - it "should use darwin listener on Mac OS X" do + it "uses darwin listener on Mac OS X" do Config::CONFIG['target_os'] = 'darwin10.4.0' Guard::Darwin.stub(:usable?).and_return(true) Guard::Darwin.should_receive(:new) subject.select_and_init end - it "should use polling listener on Windows" do + it "uses polling listener on Windows" do Config::CONFIG['target_os'] = 'win32' Guard::Polling.stub(:usable?).and_return(true) Guard::Polling.should_receive(:new) subject.select_and_init end - it "should use linux listener on Linux" do + it "uses linux listener on Linux" do Config::CONFIG['target_os'] = 'linux' Guard::Linux.stub(:usable?).and_return(true) Guard::Linux.should_receive(:new) @@ -32,7 +32,7 @@ describe Guard::Listener do describe "#update_last_event" do subject { described_class.new } - it "should update last_event with time.now" do + it "updates last_event with time.now" do time = Time.now subject.update_last_event subject.last_event.should >= time @@ -40,4 +40,4 @@ describe Guard::Listener do end -end \ No newline at end of file +end diff --git a/spec/guard/listeners/darwin_spec.rb b/spec/guard/listeners/darwin_spec.rb index fa06f0b..7a562f1 100644 --- a/spec/guard/listeners/darwin_spec.rb +++ b/spec/guard/listeners/darwin_spec.rb @@ -5,17 +5,17 @@ describe Guard::Darwin do subject { Guard::Darwin } if linux? - it "should not be usable on linux" do + it "isn't usable on linux" do subject.should_not be_usable end end if mac? - it "should be usable on 10.6" do + it "is usable on 10.6" do subject.should be_usable end - describe "watch" do + describe "#on_change" do before(:each) do @results = [] @listener = Guard::Darwin.new @@ -24,7 +24,7 @@ describe Guard::Darwin do end end - it "should catch new file" do + it "catches new file" do file = @fixture_path.join("newfile.rb") File.exists?(file).should be_false start @@ -34,7 +34,7 @@ describe Guard::Darwin do @results.should == ['spec/fixtures/newfile.rb'] end - it "should catch file update" do + it "catches file update" do file = @fixture_path.join("folder1/file1.txt") File.exists?(file).should be_true start @@ -43,7 +43,7 @@ describe Guard::Darwin do @results.should == ['spec/fixtures/folder1/file1.txt'] end - it "should catch files update" do + it "catches files update" do file1 = @fixture_path.join("folder1/file1.txt") file2 = @fixture_path.join("folder1/folder2/file2.txt") File.exists?(file1).should be_true @@ -60,14 +60,14 @@ describe Guard::Darwin do private def start - sleep 1 + sleep 0.6 Thread.new { @listener.start } - sleep 1 + sleep 0.6 end def stop - sleep 1 + sleep 0.6 @listener.stop end -end \ No newline at end of file +end diff --git a/spec/guard/listeners/linux_spec.rb b/spec/guard/listeners/linux_spec.rb index ad45285..ba5d86f 100644 --- a/spec/guard/listeners/linux_spec.rb +++ b/spec/guard/listeners/linux_spec.rb @@ -6,17 +6,17 @@ describe Guard::Linux do subject { Guard::Linux } if mac? - it "should not be usable on 10.6" do + it "isn't usable on 10.6" do subject.should_not be_usable end end if linux? - it "should be usable on linux" do + it "is usable on linux" do subject.should be_usable end - describe "start" do + describe "#start" do before(:each) do @listener = Guard::Linux.new end @@ -40,7 +40,7 @@ describe Guard::Linux do end - describe "watch" do + describe "#on_change" do before(:each) do @results = [] @listener = Guard::Linux.new @@ -127,4 +127,4 @@ private sleep 1 end -end \ No newline at end of file +end diff --git a/spec/guard/listeners/polling_spec.rb b/spec/guard/listeners/polling_spec.rb index 3ec91a7..627a942 100644 --- a/spec/guard/listeners/polling_spec.rb +++ b/spec/guard/listeners/polling_spec.rb @@ -11,35 +11,37 @@ describe Guard::Polling do end end - it "should catch new file" do - file = @fixture_path.join("newfile.rb") - File.exists?(file).should be_false - start - FileUtils.touch file - stop - File.delete file - @results.should == ['spec/fixtures/newfile.rb'] - end + describe "#on_change" do + it "catches new file" do + file = @fixture_path.join("newfile.rb") + File.exists?(file).should be_false + start + FileUtils.touch file + stop + File.delete file + @results.should == ['spec/fixtures/newfile.rb'] + end - it "should catch file update" do - file = @fixture_path.join("folder1/file1.txt") - File.exists?(file).should be_true - start - FileUtils.touch file - stop - @results.should == ['spec/fixtures/folder1/file1.txt'] - end + it "catches file update" do + file = @fixture_path.join("folder1/file1.txt") + File.exists?(file).should be_true + start + FileUtils.touch file + stop + @results.should == ['spec/fixtures/folder1/file1.txt'] + end - it "should catch files update" do - file1 = @fixture_path.join("folder1/file1.txt") - file2 = @fixture_path.join("folder1/folder2/file2.txt") - File.exists?(file1).should be_true - File.exists?(file2).should be_true - start - FileUtils.touch file1 - FileUtils.touch file2 - stop - @results.sort.should == ['spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/folder2/file2.txt'] + it "catches files update" do + file1 = @fixture_path.join("folder1/file1.txt") + file2 = @fixture_path.join("folder1/folder2/file2.txt") + File.exists?(file1).should be_true + File.exists?(file2).should be_true + start + FileUtils.touch file1 + FileUtils.touch file2 + stop + @results.sort.should == ['spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/folder2/file2.txt'] + end end private @@ -54,4 +56,4 @@ private @listener.stop end -end \ No newline at end of file +end diff --git a/spec/guard/notifier_spec.rb b/spec/guard/notifier_spec.rb index 5431536..b12781b 100644 --- a/spec/guard/notifier_spec.rb +++ b/spec/guard/notifier_spec.rb @@ -3,12 +3,15 @@ require 'spec_helper' describe Guard::Notifier do subject { Guard::Notifier } - describe "notify" do - before(:each) { ENV["GUARD_ENV"] = 'special_test' } + describe ".notify" do + before(:each) do + @saved_guard_env = ENV["GUARD_ENV"] + ENV["GUARD_ENV"] = 'dont_mute_notify' + end if mac? require 'growl' - it "should use Growl on Mac OS X" do + it "uses Growl on Mac OS X" do Growl.should_receive(:notify).with("great", :title => "Guard", :icon => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s, @@ -20,7 +23,7 @@ describe Guard::Notifier do if linux? require 'libnotify' - it "should use Libnotify on Linux" do + it "uses Libnotify on Linux" do Libnotify.should_receive(:show).with( :body => "great", :summary => 'Guard', @@ -30,12 +33,12 @@ describe Guard::Notifier do end end - context "turned off" do + describe ".turn_off" do before(:each) { subject.turn_off } if mac? require 'growl' - it "should do nothing" do + it "does nothing" do Growl.should_not_receive(:notify) subject.notify 'great', :title => 'Guard' end @@ -43,14 +46,14 @@ describe Guard::Notifier do if linux? require 'libnotify' - it "should do nothing" do + it "does 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"] = @saved_guard_env } end -end \ No newline at end of file +end diff --git a/spec/guard/watcher_spec.rb b/spec/guard/watcher_spec.rb index b4889c0..65d1d1e 100644 --- a/spec/guard/watcher_spec.rb +++ b/spec/guard/watcher_spec.rb @@ -2,62 +2,66 @@ require 'spec_helper' require 'guard/guard' describe Guard::Watcher do - - describe "pattern" do - it "should be required" do - expect { Guard::Watcher.new }.to raise_error(ArgumentError) - end - - it "can be a string" do - Guard::Watcher.new('spec_helper.rb').pattern.should == 'spec_helper.rb' - end - - it "can be a regexp" do - Guard::Watcher.new(/spec_helper\.rb/).pattern.should == /spec_helper\.rb/ - end - - describe "string looking like a regex" do - before(:each) { Guard::UI.should_receive(:info).any_number_of_times } - - specify { Guard::Watcher.new('^spec_helper.rb').pattern.should == /^spec_helper.rb/ } - specify { Guard::Watcher.new('spec_helper.rb$').pattern.should == /spec_helper.rb$/ } - specify { Guard::Watcher.new('spec_helper\.rb').pattern.should == /spec_helper\.rb/ } - specify { Guard::Watcher.new('.*_spec.rb').pattern.should == /.*_spec.rb/ } + + describe "#initialize" do + describe "pattern parameter" do + it "is required" do + expect { Guard::Watcher.new }.to raise_error(ArgumentError) + end + + it "can be a string" do + Guard::Watcher.new('spec_helper.rb').pattern.should == 'spec_helper.rb' + end + + it "can be a regexp" do + Guard::Watcher.new(/spec_helper\.rb/).pattern.should == /spec_helper\.rb/ + end + + describe "can be a string looking like a regex (deprecated)" do + before(:each) { Guard::UI.should_receive(:info).any_number_of_times } + + it "and is automatically casted to a regex" do + Guard::Watcher.new('^spec_helper.rb').pattern.should == /^spec_helper.rb/ + Guard::Watcher.new('spec_helper.rb$').pattern.should == /spec_helper.rb$/ + Guard::Watcher.new('spec_helper\.rb').pattern.should == /spec_helper\.rb/ + Guard::Watcher.new('.*_spec.rb').pattern.should == /.*_spec.rb/ + end + end end end - + describe "action" do it "should set action to nil by default" do Guard::Watcher.new(/spec_helper\.rb/).action.should be_nil end - + it "should set action with a block" do action = lambda { |m| "spec/#{m[1]}_spec.rb" } Guard::Watcher.new(%r{^lib/(.*).rb}, action).action.should == action end end - + describe ".match_files" do before(:all) { @guard = Guard::Guard.new } - + describe "a watcher's with no action" do context "regex pattern" do before(:all) { @guard.watchers = [Guard::Watcher.new(/.*_spec\.rb/)] } - + it "should return paths as they came" do Guard::Watcher.match_files(@guard, ['guard_rocks_spec.rb']).should == ['guard_rocks_spec.rb'] end end - + context "string pattern" do before(:all) { @guard.watchers = [Guard::Watcher.new('guard_rocks_spec.rb')] } - + it "should return paths as they came" do Guard::Watcher.match_files(@guard, ['guard_rocks_spec.rb']).should == ['guard_rocks_spec.rb'] end end end - + describe "a watcher's action with an arity equal to 0" do before(:all) do @guard.watchers = [ @@ -69,7 +73,7 @@ describe Guard::Watcher do Guard::Watcher.new(/^uptime\.rb/, lambda { `uptime > /dev/null` }) ] end - + it "should return paths specified in the watcher's action" do Guard::Watcher.match_files(@guard, ['spec_helper.rb']).should == ['spec'] end @@ -89,7 +93,7 @@ describe Guard::Watcher do Guard::Watcher.match_files(@guard, ['uptime.rb']).should == [] end end - + describe "a watcher's action with an arity equal to 1" do before(:all) do @guard.watchers = [ @@ -101,7 +105,7 @@ describe Guard::Watcher do Guard::Watcher.new(/uptime(.*)\.rb/, lambda { |m| `uptime > /dev/null` }) ] end - + it "should return paths after watcher's action has been called against them" do Guard::Watcher.match_files(@guard, ['lib/my_wonderful_lib.rb']).should == ['spec/my_wonderful_lib_spec.rb'] end @@ -121,56 +125,56 @@ describe Guard::Watcher do Guard::Watcher.match_files(@guard, ['uptime.rb']).should == [] end end - + describe "an exception is raised" do before(:all) { @guard.watchers = [Guard::Watcher.new('evil.rb', lambda { raise "EVIL" })] } - + it "should display an error" do Guard::UI.should_receive(:error).with("Problem with watch action!") Guard::Watcher.match_files(@guard, ['evil.rb']) end end end - + describe ".match_files?" do before(:all) do @guard1 = Guard::Guard.new([Guard::Watcher.new(/.*_spec\.rb/)]) @guard2 = Guard::Guard.new([Guard::Watcher.new('spec_helper.rb', 'spec')]) @guards = [@guard1, @guard2] end - + describe "with at least on watcher that match a file given" do specify { Guard::Watcher.match_files?(@guards, ['lib/my_wonderful_lib.rb', 'guard_rocks_spec.rb']).should be_true } end - + describe "with no watcher matching a file given" do specify { Guard::Watcher.match_files?(@guards, ['lib/my_wonderful_lib.rb']).should be_false } end end - + describe "#match_file?" do describe "string pattern" do describe "normal string" do subject { Guard::Watcher.new('guard_rocks_spec.rb') } - + specify { subject.match_file?('lib/my_wonderful_lib.rb').should be_false } specify { subject.match_file?('guard_rocks_spec.rb').should be_true } end - + describe "string representing a regexp converted (while deprecation is active)" do subject { Guard::Watcher.new('^guard_rocks_spec\.rb$') } - + specify { subject.match_file?('lib/my_wonderful_lib.rb').should be_false } specify { subject.match_file?('guard_rocks_spec.rb').should be_true } end end - + describe "regexp pattern" do subject { Guard::Watcher.new(/.*_spec\.rb/) } - + specify { subject.match_file?('lib/my_wonderful_lib.rb').should be_false } specify { subject.match_file?('guard_rocks_spec.rb').should be_true } end end - + end \ No newline at end of file diff --git a/spec/guard_spec.rb b/spec/guard_spec.rb index dbd1c14..97f6d6a 100644 --- a/spec/guard_spec.rb +++ b/spec/guard_spec.rb @@ -44,7 +44,7 @@ describe Guard do end describe ".locate_guard" do - it "should return guard-rspec gem path" do + it "returns guard-rspec gem path" do guard_path = Guard.locate_guard('rspec') guard_path.should match(/^.*\/guard-rspec-.*$/) guard_path.should == guard_path.chomp @@ -54,7 +54,7 @@ describe Guard do describe ".supervised_task" do subject { ::Guard.setup } before(:each) do - @g = mock(Guard::Guard) + @g = mock(Guard::Guard).as_null_object subject.guards.push(@g) end @@ -64,15 +64,15 @@ describe Guard do @g.stub!(:regular_with_arg).with("given_path") { "i'm a success" } end - it "should not fire the guard with a supervised method without argument" do + it "doesn't fire the guard with a supervised method without argument" do lambda { subject.supervised_task(@g, :regular) }.should_not change(subject.guards, :size) end - it "should not fire the guard with a supervised method with argument" do + it "doesn't fire the guard with a supervised method with argument" do lambda { subject.supervised_task(@g, :regular_with_arg, "given_path") }.should_not change(subject.guards, :size) end - it "should return the result of the supervised method" do + it "returns the result of the supervised method" do ::Guard.supervised_task(@g, :regular).should be_true ::Guard.supervised_task(@g, :regular_with_arg, "given_path").should == "i'm a success" end @@ -81,18 +81,24 @@ describe Guard do describe "tasks that raise an exception" do before(:each) { @g.stub!(:failing) { raise "I break your system" } } - it "should fire the guard" do + it "fires the guard" do lambda { subject.supervised_task(@g, :failing) }.should change(subject.guards, :size).by(-1) subject.guards.should_not include(@g) end - it "should return the exception object" do + it "returns the exception object" do failing_result = ::Guard.supervised_task(@g, :failing) failing_result.should be_kind_of(Exception) failing_result.message.should == 'I break your system' end end end + + describe ".locate_guard" do + it "returns the path of the guard gem" do + Guard.locate_guard('rspec').should == Gem.source_index.find_name("guard-rspec").last.full_gem_path + end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bbbe914..1520849 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,12 +10,12 @@ puts "Please do not update/create files while tests are running." RSpec.configure do |config| config.color_enabled = true - + config.filter_run :focus => true config.run_all_when_everything_filtered = true - + config.before(:each) do @fixture_path = Pathname.new(File.expand_path('../fixtures/', __FILE__)) end - -end \ No newline at end of file + +end