Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Tracking of removed subdirectories #460

Merged
merged 3 commits into from Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/listen/record.rb
Expand Up @@ -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 = {}
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions spec/acceptance/listen_spec.rb
Expand Up @@ -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'
Expand Down
5 changes: 5 additions & 0 deletions spec/lib/listen/record_spec.rb
Expand Up @@ -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

Expand Down