From bc740d725f521b589c89165d3ebc9e56c35dac93 Mon Sep 17 00:00:00 2001 From: Niklas Hofer Date: Fri, 13 May 2011 23:06:45 +0200 Subject: [PATCH] create shared examples all listeners should behave like --- lib/guard/listener.rb | 22 ++++- lib/guard/listeners/darwin.rb | 25 +++--- lib/guard/listeners/linux.rb | 27 ++++--- lib/guard/listeners/polling.rb | 13 +-- lib/guard/listeners/windows.rb | 26 +++--- spec/guard/listener_spec.rb | 35 ++++++++ spec/guard/listeners/darwin_spec.rb | 56 +------------ spec/guard/listeners/linux_spec.rb | 98 ++++------------------ spec/guard/listeners/polling_spec.rb | 57 +------------ spec/guard/listeners/windows_spec.rb | 65 +-------------- spec/support/listener_helper.rb | 116 +++++++++++++++++++++++++++ 11 files changed, 251 insertions(+), 289 deletions(-) create mode 100644 spec/support/listener_helper.rb diff --git a/lib/guard/listener.rb b/lib/guard/listener.rb index 51d0106..8144cff 100644 --- a/lib/guard/listener.rb +++ b/lib/guard/listener.rb @@ -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 = {}) diff --git a/lib/guard/listeners/darwin.rb b/lib/guard/listeners/darwin.rb index b82089e..bcb67ba 100644 --- a/lib/guard/listeners/darwin.rb +++ b/lib/guard/listeners/darwin.rb @@ -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 diff --git a/lib/guard/listeners/linux.rb b/lib/guard/listeners/linux.rb index c25b86b..7bae611 100644 --- a/lib/guard/listeners/linux.rb +++ b/lib/guard/listeners/linux.rb @@ -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 diff --git a/lib/guard/listeners/polling.rb b/lib/guard/listeners/polling.rb index d637de2..fa1697e 100644 --- a/lib/guard/listeners/polling.rb +++ b/lib/guard/listeners/polling.rb @@ -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 diff --git a/lib/guard/listeners/windows.rb b/lib/guard/listeners/windows.rb index f8e7fa4..acb2ce0 100644 --- a/lib/guard/listeners/windows.rb +++ b/lib/guard/listeners/windows.rb @@ -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 diff --git a/spec/guard/listener_spec.rb b/spec/guard/listener_spec.rb index 1d19f6a..d527e2b 100644 --- a/spec/guard/listener_spec.rb +++ b/spec/guard/listener_spec.rb @@ -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 diff --git a/spec/guard/listeners/darwin_spec.rb b/spec/guard/listeners/darwin_spec.rb index c6d54eb..4deca21 100644 --- a/spec/guard/listeners/darwin_spec.rb +++ b/spec/guard/listeners/darwin_spec.rb @@ -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 diff --git a/spec/guard/listeners/linux_spec.rb b/spec/guard/listeners/linux_spec.rb index a36b33c..646087e 100644 --- a/spec/guard/listeners/linux_spec.rb +++ b/spec/guard/listeners/linux_spec.rb @@ -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 diff --git a/spec/guard/listeners/polling_spec.rb b/spec/guard/listeners/polling_spec.rb index 258783c..90d389f 100644 --- a/spec/guard/listeners/polling_spec.rb +++ b/spec/guard/listeners/polling_spec.rb @@ -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 diff --git a/spec/guard/listeners/windows_spec.rb b/spec/guard/listeners/windows_spec.rb index b0d945e..133dc83 100644 --- a/spec/guard/listeners/windows_spec.rb +++ b/spec/guard/listeners/windows_spec.rb @@ -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 diff --git a/spec/support/listener_helper.rb b/spec/support/listener_helper.rb new file mode 100644 index 0000000..a68db1d --- /dev/null +++ b/spec/support/listener_helper.rb @@ -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 +