Implement Remote::FS#file_read

This commit is contained in:
Thibault Jouan 2014-01-23 16:37:02 +00:00
parent 39b3796aa0
commit 05331d334d
2 changed files with 37 additions and 0 deletions

View File

@ -30,6 +30,12 @@ module Producer
sftp.mkdir! path
end
def file_read(path)
sftp.file.open(path) { |f| content = f.read }
rescue Net::SFTP::StatusException
nil
end
def file_write(path, content)
sftp.file.open path, 'w' do |f|
f.write content

View File

@ -124,6 +124,37 @@ module Producer::Core
end
end
describe '#file_read' do
let(:sftp) { double 'sftp' }
let(:file) { double 'file' }
let(:f) { double 'f' }
let(:path) { 'some_file_path' }
let(:content) { 'some_content' }
before do
allow(fs).to receive(:sftp) { sftp }
allow(sftp).to receive(:file) { file }
allow(file).to receive(:open).and_yield(f)
allow(f).to receive(:read) { content }
end
it 'returns the file content' do
expect(fs.file_read(path)).to eq content
end
context 'when opening the file raises a Net::SFTP::StatusException' do
before do
response = double 'response', code: '42', message: 'some message'
ex = Net::SFTP::StatusException.new(response)
allow(file).to receive(:open).and_raise(ex)
end
it 'returns nil' do
expect(fs.file_read(path)).to be nil
end
end
end
describe '#file_write' do
let(:sftp) { double 'sftp' }
let(:file) { double 'file' }