From 9d7af04d280f5b183220fcfe67af46043053710b Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Tue, 21 Jan 2014 15:01:27 +0000 Subject: [PATCH] Implement Remote::FS#dir? --- lib/producer/core/remote/fs.rb | 6 +++++ spec/producer/core/remote/fs_spec.rb | 39 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/lib/producer/core/remote/fs.rb b/lib/producer/core/remote/fs.rb index c79bd77..db3fbb1 100644 --- a/lib/producer/core/remote/fs.rb +++ b/lib/producer/core/remote/fs.rb @@ -14,6 +14,12 @@ module Producer @sftp ||= @remote.session.sftp.connect end + def dir?(path) + sftp.stat!(path).directory? + rescue Net::SFTP::StatusException + false + end + def file?(path) sftp.stat!(path).file? rescue Net::SFTP::StatusException diff --git a/spec/producer/core/remote/fs_spec.rb b/spec/producer/core/remote/fs_spec.rb index 5454d7e..40d6c50 100644 --- a/spec/producer/core/remote/fs_spec.rb +++ b/spec/producer/core/remote/fs_spec.rb @@ -31,6 +31,45 @@ module Producer::Core end end + describe '#dir?' do + let(:sftp) { double('sftp').as_null_object } + let(:path) { 'some_directory_path' } + let(:stat) { double 'stat' } + + before do + 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 + before { allow(stat).to receive(:directory?) { true } } + + it 'returns true' do + expect(fs.dir? path).to be true + end + end + + context 'when path given as argument is not a directory' do + before { allow(stat).to receive(:directory?) { false } } + + it 'returns false' do + expect(fs.dir? path).to be false + end + end + + context 'when querying the path raises a Net::SFTP::StatusException' do + before do + response = double 'response', code: '42', message: 'some message' + ex = Net::SFTP::StatusException.new(response) + allow(sftp).to receive(:stat!).with(path).and_raise(ex) + end + + it 'returns false' do + expect(fs.dir? path).to be false + end + 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 '#file?', :ssh do