diff --git a/lib/listen/record.rb b/lib/listen/record.rb index ad374cec..25e29376 100644 --- a/lib/listen/record.rb +++ b/lib/listen/record.rb @@ -45,8 +45,7 @@ def dir_entries(rel_path) if [nil, '', '.'].include? rel_path.to_s tree else - tree[rel_path.to_s] ||= _auto_hash - tree[rel_path.to_s] + _sub_tree(rel_path) end result = {} @@ -57,6 +56,19 @@ def dir_entries(rel_path) result end + def _sub_tree(rel_path) + tree.each_with_object({}) do |(path, meta), result| + next unless path.start_with?(rel_path) + + if path == rel_path + result.merge!(meta) + else + sub_path = path.sub(%r{\A#{rel_path}/?}, '') + result[sub_path] = meta + end + end + end + def build @tree = _auto_hash # TODO: test with a file name given diff --git a/spec/acceptance/listen_spec.rb b/spec/acceptance/listen_spec.rb index ca1c5ac2..07556f23 100644 --- a/spec/acceptance/listen_spec.rb +++ b/spec/acceptance/listen_spec.rb @@ -195,6 +195,29 @@ end end + context 'listens to subdirectory removed' do + around do |example| + mkdir_p 'dir1' + mkdir_p 'dir1/subdir1' + mkdir_p 'dir1/subdir1/subdir2' + touch 'dir1/subdir1/file.rb' + touch 'dir1/subdir1/subdir2/file.rb' + example.run + end + + it 'listen to the files of a removed directory' do + expected = { + modified: [], + added: [], + removed: %w(dir1/subdir1/file.rb dir1/subdir1/subdir2/file.rb) + } + + expect(wrapper.listen do + rm_rf 'dir1' + end).to eq expected + end + end + context 'with .bundle dir ignored by default' do around do |example| mkdir_p '.bundle' diff --git a/spec/lib/listen/record_spec.rb b/spec/lib/listen/record_spec.rb index 285aa8bb..f1efbe90 100644 --- a/spec/lib/listen/record_spec.rb +++ b/spec/lib/listen/record_spec.rb @@ -217,6 +217,11 @@ def record_tree(record) before { record.update_file('path/file.rb', mtime: 1.1) } it { should eq('file.rb' => { mtime: 1.1 }) } end + + context 'with path/subdir' do + before { record.add_dir('path/subdir') } + it { should eq('subdir' => {}) } + end end end