diff --git a/lib/fsevents/stream.rb b/lib/fsevents/stream.rb index de5b136..df145ca 100644 --- a/lib/fsevents/stream.rb +++ b/lib/fsevents/stream.rb @@ -63,10 +63,13 @@ def update_last_event when :mtime @last_event = Time.now when :cache - paths.each do |path| + cache_paths = paths.dup + cache_paths.each do |path| dirs[path] = {} Dir["#{path}/*"].each do |file| - dirs[path][file] = File::Stat.new(file) + stat = File::Stat.new(file) + dirs[path][file] = stat + cache_paths.push(file) if stat.directory? end end end diff --git a/spec/stream_spec.rb b/spec/stream_spec.rb index fb7d67d..5016fcc 100644 --- a/spec/stream_spec.rb +++ b/spec/stream_spec.rb @@ -436,7 +436,7 @@ before :each do @stream.stubs(:mode).returns(:cache) @files = Array.new(5) { |i| stub("file #{i+1}") } - @stats = Array.new(5) { |i| stub("file #{i+1} stat") } + @stats = Array.new(5) { |i| stub("file #{i+1} stat", :directory? => false) } @files.zip(@stats).each do |file, stat| File::Stat.stubs(:new).with(file).returns(stat) @@ -492,6 +492,28 @@ end @stream.update_last_event end + + it 'should see if there are any subdirectories' do + @stats.each { |stat| stat.expects(:directory?) } + @stream.update_last_event + end + + it 'should cache subdirectories' do + subdir_path = '/sub/dir/path' + @files[3].stubs(:to_s).returns(subdir_path) + @stats[3].stubs(:directory?).returns(true) + Dir.expects(:[]).with("#{subdir_path}/*").returns([]) + @stream.update_last_event + end + + it 'should not add cached subdirectories to the watched paths' do + subdir_path = '/sub/dir/path' + @files[3].stubs(:to_s).returns(subdir_path) + @stats[3].stubs(:directory?).returns(true) + Dir.stubs(:[]).with("#{subdir_path}/*").returns([]) + @stream.update_last_event + @stream.paths.should_not include(@files[3]) + end end end