Skip to content

Commit

Permalink
update tests and + fix Record build logic
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed May 6, 2015
1 parent 0b966eb commit 8768e19
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 44 deletions.
2 changes: 0 additions & 2 deletions lib/listen/directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ def self.scan(queue, sync_record, dir, rel_path, options)

previous = sync_record.dir_entries(dir, rel_path)

record.add_dir(dir, rel_path)

# TODO: use children(with_directory: false)
path = dir + rel_path
current = Set.new(path.children)
Expand Down
21 changes: 14 additions & 7 deletions lib/listen/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ def initialize(listener)
@paths = _auto_hash
end

def add_dir(dir, rel_path)
rel_path = '.' if [nil, '', '.'].include? rel_path
@paths[dir.to_s][rel_path] ||= {}
end

def update_file(dir, rel_path, data)
dirname, basename = Pathname(rel_path).split.map(&:to_s)
_fast_update_file(dir, dirname, basename, data)
Expand All @@ -45,7 +40,7 @@ def dir_entries(dir, rel_path)

result = {}
tree.each do |key, values|
# only get data for file entries
# only get data for file/dir entries
result[key] = values.key?(:mtime) ? values : {}
end
result
Expand All @@ -68,6 +63,18 @@ def build

private

# TODO: refactor/refactor out
def add_dir(dir, rel_path, children)
rel_path = '.' if [nil, '', '.'].include? rel_path
dirname, basename = Pathname(rel_path).split.map(&:to_s)
basename = '.' if [nil, '', '.'].include? basename
if [nil, '', '.'].include?(dirname) && basename == '.'
@paths[dir.to_s]['.'] = Hash[children.map { |child| [child, {} ] } ]
else
@paths[dir.to_s][rel_path] ||= {}
end
end

def _auto_hash
Hash.new { |h, k| h[k] = Hash.new }
end
Expand Down Expand Up @@ -104,7 +111,7 @@ def _fast_build_dir(remaining, symlink_detector)
children = entry.children # NOTE: children() implicitly tests if dir
symlink_detector.verify_unwatched!(entry)
children.each { |child| remaining << child }
add_dir(entry.root, entry.record_dir_key)
add_dir(entry.root, entry.record_dir_key, children.map(&:name))
rescue Errno::ENOTDIR
_fast_try_file(entry)
rescue SystemCallError, SymlinkDetector::Error
Expand Down
7 changes: 1 addition & 6 deletions spec/lib/listen/directory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
let(:queue) { instance_double(Change, change: nil) }

let(:async_record) do
instance_double(Record, add_dir: true, unset_path: true)
instance_double(Record, unset_path: true)
end

let(:record) do
Expand Down Expand Up @@ -37,11 +37,6 @@
context 'with empty dir' do
before { allow(dir).to receive(:children) { [] } }

it 'sets record dir path' do
expect(async_record).to receive(:add_dir).with(dir, '.')
described_class.scan(queue, record, dir, '.', options)
end

it "queues changes for file path and dir that doesn't exist" do
expect(queue).to receive(:change).with(:file, dir, 'file.rb')

Expand Down
93 changes: 64 additions & 29 deletions spec/lib/listen/record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ def symlink(hash_or_dir)
context 'with path in watched dir' do
it 'sets path by spliting dirname and basename' do
record.update_file(dir, 'file.rb', mtime: 1.1)
expect(record.paths['/dir']).to eq('file.rb' => { mtime: 1.1 })
expect(record.paths['/dir']).to eq(
'.' => { 'file.rb' => { mtime: 1.1 } }
)
end

it 'sets path and keeps old data not overwritten' do
record.update_file(dir, 'file.rb', foo: 1, bar: 2)
record.update_file(dir, 'file.rb', foo: 3)
watched_dir = record.paths['/dir']
expect(watched_dir).to eq('file.rb' => { foo: 3, bar: 2 })
expect(watched_dir).to eq('.' => { 'file.rb' => { foo: 3, bar: 2 } })
end
end

Expand All @@ -78,36 +80,45 @@ def symlink(hash_or_dir)
end
end

# TODO: refactor/refactor out
describe '#add_dir' do
it 'sets itself when .' do
record.add_dir(dir, '.')
expect(record.paths['/dir']).to eq({})
record.send(:add_dir, dir, '.', [])
expect(record.paths['/dir']).to eq('.' => {})
end

it 'sets children' do
record.send(:add_dir, dir, '.', ['foo', 'bar'])
expect(record.paths['/dir']).to eq('.' => {'foo' => {}, 'bar' => {}})
end

it 'sets itself when nil' do
record.add_dir(dir, nil)
expect(record.paths['/dir']).to eq({})
record.send(:add_dir, dir, nil, [])
expect(record.paths['/dir']).to eq('.' => {})
end

