Implement Remote::FS#has_file?

This commit is contained in:
Thibault Jouan 2013-09-25 23:58:07 +00:00
parent 91f2bc3567
commit 3492382968
2 changed files with 52 additions and 0 deletions

View File

@ -11,6 +11,12 @@ module Producer
def sftp
@sftp ||= @remote.session.sftp.connect
end
def has_file?(path)
sftp.stat!(path).file?
rescue Net::SFTP::StatusException
false
end
end
end
end

View File

@ -30,5 +30,51 @@ module Producer::Core
expect(fs.sftp).to be fs.sftp
end
end
# FIXME: We rely a lot on mocking net-sftp heavily, while we already use a
# part of net-ssh story helpers, which are more close to integration tests.
describe '#has_file?', :ssh do
let(:file_path) { "some_file_path" }
let(:stat) { double('stat') }
before do
sftp_story
allow(fs.sftp).to receive(:stat!) { stat }
end
context 'when path given as argument exists' do
context 'when path is a file' do
before do
allow(stat).to receive(:file?) { true }
end
it 'returns true' do
expect(fs.has_file?(file_path)).to be true
end
end
context 'when path is not a file' do
before do
allow(stat).to receive(:file?) { false }
end
it 'returns false' do
expect(fs.has_file?(file_path)).to be false
end
end
end
context 'when querying the path raises a Net::SFTP::StatusException' do
before do
response = double('response', code: '42', message: '…')
ex = Net::SFTP::StatusException.new(response)
allow(stat).to receive(:file?).and_raise(ex)
end
it 'returns false' do
expect(fs.has_file?(file_path)).to be false
end
end
end
end
end