Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Guardfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard 'rspec', :version => 2 do
guard 'rspec', version: 2, cli: '--format documentation' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
Expand Down
20 changes: 19 additions & 1 deletion lib/wordpress_deploy/storage/ftp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ def initialize(&block)
instance_eval(&block) if block_given?
end

##
# Check if there is an open connection.
#
# Captures all exceptions that would be raised; returns false in the
# of any exception being raised.
#
# Returns true only if the connection is open, false otherwise.
def open?
!ftp.closed?
rescue Net::FTPConnectionError
false
end

##
# An array of the files in the configured Wordpress directory.
def files
Dir.glob(File.join(Config.wp_dir, "**/*")).sort
end

def host(new_host = nil)
unless new_host.nil?
match = /(?<host>.*?)(?=:|\z)(:(?<port>\d+))?/.match(new_host.to_s)
Expand Down Expand Up @@ -78,7 +97,6 @@ def transmit
#
def transmit!
connect
files = Dir.glob(File.join(Config.wp_dir, "**/*")).sort
files.each do |file|
put_file file
end
Expand Down
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require 'rubygems'
require 'bundler/setup'
require 'fake_ftp'
require 'fakefs/safe'
require 'fakefs/spec_helpers'

require 'wordpress_deploy'

Expand All @@ -23,6 +26,8 @@ def data_dir
# config.mock_with :rr
config.mock_with :rspec

config.include FakeFS::SpecHelpers, fakefs: true

# Reset the environment for each spec that is run
config.before(:each) do
WordpressDeploy::Config.root_dir = data_dir
Expand Down
95 changes: 57 additions & 38 deletions spec/wordpress_deploy/storage/ftp_spec.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,77 @@
require 'spec_helper'

include WordpressDeploy
include WordpressDeploy::Storage

# TODO
# * Test for Errno::ECONNREFUSED
# * Test for Errno::ECONNRESET
# * Test for Net::FTPPermError
# * Test for Net::FTPConnectionError

describe WordpressDeploy::Storage::Ftp.new do
let(:control_port) { 21212 }
let(:data_port) { 21213 }
let(:ftp_server) { FakeFtp::Server.new(control_port, data_port) }
let(:ftp_directory) { WordpressDeploy::Config.wp_dir }

describe WordpressDeploy::Storage::Ftp do
before(:each) do
# None of the methods that follow are testing
# the Net::FTP object actions; therefore they
# can be stubbed out
@ftp = double("ftp")
[:connect, :login, :passive=].each do |methods|
@ftp.stub(methods).with(any_args)
end
Net::FTP.stub(:new).and_return(@ftp)
# Start and stop the fake FTP server between each test
ftp_server.start
end

it "has methods that allow for interactive overwrite" do
expect { subject.transmit }.to raise_error(NotImplementedError)
expect { subject.receive }.to raise_error(NotImplementedError)
after(:each) do
# Start and stop the fake FTP server between each test
ftp_server.stop
end

it { should respond_to :transmit! }
it { should respond_to :receive! }

context "FTP connection" do
context "default parameters" do
its(:host) { should eq "localhost" }
its(:port) { should eq 21 }
its(:open?) { should be_false }
its(:user) { should eq "root" }
its(:password) { should eq "" }
end

describe WordpressDeploy::Storage::Ftp.new do
before(:each) do
@ftp = double("ftp")
@ftp.should_receive(:connect).with("ftp.hanerutherford.biz", 77)
@ftp.should_receive(:login).with("red_user", "Bun__huPEMeBreM6tebRAp@eguzuQExe")
@ftp.should_receive(:passive=).with(true)
@ftp.stub(:pwd)
@ftp.stub(:closed?).and_return(false)
@ftp.stub(:close)
@ftp.stub(:chdir)
@ftp.stub(:putbinaryfile)
Net::FTP.stub(:new).and_return(@ftp)
WordpressDeploy::Storage::Ftp.any_instance.stub(:ftp).and_return(@ftp)
subject.host "localhost:#{control_port}"
subject.user "red_user"
subject.password "Bun__huPEMeBreM6tebRAp@eguzuQExe"
subject.destination "/html"
end
its(:port) { should eq control_port }
its(:host) { should eq "localhost" }
its(:open?) { should be_false }
context "files" do
before(:all) do
# Start FakeFS
FakeFS.activate!

it "should send files" do
files = Dir.glob(File.join(data_dir, "**/*"))

Dir.should_receive(:glob).with("#{data_dir}/**/*").and_return(files)
FileUtils.mkdir_p(ftp_directory)
File.open(File.join(ftp_directory, "file1.txt"), "w") do |file|
file.puts("ryan")
end
end

ftp = WordpressDeploy::Storage::Ftp.new do
host "ftp.hanerutherford.biz:77"
user "red_user"
password "Bun__huPEMeBreM6tebRAp@eguzuQExe"
destination "/html"
after(:all) do
# Stop the FakeFS
FakeFS.deactivate!
end
its(:files) { should eq [File.join(ftp_directory, "file1.txt")] }
end

ftp.should_receive(:put_file).exactly(files.count).times
context "#transmit!" do
let(:file_count) { subject.files.size }
before(:each) { subject.transmit! }
it { ftp_server.files.should have(file_count).file }
it { ftp_server.files.should include("file1.txt") }
end

ftp.transmit!
context "receive files" do
before(:each) { subject.receive! }
it "should have all the same files as the server"
end
end

end
11 changes: 7 additions & 4 deletions wordpress_deploy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ Gem::Specification.new do |gem|
gem.add_dependency 'php-serialize_ryan', ['~> 1.1.0']
gem.add_dependency 'mysql2', ['~> 0.3.11']

gem.add_development_dependency 'rake', ['~> 10.0.0']
gem.add_development_dependency 'rspec', ['~> 2.13.0']
gem.add_development_dependency 'guard-rspec', ['~> 2.5.0']
gem.add_development_dependency 'rb-fsevent', ['~> 0.9.1']
gem.add_development_dependency 'rake', ['~> 10.0.0']
gem.add_development_dependency 'rspec', ['~> 2.13.0']
gem.add_development_dependency 'guard-rspec', ['~> 2.5.0']
gem.add_development_dependency 'rb-fsevent', ['~> 0.9.1']
gem.add_development_dependency 'terminal-notifier-guard', ['~> 1.5.3']
gem.add_development_dependency 'fakefs', ['~> 0.3.2']
gem.add_development_dependency 'fake_ftp', ['~> 0.0.9']
end