Skip to content

Commit

Permalink
Getting #nlst working
Browse files Browse the repository at this point in the history
  • Loading branch information
petebrowne committed Jul 15, 2010
1 parent 155605d commit 1fbf802
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Gemfile
@@ -1,5 +1,5 @@
source 'http://rubygems.org'

group :development do
gem 'rspec', '~> 1.3.0'
gem 'rspec', '>= 2.0.0.beta.17'
end
12 changes: 10 additions & 2 deletions Gemfile.lock
@@ -1,10 +1,18 @@
GEM
remote: http://rubygems.org/
specs:
rspec (1.3.0)
diff-lcs (1.1.2)
rspec (2.0.0.beta.17)
rspec-core (= 2.0.0.beta.17)
rspec-expectations (= 2.0.0.beta.17)
rspec-mocks (= 2.0.0.beta.17)
rspec-core (2.0.0.beta.17)
rspec-expectations (2.0.0.beta.17)
diff-lcs (>= 1.1.2)
rspec-mocks (2.0.0.beta.17)

PLATFORMS
ruby

DEPENDENCIES
rspec (~> 1.3.0)
rspec (>= 2.0.0.beta.17)
12 changes: 5 additions & 7 deletions Rakefile
Expand Up @@ -5,15 +5,13 @@ task :default => :spec
#------------------------------------

begin
require 'spec/rake/spectask'
require 'rspec/core'
require 'rspec/core/rake_task'
rescue LoadError
raise 'Run `gem install rspec` to be able to run specs'
raise 'Run `gem install rspec --pre` to be able to run specs'
else
desc 'Run specs'
Spec::Rake::SpecTask.new do |t|
t.spec_files = FileList['spec/*_spec.rb']
t.spec_opts = %w(-fs --color)
end
desc 'Run the specs'
RSpec::Core::RakeTask.new
end

#------------------------------------
Expand Down
18 changes: 17 additions & 1 deletion lib/mock_ftp.rb
@@ -1,5 +1,21 @@
require 'net/ftp'
require 'mock_ftp/core_ext/string'

module MockFTP
def mock_ftp
autoload :File, 'mock_ftp/file'
autoload :Folder, 'mock_ftp/folder'

module Net
autoload :FTP, 'mock_ftp/net_ftp'
end

def mock_ftp(root_path = '', &block)
@original_net_ftp = ::Net.__send__ :remove_const, :FTP
::Net.const_set :FTP, MockFTP::Net::FTP

MockFTP::Folder.root(root_path, &block)

::Net.__send__ :remove_const, :FTP
::Net.const_set :FTP, @original_net_ftp
end
end
40 changes: 39 additions & 1 deletion spec/mock_ftp_spec.rb
@@ -1,5 +1,43 @@
require 'spec_helper'

describe MockFTP do
pending 'YES!'
describe '#nlst' do
it 'should list mocked files' do
mock_ftp do |f|
f.file 'file 1'
f.file 'file 2'

open_ftp do |ftp|
ftp.nlst.should == [ 'file 1', 'file 2' ]
end
end
end

context 'within a folder' do
it 'should list mocked files with prefixed folder name' do
mock_ftp do |f|
f.folder 'folder' do |f|
f.file 'file 1'
f.file 'file 2'
end

open_ftp do |ftp|
ftp.nlst('/folder').should == [ '/folder/file 1', '/folder/file 2' ]
end
end
end
end

context 'without a folder' do
it 'should raise an error' do
mock_ftp do |f|
open_ftp do |ftp|
expect {
ftp.nlst('blah')
}.to raise_error(Net::FTPPermError)
end
end
end
end
end
end
8 changes: 6 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -6,6 +6,10 @@
Bundler.require(:default, :development)
require 'mock_ftp'

Spec::Runner.configure do |config|

RSpec.configure do |config|
config.include MockFTP
end

def open_ftp(&block)
::Net::FTP.open('www.example.com', &block)
end

0 comments on commit 1fbf802

Please sign in to comment.