it 'sets itself when empty' do
record.add_dir(dir, '')
expect(record.paths['/dir']).to eq({})
record.send(:add_dir, dir, '', [])
expect(record.paths['/dir']).to eq('.' => {})
end

it 'correctly sets new directory data' do
record.add_dir(dir, 'path/subdir')
expect(record.paths['/dir']).to eq('path/subdir' => {})
record.send(:add_dir, dir, '.', %w(subdir))
record.send(:add_dir, dir, 'path/subdir', [])
expect(record.paths['/dir']).to eq('.' => {'subdir' => {}}, 'path/subdir' => {})
end

it 'sets path and keeps old data not overwritten' do
record.add_dir(dir, 'path/subdir')
record.send(:add_dir, dir, '.', %w(path))
record.send(:add_dir, dir, 'path', %w(subdir))
record.send(:add_dir, dir, 'path/subdir', [])
record.update_file(dir, 'path/subdir/file.rb', mtime: 1.1)
record.add_dir(dir, 'path/subdir')
record.send(:add_dir, dir, 'path/subdir', [])
record.update_file(dir, 'path/subdir/file2.rb', mtime: 1.2)
record.add_dir(dir, 'path/subdir')
record.send(:add_dir, dir, 'path/subdir', [])

watched = record.paths['/dir']
expect(watched.keys).to eq ['path/subdir']
expect(watched.keys).to eq ['.', 'path', 'path/subdir']
expect(watched['path/subdir'].keys).to eq %w(file.rb file2.rb)

subdir = watched['path/subdir']
Expand All @@ -123,7 +134,7 @@ def symlink(hash_or_dir)

it 'unsets path' do
record.unset_path(dir, 'file.rb')
expect(record.paths).to eq('/dir' => {})
expect(record.paths).to eq('/dir' => { '.' => {}})
end
end

Expand Down Expand Up @@ -203,8 +214,28 @@ def symlink(hash_or_dir)
end

context 'with subdir/file.rb in record' do
before { record.update_file(dir, 'subdir/file.rb', mtime: 1.1) }
it { should eq('subdir' => {}) }
before do
record.update_file(dir, 'subdir', mtime: 1.0)
record.update_file(dir, 'subdir/file.rb', mtime: 1.1)
end

it do
should eq('subdir' => { mtime: 1.0 })
end
end

context 'with tree and root files in record' do
before do
record.update_file(dir, 'file1.rb', mtime: 1.1)
record.update_file(dir, 'subdir', mtime: 1.2)
record.update_file(dir, 'subdir/file2.rb', mtime: 1.4)
end
it do
should eq(
'file1.rb' => { mtime: 1.1 },
'subdir' => { mtime: 1.2 }
)
end
end
end

Expand Down Expand Up @@ -251,7 +282,7 @@ def symlink(hash_or_dir)

record.update_file(dir, 'path/file.rb', mtime: 1.1)
record.build
expect(record.paths).to eq('/dir1' => {}, '/dir2' => {})
expect(record.paths).to eq('/dir1' => {'.' => {}}, '/dir2' => {'.' => {}})
expect(record.file_data(dir, 'path/file.rb')).to be_empty
end

Expand All @@ -271,8 +302,10 @@ def symlink(hash_or_dir)
expect(record.paths.keys).to eq %w( /dir1 /dir2 )
expect(record.paths['/dir1']).
to eq(
'foo' => { mtime: 1.0, mode: 0644 },
'bar' => { mtime: 2.3, mode: 0755 })
'.' => {
'foo' => { mtime: 1.0, mode: 0644 },
'bar' => { mtime: 2.3, mode: 0755 }
})
end
end

Expand All @@ -287,9 +320,11 @@ def symlink(hash_or_dir)
it 'builds record' do
record.build
expect(record.paths.keys).to eq %w( /dir1 /dir2 )
expect(record.paths['/dir1']).
to eq('foo' => { 'bar' => { mtime: 2.3, mode: 0755 } })
expect(record.paths['/dir2']).to eq({})
expect(record.paths['/dir1']).to eq(
'.' => { 'foo' => {} },
'foo' => { 'bar' => { mtime: 2.3, mode: 0755 } }
)
expect(record.paths['/dir2']).to eq('.' => {})
end
end

Expand All @@ -307,13 +342,13 @@ def symlink(hash_or_dir)
it 'builds record' do
record.build
expect(record.paths.keys).to eq %w( /dir1 /dir2 )
expect(record.paths['/dir1']).
to eq(
'foo' => {},
'foo/bar' => {},
'foo/baz' => {}
expect(record.paths['/dir1']).to eq(
'.' => { 'foo' => {}},
'foo' => {},
'foo/bar' => {},
'foo/baz' => {}
)
expect(record.paths['/dir2']).to eq({})
expect(record.paths['/dir2']).to eq('.' => {})
end
end

Expand Down

0 comments on commit 8768e19

Please sign in to comment.