Skip to content

Commit

Permalink
rework Listener spec
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed Jun 12, 2015
1 parent 1539803 commit 1e054d7
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 27 deletions.
123 changes: 97 additions & 26 deletions spec/lib/listen/listener_spec.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,91 @@
include Listen

RSpec.describe Listener do
subject { described_class.new(options) }

let(:realdir1) { instance_double(Pathname, '/foo/dir1', children: []) }
let(:realdir2) { instance_double(Pathname, '/foo/dir2', children: []) }

let(:queue) { instance_double(Queue) }

let(:dir1) { instance_double(Pathname, 'dir1', realpath: realdir1) }
let(:dir2) { instance_double(Pathname, 'dir2', realpath: realdir2) }

let(:dirs) { ['dir1'] }

subject { described_class.new(*(dirs + [options]).compact) }

let(:options) { {} }
let(:registry) { instance_double(Celluloid::Registry) }

let(:supervisor) do
instance_double(Celluloid::SupervisionGroup, add: true, pool: true)
let(:record) { instance_double(Record, build: true, root: 'dir2') }
let(:silencer) { instance_double(Silencer, configure: nil) }

let(:adapter_namespace) do
class_double('Listen::Adapter').
as_stubbed_const(transfer_nested_constants: true)
end

let(:record) { instance_double(Record, terminate: true, build: true) }
let(:silencer) { instance_double(Silencer, configure: nil) }
let(:adapter) { instance_double(Adapter::Base, start: nil) }
let(:adapter_class) { class_double('Listen::Adapter::Base') }
let(:adapter) { instance_double('Listen::Adapter::Base', start: nil) }

let(:optimizer_config) { instance_double(QueueOptimizer::Config) }
let(:optimizer) { instance_double(QueueOptimizer) }

let(:processor_config) { instance_double(EventProcessor::Config) }
let(:processor) { instance_double(EventProcessor) }

let(:default_latency) { 0.1 }

before do
allow(Listen::Silencer).to receive(:new) { silencer }
allow(Silencer).to receive(:new) { silencer }

# TODO: use a configuration object to clean this up
allow(adapter_namespace).to receive(:select).
with(anything).and_return(adapter_class)

allow(adapter_class).to receive(:new).with(anything).and_return(adapter)
allow(adapter_class).to receive(:local_fs?).and_return(true)
allow(adapter).to receive(:class).and_return(adapter_class)

allow(QueueOptimizer::Config).to receive(:new).
with(adapter_class, silencer).and_return(optimizer_config)

allow(QueueOptimizer).to receive(:new).with(optimizer_config).
and_return(optimizer)

allow(Celluloid::Registry).to receive(:new) { registry }
allow(Celluloid::SupervisionGroup).to receive(:run!) { supervisor }
allow(registry).to receive(:[]).with(:adapter) { adapter }
allow(registry).to receive(:[]).with(:record) { record }
allow(EventProcessor::Config).to receive(:new).
with(anything, queue, optimizer).and_return(processor_config)

allow(EventProcessor).to receive(:new).with(processor_config).
and_return(processor)

allow(processor).to receive(:loop_for).with(default_latency)

allow(Record).to receive(:new).and_return(record)

allow(Pathname).to receive(:new).with('dir1').and_return(dir1)
allow(Pathname).to receive(:new).with('dir2').and_return(dir2)

allow(Queue).to receive(:new).and_return(queue)
allow(queue).to receive(:<<)
allow(queue).to receive(:empty?).and_return(true)

allow(Internals::ThreadPool).to receive(:add)
end

describe 'initialize' do
it { should_not be_paused }

context 'with a block' do
describe 'block' do
subject { described_class.new('lib', &(proc {})) }
subject { described_class.new('dir1', &(proc {})) }
specify { expect(subject.block).to_not be_nil }
end
end

context 'with directories' do
describe 'directories' do
subject { described_class.new('lib', 'spec') }
expected = %w(lib spec).map { |dir| Pathname.pwd + dir }
specify { expect(subject.directories).to eq expected }
subject { described_class.new('dir1', 'dir2') }
specify { expect(subject.directories).to eq([realdir1, realdir2]) }
end
end
end
Expand All @@ -58,7 +107,7 @@
context 'custom options' do
subject do
described_class.new(
'lib',
'dir1',
latency: 1.234,
wait_for_delay: 0.85,
relative: true)
Expand Down Expand Up @@ -123,7 +172,7 @@

subject.start
subject.queue(:file, :modified, event_dir, 'foo')
subject.block.call(['foo'], [], [] )
subject.block.call(['foo'], [], [])
sleep 0.25
end
end
Expand All @@ -148,16 +197,23 @@

subject.start
subject.queue(:file, :modified, event_dir, 'foo')
subject.block.call(['../foo'], [], [] )
subject.block.call(['../foo'], [], [])
end
end

context 'when watched dir is on another drive' do
let(:options) { { relative: true } }

it 'registers full path' do
event_dir = instance_double(Pathname)
foo_rel_path = instance_double(Pathname, to_s: 'd:/foo', exist?: true)
event_dir = instance_double(Pathname, 'event_dir', realpath: 'd:/foo')

foo_rel_path = instance_double(
Pathname,
'rel_path',
to_s: 'd:/foo',
exist?: true,
children: []
)

allow(event_dir).to receive(:relative_path_from).
with(Pathname.pwd).
Expand All @@ -171,7 +227,7 @@

subject.start
subject.queue(:file, :modified, event_dir, 'foo')
subject.block.call(['d:/foo'], [], [] )
subject.block.call(['d:/foo'], [], [])
end
end

Expand Down Expand Up @@ -250,8 +306,12 @@

it 'adds up to existing ignore options' do
expect(silencer).to receive(:configure).once.with(ignore: [/bar/])

subject
expect(silencer).to receive(:configure).once.with(ignore: [/bar/, /foo/])

expect(silencer).to receive(:configure).once.
with(ignore: [/bar/, /foo/])

subject.ignore(/foo/)
end
end
Expand All @@ -261,8 +321,12 @@

it 'adds up to existing ignore options' do
expect(silencer).to receive(:configure).once.with(ignore: [/bar/])

subject
expect(silencer).to receive(:configure).once.with(ignore: [/bar/, /foo/])

expect(silencer).to receive(:configure).once.
with(ignore: [/bar/, /foo/])

subject.ignore(/foo/)
end
end
Expand Down Expand Up @@ -332,7 +396,7 @@
subject.start
subject.queue(:file, :modified, dir, 'bar.txt', {})
subject.queue(:file, :added, dir, 'baz.txt', {})
subject.block.call(['foo/bar.txt'], ['foo/baz.txt'], [] )
subject.block.call(['foo/bar.txt'], ['foo/baz.txt'], [])
end
end

Expand All @@ -344,7 +408,14 @@

it 'queuing does not crash when changes come in' do
expect do
subject.send(:_queue_raw_change, :dir, realdir1, 'path', recursive: true)
# TODO: write directly to queue
subject.send(
:_queue_raw_change,
:dir,
realdir1,
'path',
recursive: true)

end.to_not raise_error
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/listen/record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def record_tree(record)

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

Expand Down

0 comments on commit 1e054d7

Please sign in to comment.