Skip to content

Commit

Permalink
use JRuby workarounds for stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed May 3, 2016
1 parent c3a8cbb commit b09a3a7
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions spec/lib/listen/directory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ def fake_dir_stat(name, options = {})
instance_double(::File::Stat, name, defaults.merge(options))
end

def fake_children(ex, dir, *args, &block)
if block_given?
ex.send(:allow, dir).to receive(:children, &block)
else
ex.send(:allow, dir).to receive(:children).and_return(*args)
end
ex.send(:allow, dir).to receive(:exist?).and_return(true)
ex.send(:allow, dir).to receive(:directory?).and_return(true)
end

let(:dir) { double(:dir) }
let(:file) { fake_path('file.rb') }
let(:file2) { fake_path('file2.rb') }
Expand Down Expand Up @@ -53,7 +63,7 @@ def fake_dir_stat(name, options = {})
end

context 'with empty dir' do
before { allow(dir).to receive(:children) { [] } }
before { fake_children(self, dir, []) }

it 'sets record dir path' do
expect(record).to receive(:add_dir).with('.')
Expand All @@ -71,9 +81,8 @@ def fake_dir_stat(name, options = {})
end

context 'when subdir is removed' do
before do
allow(dir).to receive(:children) { [file] }

before do
fake_children(self, dir, [file])
allow(::File).to receive(:lstat).with('file.rb').
and_return(fake_file_stat('file.rb'))
end
Expand All @@ -88,7 +97,7 @@ def fake_dir_stat(name, options = {})

context 'when file.rb removed' do
before do
allow(dir).to receive(:children) { [subdir] }
fake_children(self, dir, [subdir])

allow(::File).to receive(:lstat).with('subdir').
and_return(fake_dir_stat('subdir'))
Expand All @@ -102,7 +111,7 @@ def fake_dir_stat(name, options = {})

context 'when file.rb no longer exists after scan' do
before do
allow(dir).to receive(:children).and_return([file], [file2])
fake_children(self, dir, [file], [file2])

allow(::File).to receive(:lstat).with('file.rb').
and_raise(Errno::ENOENT)
Expand All @@ -119,7 +128,7 @@ def fake_dir_stat(name, options = {})

context 'when file2.rb is added' do
before do
allow(dir).to receive(:children) { [file, file2, subdir] }
fake_children(self, dir, [file, file2, subdir])

allow(::File).to receive(:lstat).with('file.rb').
and_return(fake_file_stat('file.rb'))
Expand All @@ -142,7 +151,7 @@ def fake_dir_stat(name, options = {})
let(:record_entries) { {} }

context 'with non-existing dir path' do
before { allow(dir).to receive(:children) { fail Errno::ENOENT } }
before { fake_children(self, dir) { fail Errno::ENOENT } }

it 'reports no changes' do
expect(snapshot).to_not receive(:invalidate)
Expand All @@ -156,7 +165,7 @@ def fake_dir_stat(name, options = {})
end

context 'when network share is disconnected' do
before { allow(dir).to receive(:children) { fail Errno::EHOSTDOWN } }
before { fake_children(self, dir) { fail Errno::EHOSTDOWN } }

it 'reports no changes' do
expect(snapshot).to_not receive(:invalidate)
Expand All @@ -171,7 +180,7 @@ def fake_dir_stat(name, options = {})

context 'with file.rb in dir' do
before do
allow(dir).to receive(:children) { [file] }
fake_children(self, dir, [file])

allow(::File).to receive(:lstat).with('file.rb').
and_return(fake_file_stat('file.rb'))
Expand Down Expand Up @@ -202,9 +211,7 @@ def fake_dir_stat(name, options = {})
end

context 'with empty dir' do
before do
allow(dir).to receive(:children) { [] }
end
before { fake_children(self, dir, []) }

it 'snapshots changes for file & subdir path' do
expect(snapshot).to receive(:invalidate).with(:file, 'file.rb', {})
Expand All @@ -220,7 +227,7 @@ def fake_dir_stat(name, options = {})
let(:subdir2) { fake_path('subdir2', children: []) }

before do
allow(dir).to receive(:children) { [subdir2] }
fake_children(self, dir, [subdir2])
allow(subdir2).to receive(:relative_path_from).with(dir) { 'subdir2' }

allow(::File).to receive(:lstat).with('subdir2').
Expand All @@ -246,7 +253,7 @@ def fake_dir_stat(name, options = {})

context 'with non-existing dir' do
before do
allow(dir).to receive(:children) { fail Errno::ENOENT }
fake_children(self, dir) { fail Errno::ENOENT }
end

it 'reports no changes' do
Expand All @@ -257,8 +264,8 @@ def fake_dir_stat(name, options = {})

context 'with subdir present in dir' do
before do
allow(dir).to receive(:children) { [subdir] }
allow(subdir).to receive(:children) { [] }
fake_children(self, dir, [subdir])
fake_children(self, subdir, [])
allow(::File).to receive(:lstat).with('subdir').
and_return(fake_dir_stat('subdir'))
end
Expand Down

0 comments on commit b09a3a7

Please sign in to comment.