From 8897b31d7cffbd56376096279b8214608cfd81b4 Mon Sep 17 00:00:00 2001 From: Alex Ghiculescu Date: Fri, 11 Dec 2020 16:27:05 -0600 Subject: [PATCH] Don't return incorrect files when there's a file whose name matches a dir --- lib/listen/record.rb | 5 ++++- spec/lib/listen/record_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/listen/record.rb b/lib/listen/record.rb index 144bc058..f4c77edb 100644 --- a/lib/listen/record.rb +++ b/lib/listen/record.rb @@ -64,7 +64,10 @@ def _sub_tree(rel_path) if path == rel_path result.merge!(meta) else - sub_path = path.sub(%r{\A#{rel_path}/?}, '') + parts = path.split("/") + next unless parts.include?(rel_path) + + sub_path = (parts - [rel_path]).join("/") result[sub_path] = meta end end diff --git a/spec/lib/listen/record_spec.rb b/spec/lib/listen/record_spec.rb index 7d3b2c15..916a77e5 100644 --- a/spec/lib/listen/record_spec.rb +++ b/spec/lib/listen/record_spec.rb @@ -208,6 +208,26 @@ 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) + 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) + end + it { should eq('README.md' => { mtime: 1.2 }) } + end + context 'in subdir /path' do subject { record.dir_entries('path') }