From 3492382968233ad629f92e95f0e721baed34a84b Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Wed, 25 Sep 2013 23:58:07 +0000 Subject: [PATCH] Implement Remote::FS#has_file? --- lib/producer/core/remote/fs.rb | 6 ++++ spec/producer/core/remote/fs_spec.rb | 46 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/lib/producer/core/remote/fs.rb b/lib/producer/core/remote/fs.rb index f36a2e7..14a8b34 100644 --- a/lib/producer/core/remote/fs.rb +++ b/lib/producer/core/remote/fs.rb @@ -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 diff --git a/spec/producer/core/remote/fs_spec.rb b/spec/producer/core/remote/fs_spec.rb index 0a8d32b..15ce81e 100644 --- a/spec/producer/core/remote/fs_spec.rb +++ b/spec/producer/core/remote/fs_spec.rb @@ -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