From 25329734d1faf75a887b068401511bff27ea97a7 Mon Sep 17 00:00:00 2001 From: Yossef Mendelssohn Date: Tue, 1 Jul 2008 22:30:14 -0500 Subject: [PATCH] FSEvents::Event#deleted_files -- returns files deleted for this event. This only works if the stream is in cache mode. Trying this with mtime mode raises an exception because there isn't enough information to get a list of deleted files. --- lib/fsevents/event.rb | 11 ++++++ spec/event_spec.rb | 82 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/lib/fsevents/event.rb b/lib/fsevents/event.rb index c640ac7..2878f21 100644 --- a/lib/fsevents/event.rb +++ b/lib/fsevents/event.rb @@ -29,5 +29,16 @@ def modified_files end end end + + def deleted_files + case stream.mode + when :mtime + raise RuntimeError, 'This mode does not support getting deleted files' + when :cache + cache = stream.dirs[path] || {} + + cache.keys - files + end + end end end diff --git a/spec/event_spec.rb b/spec/event_spec.rb index 6376355..968ef59 100644 --- a/spec/event_spec.rb +++ b/spec/event_spec.rb @@ -204,4 +204,86 @@ end end end + + it 'should list deleted files' do + @event.should respond_to(:deleted_files) + end + + describe 'listing deleted files' do + it 'should check the stream mode' do + @stream.expects(:mode) + @event.deleted_files + end + + describe 'when the stream mode is mtime' do + before :each do + @stream.stubs(:mode).returns(:mtime) + end + + it 'should error' do + lambda { @event.deleted_files }.should raise_error(RuntimeError) + end + end + + describe 'when the stream mode is cache' do + before :each do + @stream.stubs(:mode).returns(:cache) + end + + before :each do + @now = Time.now + @files = Array.new(5) { |i| stub("file #{i+1}") } + @event.stubs(:files).returns(@files) + + @dir_cache = { @path => {} } + @files.each_with_index do |file, i| + size = 50 * (i + 1) + mtime = @now + i - 2 + stat = stub("file #{i+1} stat", :mtime => mtime, :size => size) + @dir_cache[@path][file] = stat + end + @stream.stubs(:dirs).returns(@dir_cache) + end + + it 'should get the file list' do + @event.expects(:files).returns(@files) + @event.deleted_files + end + + it 'should get the stream dir cache' do + @stream.expects(:dirs).returns(@dir_cache) + @event.deleted_files + end + + it 'should get the dir cache for the event path' do + sub_cache = @dir_cache[@path] + @dir_cache.expects(:[]).with(@path).returns(sub_cache) + @event.deleted_files + end + + it 'should return files from the cache that are missing from the file list' do + expected_files = Array.new(2) { |i| stub("new file #{i+1}") } + expected_files.each { |file| @dir_cache[@path][file] = stub('stat') } + deleted_files = @event.deleted_files + + expected_files.each do |file| + deleted_files.should include(file) + end + end + + it 'should not return files from that cache that are present in the file list' do + unexpected_files = @files + deleted_files = @event.deleted_files + + unexpected_files.each do |file| + deleted_files.should_not include(file) + end + end + + it 'should handle this path not yet cached' do + @dir_cache.delete(@path) + @event.deleted_files.should == [] + end + end + end end