diff --git a/lib/listen/record.rb b/lib/listen/record.rb index 144bc058..4f8fedf0 100644 --- a/lib/listen/record.rb +++ b/lib/listen/record.rb @@ -45,10 +45,11 @@ def file_data(rel_path) end def dir_entries(rel_path) - subtree = if [nil, '', '.'].include? rel_path.to_s + subtree = if ['', '.'].include? rel_path.to_s @tree else - _sub_tree(rel_path) + @tree[rel_path.to_s] ||= _auto_hash + @tree[rel_path.to_s] end subtree.transform_values do |values| @@ -57,19 +58,6 @@ def dir_entries(rel_path) end 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 76a93942..7d25a560 100644 --- a/spec/acceptance/listen_spec.rb +++ b/spec/acceptance/listen_spec.rb @@ -208,6 +208,29 @@ end end + context 'listens to sub-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/subdir1' + 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 7d3b2c15..d3674ee1 100644 --- a/spec/lib/listen/record_spec.rb +++ b/spec/lib/listen/record_spec.rb @@ -208,6 +208,29 @@ def record_tree(record) end end + context 'when there is a file with the same name as a dir' do + subject { record.dir_entries('cypress') } + + before do + record.update_file('cypress.json', mtime: 1.1) + record.update_file('cypress/README.md', mtime: 1.2) + record.update_file('a/b/cypress/d', mtime: 1.3) + record.update_file('a/b/c/cypress', mtime: 1.3) + end + it { should eq('README.md' => { mtime: 1.2 }) } + end + + context 'when there is a file with a similar name to a dir' do + subject { record.dir_entries('app') } + + before do + record.update_file('appspec.yml', mtime: 1.1) + record.update_file('app/README.md', mtime: 1.2) + record.update_file('spec/app/foo', mtime: 1.3) + end + it { should eq('README.md' => { mtime: 1.2 }) } + end + context 'in subdir /path' do subject { record.dir_entries('path') } @@ -220,9 +243,17 @@ def record_tree(record) it { should eq('file.rb' => { mtime: 1.1 }) } end - context 'with path/subdir' do + context 'with empty path/subdir' do before { record.add_dir('path/subdir') } - it { should eq('subdir' => {}) } + it { should be_empty } + end + + context 'with path/subdir with file' do + before do + record.add_dir('path/subdir') + record.update_file('path/subdir/file.rb', mtime: 1.1) + end + it { should be_empty } end end end