Merge branch 'master' into hook

Conflicts:
	lib/guard/guard.rb
	spec/guard_spec.rb
This commit is contained in:
Rémy Coutable 2011-04-16 23:23:08 +02:00
commit 5b8ae609da
12 changed files with 145 additions and 126 deletions

View File

@ -1,4 +1,4 @@
guard('rspec', :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" }
watch('spec/spec_helper.rb') { "spec" } watch('spec/spec_helper.rb') { "spec" }

View File

@ -34,9 +34,7 @@ module Guard
UI.error "No guards found in Guardfile, please add at least one." UI.error "No guards found in Guardfile, please add at least one."
else else
listener.on_change do |files| listener.on_change do |files|
if Watcher.match_files?(guards, files) run { run_on_change_for_all_guards(files) } if Watcher.match_files?(guards, files)
run { run_on_change_for_all_guards(files) }
end
end end
UI.info "Guard is now watching at '#{Dir.pwd}'" UI.info "Guard is now watching at '#{Dir.pwd}'"
@ -50,11 +48,12 @@ module Guard
paths = Watcher.match_files(guard, files) paths = Watcher.match_files(guard, files)
supervised_task(guard, :run_on_change, paths) unless paths.empty? supervised_task(guard, :run_on_change, paths) unless paths.empty?
end end
# Reparse the whole directory to catch new files modified during the guards run # Reparse the whole directory to catch new files modified during the guards run
new_modified_files = listener.modified_files([Dir.pwd + '/'], :all => true) new_modified_files = listener.modified_files([Dir.pwd + '/'], :all => true)
listener.update_last_event listener.update_last_event
unless new_modified_files.empty? 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
end end
@ -97,7 +96,7 @@ module Guard
end end
def locate_guard(name) def locate_guard(name)
`gem which guard/#{name}`.chomp Gem.source_index.find_name("guard-#{name}").last.full_gem_path
rescue rescue
UI.error "Could not find 'guard-#{name}' gem path." UI.error "Could not find 'guard-#{name}' gem path."
end end

View File

@ -46,7 +46,6 @@ module Guard
false false
end end
def self.mac? def self.mac?
Config::CONFIG['target_os'] =~ /darwin/i Config::CONFIG['target_os'] =~ /darwin/i
end end

View File

@ -1,5 +1,4 @@
require 'spec_helper' require 'spec_helper'
require 'guard/dsl'
describe Guard::Dsl do describe Guard::Dsl do
subject { Guard::Dsl } subject { Guard::Dsl }
@ -8,14 +7,14 @@ describe Guard::Dsl do
::Guard.stub!(:add_guard) ::Guard.stub!(:add_guard)
end 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") Dir.stub!(:pwd).and_return("no_guardfile_here")
Guard::UI.should_receive(:error).with("No Guardfile in current folder, please create one.") Guard::UI.should_receive(:error).with("No Guardfile in current folder, please create one.")
lambda { subject.evaluate_guardfile }.should raise_error lambda { subject.evaluate_guardfile }.should raise_error
end 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!") mock_guardfile_content("This Guardfile is invalid!")
Guard::UI.should_receive(:error).with(/Invalid Guardfile, original error is:\n/) Guard::UI.should_receive(:error).with(/Invalid Guardfile, original error is:\n/)
@ -23,15 +22,23 @@ describe Guard::Dsl do
end end
describe ".guardfile_include?" do 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'") mock_guardfile_content("guard 'test'")
subject.guardfile_include?('test').should be_true subject.guardfile_include?('test').should be_true
end end
it "should detect a guard specified as a symbol" do it "detects a guard specified by a string with double quotes" do
mock_guardfile_content("guard :test") 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 subject.guardfile_include?('test').should be_true
end end
end end

View File

@ -1,27 +1,27 @@
require 'spec_helper' require 'spec_helper'
describe Guard::Listener do describe Guard::Listener do
subject { described_class } subject { Guard::Listener }
describe ".select_and_init" do describe ".select_and_init" do
before(:each) { @target_os = Config::CONFIG['target_os'] } before(:each) { @target_os = Config::CONFIG['target_os'] }
after(:each) { Config::CONFIG['target_os'] = @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' Config::CONFIG['target_os'] = 'darwin10.4.0'
Guard::Darwin.stub(:usable?).and_return(true) Guard::Darwin.stub(:usable?).and_return(true)
Guard::Darwin.should_receive(:new) Guard::Darwin.should_receive(:new)
subject.select_and_init subject.select_and_init
end end
it "should use polling listener on Windows" do it "uses polling listener on Windows" do
Config::CONFIG['target_os'] = 'win32' Config::CONFIG['target_os'] = 'win32'
Guard::Polling.stub(:usable?).and_return(true) Guard::Polling.stub(:usable?).and_return(true)
Guard::Polling.should_receive(:new) Guard::Polling.should_receive(:new)
subject.select_and_init subject.select_and_init
end end
it "should use linux listener on Linux" do it "uses linux listener on Linux" do
Config::CONFIG['target_os'] = 'linux' Config::CONFIG['target_os'] = 'linux'
Guard::Linux.stub(:usable?).and_return(true) Guard::Linux.stub(:usable?).and_return(true)
Guard::Linux.should_receive(:new) Guard::Linux.should_receive(:new)
@ -32,7 +32,7 @@ describe Guard::Listener do
describe "#update_last_event" do describe "#update_last_event" do
subject { described_class.new } subject { described_class.new }
it "should update last_event with time.now" do it "updates last_event with time.now" do
time = Time.now time = Time.now
subject.update_last_event subject.update_last_event
subject.last_event.should >= time subject.last_event.should >= time

View File

@ -5,17 +5,17 @@ describe Guard::Darwin do
subject { Guard::Darwin } subject { Guard::Darwin }
if linux? if linux?
it "should not be usable on linux" do it "isn't usable on linux" do
subject.should_not be_usable subject.should_not be_usable
end end
end end
if mac? if mac?
it "should be usable on 10.6" do it "is usable on 10.6" do
subject.should be_usable subject.should be_usable
end end
describe "watch" do describe "#on_change" do
before(:each) do before(:each) do
@results = [] @results = []
@listener = Guard::Darwin.new @listener = Guard::Darwin.new
@ -24,7 +24,7 @@ describe Guard::Darwin do
end end
end end
it "should catch new file" do it "catches new file" do
file = @fixture_path.join("newfile.rb") file = @fixture_path.join("newfile.rb")
File.exists?(file).should be_false File.exists?(file).should be_false
start start
@ -34,7 +34,7 @@ describe Guard::Darwin do
@results.should == ['spec/fixtures/newfile.rb'] @results.should == ['spec/fixtures/newfile.rb']
end end
it "should catch file update" do it "catches file update" do
file = @fixture_path.join("folder1/file1.txt") file = @fixture_path.join("folder1/file1.txt")
File.exists?(file).should be_true File.exists?(file).should be_true
start start
@ -43,7 +43,7 @@ describe Guard::Darwin do
@results.should == ['spec/fixtures/folder1/file1.txt'] @results.should == ['spec/fixtures/folder1/file1.txt']
end end
it "should catch files update" do it "catches files update" do
file1 = @fixture_path.join("folder1/file1.txt") file1 = @fixture_path.join("folder1/file1.txt")
file2 = @fixture_path.join("folder1/folder2/file2.txt") file2 = @fixture_path.join("folder1/folder2/file2.txt")
File.exists?(file1).should be_true File.exists?(file1).should be_true
@ -60,13 +60,13 @@ describe Guard::Darwin do
private private
def start def start
sleep 1 sleep 0.6
Thread.new { @listener.start } Thread.new { @listener.start }
sleep 1 sleep 0.6
end end
def stop def stop
sleep 1 sleep 0.6
@listener.stop @listener.stop
end end

View File

@ -6,17 +6,17 @@ describe Guard::Linux do
subject { Guard::Linux } subject { Guard::Linux }
if mac? if mac?
it "should not be usable on 10.6" do it "isn't usable on 10.6" do
subject.should_not be_usable subject.should_not be_usable
end end
end end
if linux? if linux?
it "should be usable on linux" do it "is usable on linux" do
subject.should be_usable subject.should be_usable
end end
describe "start" do describe "#start" do
before(:each) do before(:each) do
@listener = Guard::Linux.new @listener = Guard::Linux.new
end end
@ -40,7 +40,7 @@ describe Guard::Linux do
end end
describe "watch" do describe "#on_change" do
before(:each) do before(:each) do
@results = [] @results = []
@listener = Guard::Linux.new @listener = Guard::Linux.new

View File

@ -11,7 +11,8 @@ describe Guard::Polling do
end end
end end
it "should catch new file" do describe "#on_change" do
it "catches new file" do
file = @fixture_path.join("newfile.rb") file = @fixture_path.join("newfile.rb")
File.exists?(file).should be_false File.exists?(file).should be_false
start start
@ -21,7 +22,7 @@ describe Guard::Polling do
@results.should == ['spec/fixtures/newfile.rb'] @results.should == ['spec/fixtures/newfile.rb']
end end
it "should catch file update" do it "catches file update" do
file = @fixture_path.join("folder1/file1.txt") file = @fixture_path.join("folder1/file1.txt")
File.exists?(file).should be_true File.exists?(file).should be_true
start start
@ -30,7 +31,7 @@ describe Guard::Polling do
@results.should == ['spec/fixtures/folder1/file1.txt'] @results.should == ['spec/fixtures/folder1/file1.txt']
end end
it "should catch files update" do it "catches files update" do
file1 = @fixture_path.join("folder1/file1.txt") file1 = @fixture_path.join("folder1/file1.txt")
file2 = @fixture_path.join("folder1/folder2/file2.txt") file2 = @fixture_path.join("folder1/folder2/file2.txt")
File.exists?(file1).should be_true File.exists?(file1).should be_true
@ -41,6 +42,7 @@ describe Guard::Polling do
stop stop
@results.sort.should == ['spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/folder2/file2.txt'] @results.sort.should == ['spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/folder2/file2.txt']
end end
end
private private

View File

@ -3,12 +3,15 @@ require 'spec_helper'
describe Guard::Notifier do describe Guard::Notifier do
subject { Guard::Notifier } subject { Guard::Notifier }
describe "notify" do describe ".notify" do
before(:each) { ENV["GUARD_ENV"] = 'special_test' } before(:each) do
@saved_guard_env = ENV["GUARD_ENV"]
ENV["GUARD_ENV"] = 'dont_mute_notify'
end
if mac? if mac?
require 'growl' 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", Growl.should_receive(:notify).with("great",
:title => "Guard", :title => "Guard",
:icon => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s, :icon => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s,
@ -20,7 +23,7 @@ describe Guard::Notifier do
if linux? if linux?
require 'libnotify' require 'libnotify'
it "should use Libnotify on Linux" do it "uses Libnotify on Linux" do
Libnotify.should_receive(:show).with( Libnotify.should_receive(:show).with(
:body => "great", :body => "great",
:summary => 'Guard', :summary => 'Guard',
@ -30,12 +33,12 @@ describe Guard::Notifier do
end end
end end
context "turned off" do describe ".turn_off" do
before(:each) { subject.turn_off } before(:each) { subject.turn_off }
if mac? if mac?
require 'growl' require 'growl'
it "should do nothing" do it "does nothing" do
Growl.should_not_receive(:notify) Growl.should_not_receive(:notify)
subject.notify 'great', :title => 'Guard' subject.notify 'great', :title => 'Guard'
end end
@ -43,14 +46,14 @@ describe Guard::Notifier do
if linux? if linux?
require 'libnotify' require 'libnotify'
it "should do nothing" do it "does nothing" do
Libnotify.should_not_receive(:show) Libnotify.should_not_receive(:show)
subject.notify 'great', :title => 'Guard' subject.notify 'great', :title => 'Guard'
end end
end end
end end
after(:each) { ENV["GUARD_ENV"] = 'test' } after(:each) { ENV["GUARD_ENV"] = @saved_guard_env }
end end
end end

View File

@ -3,8 +3,9 @@ require 'guard/guard'
describe Guard::Watcher do describe Guard::Watcher do
describe "pattern" do describe "#initialize" do
it "should be required" do describe "pattern parameter" do
it "is required" do
expect { Guard::Watcher.new }.to raise_error(ArgumentError) expect { Guard::Watcher.new }.to raise_error(ArgumentError)
end end
@ -16,13 +17,16 @@ describe Guard::Watcher do
Guard::Watcher.new(/spec_helper\.rb/).pattern.should == /spec_helper\.rb/ Guard::Watcher.new(/spec_helper\.rb/).pattern.should == /spec_helper\.rb/
end end
describe "string looking like a regex" do describe "can be a string looking like a regex (deprecated)" do
before(:each) { Guard::UI.should_receive(:info).any_number_of_times } before(:each) { Guard::UI.should_receive(:info).any_number_of_times }
specify { Guard::Watcher.new('^spec_helper.rb').pattern.should == /^spec_helper.rb/ } it "and is automatically casted to a regex" do
specify { Guard::Watcher.new('spec_helper.rb$').pattern.should == /spec_helper.rb$/ } Guard::Watcher.new('^spec_helper.rb').pattern.should == /^spec_helper.rb/
specify { Guard::Watcher.new('spec_helper\.rb').pattern.should == /spec_helper\.rb/ } Guard::Watcher.new('spec_helper.rb$').pattern.should == /spec_helper.rb$/
specify { Guard::Watcher.new('.*_spec.rb').pattern.should == /.*_spec.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
end end

View File

@ -44,7 +44,7 @@ describe Guard do
end end
describe ".locate_guard" do 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 = Guard.locate_guard('rspec')
guard_path.should match(/^.*\/guard-rspec-.*$/) guard_path.should match(/^.*\/guard-rspec-.*$/)
guard_path.should == guard_path.chomp guard_path.should == guard_path.chomp
@ -64,15 +64,15 @@ describe Guard do
@g.stub!(:regular_with_arg).with("given_path") { "i'm a success" } @g.stub!(:regular_with_arg).with("given_path") { "i'm a success" }
end 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) lambda { subject.supervised_task(@g, :regular) }.should_not change(subject.guards, :size)
end 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) lambda { subject.supervised_task(@g, :regular_with_arg, "given_path") }.should_not change(subject.guards, :size)
end 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).should be_true
::Guard.supervised_task(@g, :regular_with_arg, "given_path").should == "i'm a success" ::Guard.supervised_task(@g, :regular_with_arg, "given_path").should == "i'm a success"
end end
@ -87,12 +87,12 @@ describe Guard do
describe "tasks that raise an exception" do describe "tasks that raise an exception" do
before(:each) { @g.stub!(:failing) { raise "I break your system" } } 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) lambda { subject.supervised_task(@g, :failing) }.should change(subject.guards, :size).by(-1)
subject.guards.should_not include(@g) subject.guards.should_not include(@g)
end end
it "should return the exception object" do it "returns the exception object" do
failing_result = ::Guard.supervised_task(@g, :failing) failing_result = ::Guard.supervised_task(@g, :failing)
failing_result.should be_kind_of(Exception) failing_result.should be_kind_of(Exception)
failing_result.message.should == 'I break your system' failing_result.message.should == 'I break your system'
@ -106,6 +106,11 @@ describe Guard do
end 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
end end