From 9393b298371764d0b79d2aa56e420ab1599dab9b Mon Sep 17 00:00:00 2001 From: Alex Ghiculescu Date: Mon, 21 Dec 2020 10:57:38 -0600 Subject: [PATCH 1/3] Don't return incorrect files when there's a file whose name matches a dir --- lib/listen/record.rb | 3 ++- spec/acceptance/listen_spec.rb | 23 ++++++++++++++++++++++ spec/lib/listen/record_spec.rb | 35 ++++++++++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/lib/listen/record.rb b/lib/listen/record.rb index 144bc058..eb0e2794 100644 --- a/lib/listen/record.rb +++ b/lib/listen/record.rb @@ -48,7 +48,8 @@ def dir_entries(rel_path) subtree = if [nil, '', '.'].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| 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 From f89fa27285fc4a53837cbcf349de490f5c4b38a5 Mon Sep 17 00:00:00 2001 From: Alex Ghiculescu Date: Tue, 22 Dec 2020 10:39:23 -0600 Subject: [PATCH 2/3] no longer need this function --- lib/listen/record.rb | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lib/listen/record.rb b/lib/listen/record.rb index eb0e2794..85572cd4 100644 --- a/lib/listen/record.rb +++ b/lib/listen/record.rb @@ -58,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 From 2673d8e3d4b887b4a937fa94ce32f72a2bdc0300 Mon Sep 17 00:00:00 2001 From: Alex Ghiculescu Date: Tue, 22 Dec 2020 10:39:55 -0600 Subject: [PATCH 3/3] remove impossible case https://github.com/guard/listen/pull/526#issuecomment-749376277 --- lib/listen/record.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/listen/record.rb b/lib/listen/record.rb index 85572cd4..4f8fedf0 100644 --- a/lib/listen/record.rb +++ b/lib/listen/record.rb @@ -45,7 +45,7 @@ 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 @tree[rel_path.to_s] ||= _auto_hash