From 05331d334dfa1b60e31cce9499aac50cc6168f1d Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Thu, 23 Jan 2014 16:37:02 +0000 Subject: [PATCH] Implement Remote::FS#file_read --- lib/producer/core/remote/fs.rb | 6 ++++++ spec/producer/core/remote/fs_spec.rb | 31 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/producer/core/remote/fs.rb b/lib/producer/core/remote/fs.rb index 114412b..c01f594 100644 --- a/lib/producer/core/remote/fs.rb +++ b/lib/producer/core/remote/fs.rb @@ -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 diff --git a/spec/producer/core/remote/fs_spec.rb b/spec/producer/core/remote/fs_spec.rb index 120747c..54337d9 100644 --- a/spec/producer/core/remote/fs_spec.rb +++ b/spec/producer/core/remote/fs_spec.rb @@ -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' }