Merge branch 'remote-fs-di'

Move SFTP session building responsability from Remote::FS class to
Remote class, to decrease coupling in Remote::FS.
This commit is contained in:
Thibault Jouan 2014-03-05 00:13:08 +00:00
commit a7f6d7034b
5 changed files with 25 additions and 72 deletions

View File

@ -20,7 +20,7 @@ module Producer
end end
def fs def fs
@fs ||= Remote::FS.new(self) @fs ||= Remote::FS.new(session.sftp.connect)
end end
def execute(command) def execute(command)

View File

@ -2,14 +2,10 @@ module Producer
module Core module Core
class Remote class Remote
class FS class FS
attr_reader :remote attr_reader :sftp
def initialize(remote) def initialize(sftp)
@remote = remote @sftp = sftp
end
def sftp
@sftp ||= @remote.session.sftp.connect
end end
def dir?(path) def dir?(path)

View File

@ -3,44 +3,21 @@ require 'spec_helper'
module Producer::Core module Producer::Core
class Remote class Remote
describe FS do describe FS do
let(:remote) { Remote.new('some_host.example') } let(:sftp_file) { double 'sftp_file' }
subject(:fs) { FS.new(remote) } let(:sftp) { double('sftp', file: sftp_file) }
subject(:fs) { FS.new(sftp) }
describe '#initialize' do describe '#initialize' do
it 'assigns the remote given as argument' do it 'assigns the sftp session' do
expect(fs.remote).to be remote expect(fs.sftp).to be sftp
end
end
describe '#sftp', :ssh do
before { sftp_story }
it 'builds a new SFTP session' do
expect(remote.session.sftp).to receive :connect
fs.sftp
end
it 'returns the new SFTP session' do
session = double 'session'
allow(remote.session.sftp).to receive(:connect) { session }
expect(fs.sftp).to be session
end
it 'memoizes the FS' do
allow(remote.session.sftp).to receive(:connect) { Object.new }
expect(fs.sftp).to be fs.sftp
end end
end end
describe '#dir?' do describe '#dir?' do
let(:sftp) { double('sftp').as_null_object }
let(:path) { 'some_directory_path' } let(:path) { 'some_directory_path' }
let(:stat) { double 'stat' } let(:stat) { double 'stat' }
before do before { allow(sftp).to receive(:stat!).with(path) { stat } }
allow(fs).to receive(:sftp) { sftp }
allow(sftp).to receive(:stat!).with(path) { stat }
end
context 'when path given as argument is a directory' do context 'when path given as argument is a directory' do
before { allow(stat).to receive(:directory?) { true } } before { allow(stat).to receive(:directory?) { true } }
@ -114,10 +91,7 @@ module Producer::Core
end end
describe '#mkdir' do describe '#mkdir' do
let(:sftp) { double 'sftp' } let(:path) { 'some_directory_path' }
let(:path) { 'some_directory_path' }
before { allow(fs).to receive(:sftp) { sftp } }
it 'creates the directory' do it 'creates the directory' do
expect(sftp).to receive(:mkdir!).with(path) expect(sftp).to receive(:mkdir!).with(path)
@ -126,16 +100,12 @@ module Producer::Core
end end
describe '#file_read' do describe '#file_read' do
let(:sftp) { double 'sftp' }
let(:file) { double 'file' }
let(:f) { double 'f' } let(:f) { double 'f' }
let(:path) { 'some_file_path' } let(:path) { 'some_file_path' }
let(:content) { 'some_content' } let(:content) { 'some_content' }
before do before do
allow(fs).to receive(:sftp) { sftp } allow(sftp_file).to receive(:open).and_yield(f)
allow(sftp).to receive(:file) { file }
allow(file).to receive(:open).and_yield(f)
allow(f).to receive(:read) { content } allow(f).to receive(:read) { content }
end end
@ -147,7 +117,7 @@ module Producer::Core
before do before do
response = double 'response', code: '42', message: 'some message' response = double 'response', code: '42', message: 'some message'
ex = Net::SFTP::StatusException.new(response) ex = Net::SFTP::StatusException.new(response)
allow(file).to receive(:open).and_raise(ex) allow(sftp_file).to receive(:open).and_raise(ex)
end end
it 'returns nil' do it 'returns nil' do
@ -157,25 +127,18 @@ module Producer::Core
end end
describe '#file_write' do describe '#file_write' do
let(:sftp) { double 'sftp' }
let(:file) { double 'file' }
let(:path) { 'some_file_path' } let(:path) { 'some_file_path' }
let(:content) { 'some_content' } let(:content) { 'some_content' }
before do
allow(fs).to receive(:sftp) { sftp }
allow(sftp).to receive(:file) { file }
end
it 'opens the file' do it 'opens the file' do
expect(file).to receive(:open).with(path, 'w') expect(sftp_file).to receive(:open).with(path, 'w')
fs.file_write path, content fs.file_write path, content
end end
it 'writes the content' do it 'writes the content' do
expect(file).to receive(:open).with(any_args) do |&b| expect(sftp_file).to receive(:open).with(any_args) do |&b|
expect(file).to receive(:write).with(content) expect(sftp_file).to receive(:write).with(content)
b.call file b.call sftp_file
end end
fs.file_write path, content fs.file_write path, content
end end

View File

@ -68,20 +68,11 @@ module Producer::Core
end end
describe '#fs' do describe '#fs' do
it 'builds a new FS' do let(:sftp_session) { double 'sftp session' }
expect(Remote::FS).to receive :new
remote.fs
end
it 'returns the new FS instance' do it 'returns an FS instance built with a new sftp session' do
fs = double 'fs' remote.stub_chain(:session, :sftp, :connect) { sftp_session }
allow(Remote::FS).to receive(:new) { fs } expect(remote.fs.sftp).to be sftp_session
expect(remote.fs).to be fs
end
it 'memoizes the FS' do
allow(Remote::FS).to receive(:new) { Object.new }
expect(remote.fs).to be remote.fs
end end
end end

View File

@ -26,6 +26,9 @@ module TestEnvHelpers
end end
def build_remote def build_remote
Producer::Core::Testing::MockRemote.new('some_host.test') fs = RSpec::Mocks::Mock.new('remote fs', __declared_as: 'Double')
remote = Producer::Core::Testing::MockRemote.new('some_host.test')
remote.define_singleton_method(:fs) { fs }
remote
end end
end end