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{^lib/(.*)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
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."
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}'"
@ -50,11 +48,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
@ -97,7 +96,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

View File

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

View File

@ -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

View File

@ -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
end

View File

@ -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
end

View File

@ -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
end

View File

@ -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
end

View File

@ -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
end

View File

@ -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

View File

@ -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
@ -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
@ -87,12 +87,12 @@ 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'
@ -106,6 +106,11 @@ describe Guard do
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

View File

@ -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
end