Skip to content

Commit

Permalink
Merge 907f3ca into 8d85b4c
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanlira committed Mar 12, 2019
2 parents 8d85b4c + 907f3ca commit 926ef84
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/listen/record.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 926ef84

Please sign in to comment.