Permalink
Browse files

[test] add in tests, stubs for sftp, and functionality for sftp.

  • Loading branch information...
1 parent 3843ea1 commit a6fb5ec11831158e9641ac6b95d5fc8b0b76341b @knewter knewter committed Mar 4, 2011
View
@@ -0,0 +1,2 @@
+--color
+--format nested
View
@@ -23,6 +23,11 @@ class SomeClass
def some_method
sftp_send(:some_key, remote_path, local_path)
end
+
+ # Some method that fetches a file
+ def fetch_method
+ sftp_fetch(:some_key, remote_path, local_path)
+ end
end
== Motivation ==
@@ -22,4 +22,5 @@ Gem::Specification.new do |s|
s.add_dependency 'enviro', '~> 0.0.4'
s.add_development_dependency "bundler", ">= 1.0.0.rc.6"
s.add_development_dependency "rspec", "~> 2.4.0"
+ s.add_development_dependency "ruby-debug19"
end
@@ -1,7 +1,17 @@
require 'enviro'
require 'net/sftp'
+require 'forwardable'
+
+require 'file_transfer_mixin/interfaces'
module FileTransferMixin
include ::Enviro::Environate
configuration_path_env :file_transfer_mixin_config_path
+
+ module InstanceMethods
+ end
+
+ def self.included(base)
+ base.send :include, InstanceMethods
+ end
end
@@ -0,0 +1,6 @@
+require 'file_transfer_mixin/interfaces/sftp'
+
+module FileTransferMixin
+ module Interfaces
+ end
+end
@@ -0,0 +1,38 @@
+module FileTransferMixin
+ module InstanceMethods
+ extend Forwardable
+
+ def_delegators :file_transfer_mixin_sftp_instance, :sftp_send, :sftp_fetch
+
+ private
+ def file_transfer_mixin_sftp_instance
+ ::FileTransferMixin::Interfaces::SFTP.new
+ end
+ end
+
+ module Interfaces
+ class SFTP
+ def configuration
+ FileTransferMixin.configuration.sftp
+ end
+
+ def sftp_block(key, &block)
+ Net::SFTP.start(configuration[key][:server], configuration[key][:username], :password => configuration[key][:password]) do |sftp|
+ yield(sftp)
+ end
+ end
+
+ def sftp_send(key, remote_path, local_path)
+ sftp_block(key) do |sftp|
+ sftp.upload!(local_path, remote_path)
+ end
+ end
+
+ def sftp_fetch(key, remote_path, local_path)
+ sftp_block(key) do |sftp|
+ sftp.download!(remote_path, local_path)
+ end
+ end
+ end
+ end
+end
@@ -2,6 +2,8 @@
describe FileTransferMixin do
before(:each) do
+ Object.send(:remove_const, :TestFileTransferMixin) if defined?(TestFileTransferMixin)
+
ENV['FILE_TRANSFER_MIXIN_CONFIG_PATH'] = '/tmp/file_transfer_mixin_enviro_config.yml'
config = {
@@ -15,6 +17,10 @@
File.open(ENV['FILE_TRANSFER_MIXIN_CONFIG_PATH'], 'w') do |f|
f.write(YAML.dump(config))
end
+
+ class TestFileTransferMixin
+ include FileTransferMixin
+ end
end
it "should be valid" do
@@ -0,0 +1,55 @@
+require 'spec_helper'
+
+describe ::FileTransferMixin::Interfaces::SFTP do
+ before(:each) do
+ Object.send(:remove_const, :TestFileTransferMixin) if defined?(TestFileTransferMixin)
+ ENV['FILE_TRANSFER_MIXIN_CONFIG_PATH'] = '/tmp/file_transfer_mixin_enviro_config.yml'
+ ENV['ENVY_ENV'] = 'development'
+
+ @config = {
+ :development => {
+ :sftp => {
+ :some_key => {
+ :server => '127.0.0.1',
+ :username => 'user',
+ :password => 'pass',
+ }
+ }
+ },
+ :test => {
+ },
+ :production => {
+ },
+ }
+ File.open(ENV['FILE_TRANSFER_MIXIN_CONFIG_PATH'], 'w') do |f|
+ f.write(YAML.dump(@config))
+ end
+
+ class TestFileTransferMixin
+ include FileTransferMixin
+ end
+
+ sftp_mock = mock('sftp')
+ sftp_mock.stub!(:upload!).and_return(true)
+ sftp_mock.stub!(:download!).and_return(true)
+ Net::SFTP.stub(:start).and_return(sftp_mock)
+ end
+
+ it "should know its parent configuration key" do
+ subject.configuration.should == @config[:development][:sftp]
+ end
+
+ it "should be able to find the server, username, and password for a given key" do
+ subject.configuration[:some_key][:server].should == '127.0.0.1'
+ subject.configuration[:some_key][:username].should == 'user'
+ subject.configuration[:some_key][:password].should == 'pass'
+ end
+
+ it "should respond to sftp_send" do
+ lambda{ TestFileTransferMixin.new.sftp_send(:some_key, 'path', 'file_path') }.should_not raise_error
+ end
+
+ it "should respond to sftp_fetch" do
+ lambda{ TestFileTransferMixin.new.sftp_fetch(:some_key, 'path', 'file_path') }.should_not raise_error
+ end
+end

0 comments on commit a6fb5ec

Please sign in to comment.