create shared examples all listeners should behave like
This commit is contained in:
parent
9c44f89a61
commit
bc740d725f
@ -24,11 +24,22 @@ module Guard
|
||||
end
|
||||
|
||||
def initialize(directory=Dir.pwd)
|
||||
@directory = directory
|
||||
@directory = directory.to_s
|
||||
@sha1_checksums_hash = {}
|
||||
update_last_event
|
||||
end
|
||||
|
||||
def start
|
||||
watch directory
|
||||
end
|
||||
|
||||
def stop
|
||||
end
|
||||
|
||||
def on_change(&callback)
|
||||
@callback = callback
|
||||
end
|
||||
|
||||
def update_last_event
|
||||
@last_event = Time.now
|
||||
end
|
||||
@ -38,6 +49,15 @@ module Guard
|
||||
files.map! { |file| file.gsub("#{directory}/", '') }
|
||||
end
|
||||
|
||||
def worker
|
||||
raise NotImplementedError, "should respond to #watch"
|
||||
end
|
||||
|
||||
# register a directory to watch. must be implemented by the subclasses
|
||||
def watch(directory)
|
||||
raise NotImplementedError, "do whatever you want here, given the directory as only argument"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def potentially_modified_files(dirs, options = {})
|
||||
|
@ -2,25 +2,23 @@ module Guard
|
||||
class Darwin < Listener
|
||||
attr_reader :fsevent
|
||||
|
||||
def initialize
|
||||
def initialize(*)
|
||||
super
|
||||
@fsevent = FSEvent.new
|
||||
end
|
||||
|
||||
def on_change(&callback)
|
||||
@fsevent.watch directory do |modified_dirs|
|
||||
files = modified_files(modified_dirs)
|
||||
update_last_event
|
||||
callback.call(files)
|
||||
end
|
||||
def worker
|
||||
@fsevent
|
||||
end
|
||||
|
||||
def start
|
||||
@fsevent.run
|
||||
super
|
||||
fsevent.run
|
||||
end
|
||||
|
||||
def stop
|
||||
@fsevent.stop
|
||||
super
|
||||
fsevent.stop
|
||||
end
|
||||
|
||||
def self.usable?
|
||||
@ -36,5 +34,14 @@ module Guard
|
||||
false
|
||||
end
|
||||
|
||||
private
|
||||
def watch(directory)
|
||||
worker.watch directory do |modified_dirs|
|
||||
files = modified_files(modified_dirs)
|
||||
update_last_event
|
||||
callback.call(files)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -2,7 +2,7 @@ module Guard
|
||||
class Linux < Listener
|
||||
attr_reader :inotify, :files, :latency, :callback
|
||||
|
||||
def initialize
|
||||
def initialize(*)
|
||||
super
|
||||
|
||||
@inotify = INotify::Notifier.new
|
||||
@ -12,24 +12,16 @@ module Guard
|
||||
|
||||
def start
|
||||
@stop = false
|
||||
super
|
||||
watch_change unless watch_change?
|
||||
end
|
||||
|
||||
def stop
|
||||
super
|
||||
@stop = true
|
||||
sleep latency
|
||||
end
|
||||
|
||||
def on_change(&callback)
|
||||
@callback = callback
|
||||
inotify.watch(directory, :recursive, :modify, :create, :delete, :move) do |event|
|
||||
unless event.name == "" # Event on root directory
|
||||
@files << event.absolute_name
|
||||
end
|
||||
end
|
||||
rescue Interrupt
|
||||
end
|
||||
|
||||
def self.usable?
|
||||
require 'rb-inotify'
|
||||
if !defined?(INotify::VERSION) || Gem::Version.new(INotify::VERSION.join('.')) < Gem::Version.new('0.5.1')
|
||||
@ -49,6 +41,19 @@ module Guard
|
||||
|
||||
private
|
||||
|
||||
def worker
|
||||
@inotify
|
||||
end
|
||||
|
||||
def watch(directory)
|
||||
worker.watch(directory, :recursive, :modify, :create, :delete, :move) do |event|
|
||||
unless event.name == "" # Event on root directory
|
||||
@files << event.absolute_name
|
||||
end
|
||||
end
|
||||
rescue Interrupt
|
||||
end
|
||||
|
||||
def watch_change
|
||||
@watch_change = true
|
||||
until @stop
|
||||
|
@ -2,21 +2,19 @@ module Guard
|
||||
class Polling < Listener
|
||||
attr_reader :callback, :latency
|
||||
|
||||
def initialize
|
||||
def initialize(*)
|
||||
super
|
||||
@latency = 1.5
|
||||
end
|
||||
|
||||
def on_change(&callback)
|
||||
@callback = callback
|
||||
end
|
||||
|
||||
def start
|
||||
@stop = false
|
||||
super
|
||||
watch_change
|
||||
end
|
||||
|
||||
def stop
|
||||
super
|
||||
@stop = true
|
||||
end
|
||||
|
||||
@ -33,5 +31,10 @@ module Guard
|
||||
end
|
||||
end
|
||||
|
||||
# we have no real worker here
|
||||
# FIXME: cannot watch muliple directories, but is not needed in guard (yet?)
|
||||
def watch(directory)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -7,20 +7,13 @@ module Guard
|
||||
@fchange = FChange::Notifier.new
|
||||
end
|
||||
|
||||
def on_change(&callback)
|
||||
@fchange.watch(directory, :all_events, :recursive) do |event|
|
||||
paths = [File.expand_path(event.watcher.path) + '/']
|
||||
files = modified_files(paths, {:all => true})
|
||||
update_last_event
|
||||
callback.call(files)
|
||||
end
|
||||
end
|
||||
|
||||
def start
|
||||
super
|
||||
@fchange.run
|
||||
end
|
||||
|
||||
def stop
|
||||
super
|
||||
@fchange.stop
|
||||
end
|
||||
|
||||
@ -32,5 +25,20 @@ module Guard
|
||||
false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def worker
|
||||
@fchange
|
||||
end
|
||||
|
||||
def watch(directory)
|
||||
worker.watch(directory, :all_events, :recursive) do |event|
|
||||
paths = [File.expand_path(event.watcher.path) + '/']
|
||||
files = modified_files(paths, {:all => true})
|
||||
update_last_event
|
||||
callback.call(files)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -91,4 +91,39 @@ describe Guard::Listener do
|
||||
end
|
||||
end
|
||||
|
||||
describe "working directory" do
|
||||
|
||||
context "unspecified" do
|
||||
subject { described_class.new }
|
||||
it "defaults to Dir.pwd" do
|
||||
subject.directory.should == Dir.pwd
|
||||
end
|
||||
it "can be not changed" do
|
||||
subject.should_not respond_to(:directory=)
|
||||
end
|
||||
end
|
||||
|
||||
context "specified as first argument to ::new" do
|
||||
before :each do
|
||||
@wd = @fixture_path.join("folder1")
|
||||
end
|
||||
subject { described_class.new @wd }
|
||||
it "can be inspected" do
|
||||
subject.directory.should == @wd.to_s
|
||||
end
|
||||
it "can be not changed" do
|
||||
subject.should_not respond_to(:directory=)
|
||||
end
|
||||
|
||||
it "will be used to watch" do
|
||||
subject.should_receive(:watch).with(@wd.to_s)
|
||||
@listener = subject # indeed.
|
||||
start
|
||||
stop
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
@ -15,61 +15,9 @@ describe Guard::Darwin do
|
||||
subject.should be_usable
|
||||
end
|
||||
|
||||
describe "#on_change" do
|
||||
before(:each) do
|
||||
@results = []
|
||||
@listener = Guard::Darwin.new
|
||||
@listener.on_change do |files|
|
||||
@results += files
|
||||
end
|
||||
end
|
||||
it_should_behave_like "a listener that reacts to #on_change"
|
||||
it_should_behave_like "a listener scoped to a specific directory"
|
||||
|
||||
it "catches a 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 "catches a single 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 multiple file updates" 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.should == ['spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/folder2/file2.txt']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def start
|
||||
sleep 1
|
||||
@listener.update_last_event
|
||||
Thread.new { @listener.start }
|
||||
sleep 1
|
||||
end
|
||||
|
||||
def stop
|
||||
sleep 1
|
||||
@listener.stop
|
||||
sleep 1
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -38,94 +38,26 @@ describe Guard::Linux do
|
||||
@listener.should_not be_watch_change
|
||||
end
|
||||
|
||||
it "should use Inotify as worker" do
|
||||
@listener.__send__(:worker).should be_a(INotify::Notifier)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#on_change" do
|
||||
before(:each) do
|
||||
@results = []
|
||||
@listener = Guard::Linux.new
|
||||
@listener.on_change do |files|
|
||||
@results += files
|
||||
end
|
||||
end
|
||||
it_should_behave_like "a listener that reacts to #on_change"
|
||||
it_should_behave_like "a listener scoped to a specific directory"
|
||||
|
||||
it "catches a 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 "catches a single file update" do
|
||||
file = @fixture_path.join("folder1/file1.txt")
|
||||
File.exists?(file).should be_true
|
||||
start
|
||||
File.open(file, 'w') {|f| f.write('') }
|
||||
stop
|
||||
@results.should == ['spec/fixtures/folder1/file1.txt']
|
||||
end
|
||||
|
||||
it "catches multiple file updates" 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
|
||||
File.open(file1, 'w') {|f| f.write('') }
|
||||
File.open(file2, 'w') {|f| f.write('') }
|
||||
stop
|
||||
@results.should == ['spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/folder2/file2.txt']
|
||||
end
|
||||
|
||||
it "catches a deleted file" do
|
||||
file = @fixture_path.join("folder1/file1.txt")
|
||||
File.exists?(file).should be_true
|
||||
start
|
||||
File.delete file
|
||||
stop
|
||||
FileUtils.touch file
|
||||
@results.should == ['spec/fixtures/folder1/file1.txt']
|
||||
end
|
||||
|
||||
it "catches a moved file" do
|
||||
file1 = @fixture_path.join("folder1/file1.txt")
|
||||
file2 = @fixture_path.join("folder1/movedfile1.txt")
|
||||
File.exists?(file1).should be_true
|
||||
File.exists?(file2).should be_false
|
||||
start
|
||||
FileUtils.mv file1, file2
|
||||
stop
|
||||
FileUtils.mv file2, file1
|
||||
@results.should == ['spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/movedfile1.txt']
|
||||
end
|
||||
|
||||
it "doesn't process a change when it is stopped" do
|
||||
file = @fixture_path.join("folder1/file1.txt")
|
||||
File.exists?(file).should be_true
|
||||
start
|
||||
@listener.inotify.should_not_receive(:process)
|
||||
stop
|
||||
File.open(file, 'w') {|f| f.write('') }
|
||||
end
|
||||
it "doesn't process a change when it is stopped" do
|
||||
@listener = described_class.new
|
||||
record_results
|
||||
file = @fixture_path.join("folder1/file1.txt")
|
||||
File.exists?(file).should be_true
|
||||
start
|
||||
@listener.inotify.should_not_receive(:process)
|
||||
stop
|
||||
File.open(file, 'w') {|f| f.write('') }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def start
|
||||
sleep 1
|
||||
@listener.update_last_event
|
||||
Thread.new { @listener.start }
|
||||
sleep 1
|
||||
end
|
||||
|
||||
def stop
|
||||
sleep 1
|
||||
@listener.stop
|
||||
sleep 1
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -3,60 +3,9 @@ require 'guard/listeners/polling'
|
||||
|
||||
describe Guard::Polling do
|
||||
|
||||
before(:each) do
|
||||
@results = []
|
||||
@listener = Guard::Polling.new
|
||||
@listener.on_change do |files|
|
||||
@results += files
|
||||
end
|
||||
end
|
||||
subject { Guard::Polling }
|
||||
|
||||
describe "#on_change" do
|
||||
it "catches a 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 "catches a single 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 multiple file updates" 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.should =~ ['spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/folder2/file2.txt']
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def start
|
||||
sleep 1
|
||||
@listener.update_last_event
|
||||
Thread.new { @listener.start }
|
||||
sleep 1
|
||||
end
|
||||
|
||||
def stop
|
||||
sleep 1
|
||||
@listener.stop
|
||||
sleep 1
|
||||
end
|
||||
it_should_behave_like "a listener that reacts to #on_change"
|
||||
it_should_behave_like "a listener scoped to a specific directory"
|
||||
|
||||
end
|
||||
|
@ -21,70 +21,9 @@ describe Guard::Windows do
|
||||
subject.should be_usable
|
||||
end
|
||||
|
||||
describe "#on_change" do
|
||||
before(:each) do
|
||||
@results = []
|
||||
@listener = Guard::Windows.new
|
||||
@listener.on_change do |files|
|
||||
@results += files
|
||||
end
|
||||
end
|
||||
it_should_behave_like "a listener that reacts to #on_change"
|
||||
it_should_behave_like "a listener scoped to a specific directory"
|
||||
|
||||
it "catches a new file" do
|
||||
file = @fixture_path.join("newfile.rb")
|
||||
if File.exists?(file)
|
||||
begin
|
||||
File.delete file
|
||||
rescue
|
||||
end
|
||||
end
|
||||
File.exists?(file).should be_false
|
||||
start
|
||||
FileUtils.touch file
|
||||
stop
|
||||
begin
|
||||
File.delete file
|
||||
rescue
|
||||
end
|
||||
@results.should == ['spec/fixtures/newfile.rb']
|
||||
end
|
||||
|
||||
it "catches a single 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 multiple file updates" 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.should == ['spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/folder2/file2.txt']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def start
|
||||
sleep 1
|
||||
@listener.update_last_event
|
||||
Thread.new { @listener.start }
|
||||
sleep 1
|
||||
end
|
||||
|
||||
def stop
|
||||
sleep 1
|
||||
@listener.stop
|
||||
sleep 1
|
||||
end
|
||||
|
||||
end
|
||||
|
116
spec/support/listener_helper.rb
Normal file
116
spec/support/listener_helper.rb
Normal file
@ -0,0 +1,116 @@
|
||||
private
|
||||
|
||||
def start
|
||||
sleep 1
|
||||
@listener.update_last_event
|
||||
Thread.new { @listener.start }
|
||||
sleep 1
|
||||
end
|
||||
|
||||
def record_results
|
||||
@results = []
|
||||
@listener.on_change do |files|
|
||||
@results += files
|
||||
end
|
||||
end
|
||||
|
||||
def stop
|
||||
sleep 1
|
||||
@listener.stop
|
||||
sleep 1
|
||||
end
|
||||
|
||||
def results
|
||||
@results.flatten
|
||||
end
|
||||
|
||||
shared_examples_for 'a listener that reacts to #on_change' do
|
||||
before(:each) do
|
||||
@listener = described_class.new
|
||||
record_results
|
||||
end
|
||||
|
||||
it "catches a new file" do
|
||||
file = @fixture_path.join("newfile.rb")
|
||||
if File.exists?(file)
|
||||
begin
|
||||
File.delete file
|
||||
rescue
|
||||
end
|
||||
end
|
||||
File.exists?(file).should be_false
|
||||
start
|
||||
FileUtils.touch file
|
||||
stop
|
||||
begin
|
||||
File.delete file
|
||||
rescue
|
||||
end
|
||||
results.should =~ ['spec/fixtures/newfile.rb']
|
||||
end
|
||||
|
||||
it "catches a single 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 multiple file updates" 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.should =~ ['spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/folder2/file2.txt']
|
||||
end
|
||||
|
||||
it "catches a deleted file" do
|
||||
file = @fixture_path.join("folder1/file1.txt")
|
||||
File.exists?(file).should be_true
|
||||
start
|
||||
File.delete file
|
||||
stop
|
||||
FileUtils.touch file
|
||||
results.should =~ ['spec/fixtures/folder1/file1.txt']
|
||||
end
|
||||
|
||||
it "catches a moved file" do
|
||||
file1 = @fixture_path.join("folder1/file1.txt")
|
||||
file2 = @fixture_path.join("folder1/movedfile1.txt")
|
||||
File.exists?(file1).should be_true
|
||||
File.exists?(file2).should be_false
|
||||
start
|
||||
FileUtils.mv file1, file2
|
||||
stop
|
||||
FileUtils.mv file2, file1
|
||||
results.should =~ ['spec/fixtures/folder1/file1.txt', 'spec/fixtures/folder1/movedfile1.txt']
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
shared_examples_for "a listener scoped to a specific directory" do
|
||||
before :each do
|
||||
@wd = @fixture_path.join("folder1")
|
||||
@listener = described_class.new @wd
|
||||
end
|
||||
it "should base paths within this directory" do
|
||||
record_results
|
||||
new_file = @wd.join("folder2/newfile.rb")
|
||||
modified = @wd.join("file1.txt")
|
||||
File.exists?(modified).should be_true
|
||||
File.exists?(new_file).should be_false
|
||||
start
|
||||
FileUtils.touch new_file
|
||||
File.open(modified, 'w') {|f| f.write('') }
|
||||
stop
|
||||
File.delete new_file
|
||||
results.should =~ ["folder2/newfile.rb", 'file1.txt']
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user