features/support/test_client.rb in ftpd-1.1.1 vs features/support/test_client.rb in ftpd-2.0.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'double_bag_ftps' require 'net/ftp' class TestClient @@ -18,14 +20,15 @@ @ftp = make_ftp end def close return unless @ftp - @ftp.close + ftp.close + @ftp = nil end - def_delegators :@ftp, + def_delegators :ftp, :chdir, :connect, :delete, :getbinaryfile, :gettextfile, @@ -52,37 +55,37 @@ socket = Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0) socket.bind(in_addr) socket.connect(out_addr) decorate_socket socket @ftp = make_ftp - @ftp.set_socket(socket) + ftp.set_socket(socket) end def raw(*command) - @ftp.sendcmd command.compact.join(' ') + ftp.sendcmd command.compact.join(' ') end def get(mode, remote_path) method = "get#{mode}file" - @ftp.send method, remote_path, local_path(remote_path) + ftp.send method, remote_path, local_path(remote_path) end def put(mode, remote_path) method = "put#{mode}file" - @ftp.send method, local_path(remote_path), remote_path + ftp.send method, local_path(remote_path), remote_path end def get_size(mode, remote_path) raise unless ['binary', 'text'].include?(mode) - @ftp.binary = mode == 'binary' + ftp.binary = mode == 'binary' override_with_binary do - @ftp.size(remote_path) + ftp.size(remote_path) end end def get_mtime(remote_path) - @ftp.mtime(remote_path) + ftp.mtime(remote_path) end def add_file(path) full_path = temp_path(path) mkdir_p File.dirname(full_path) @@ -98,54 +101,59 @@ def file_contents(path) File.open(temp_path(path), 'rb', &:read) end def xpwd - response = @ftp.sendcmd('XPWD') + response = ftp.sendcmd('XPWD') response[/"(.+)"/, 1] end def store_unique(local_path, remote_path) command = ['STOU', remote_path].compact.join(' ') File.open(temp_path(local_path), 'rb') do |file| - @ftp.storbinary command, file, Net::FTP::DEFAULT_BLOCKSIZE + ftp.storbinary command, file, Net::FTP::DEFAULT_BLOCKSIZE end end def append_binary(local_path, remote_path) command = ['APPE', remote_path].compact.join(' ') File.open(temp_path(local_path), 'rb') do |file| - @ftp.storbinary command, file, Net::FTP::DEFAULT_BLOCKSIZE + ftp.storbinary command, file, Net::FTP::DEFAULT_BLOCKSIZE end end def append_text(local_path, remote_path) command = ['APPE', remote_path].compact.join(' ') File.open(temp_path(local_path), 'rb') do |file| - @ftp.storlines command, file + ftp.storlines command, file end end def connected? begin - @ftp.noop + ftp.noop true rescue Net::FTPTempError => e !!e.to_s =~ /^421/ rescue EOFError false end end def set_option(option) - @ftp.sendcmd "OPTS #{option}" + ftp.sendcmd "OPTS #{option}" end private - + RAW_METHOD_REGEX = /^send_(.*)$/ + def ftp + raise "Not started" unless @ftp + @ftp + end + def local_path(remote_path) temp_path(File.basename(remote_path)) end def temp_path(path) @@ -205,15 +213,15 @@ end end def override_with_binary - orig = @ftp.override_with_binary + orig = ftp.override_with_binary begin - @ftp.override_with_binary = true + ftp.override_with_binary = true yield ensure - @ftp.override_with_binary = orig + ftp.override_with_binary = orig end end # Ruby 2.0's Ftp class is expecting a TCPSocket, not a Socket. The # trouble comes with Ftp#close, which closes sockets by first doing