Skip to content
This repository has been archived by the owner on Mar 3, 2021. It is now read-only.

Add Support for Wildcard Arguments in LIST Command #19

Merged
merged 3 commits into from Sep 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/fake_ftp/server.rb
Expand Up @@ -117,14 +117,27 @@ def _cwd(*args)
alias :_cdup :_cwd

def _list(*args)
wildcards = []
args.each do |arg|
next unless arg.include? '*'
wildcards << arg.gsub('*', '.*')
end

respond_with('425 Ain\'t no data port!') && return if active? && @active_connection.nil?

respond_with('150 Listing status ok, about to open data connection')
data_client = active? ? @active_connection : @data_server.accept

data_client.write(@files.map do |f|
files = @files
if not wildcards.empty?
files = files.select do |f|
wildcards.any? { |wildcard| f.name =~ /#{wildcard}/ }
end
end
files = files.map do |f|
"-rw-r--r--\t1\towner\tgroup\t#{f.bytes}\t#{f.created.strftime('%b %d %H:%M')}\t#{f.name}"
end.join("\n"))
end
data_client.write(files.join("\n"))
data_client.close
@active_connection = nil

Expand Down
34 changes: 34 additions & 0 deletions spec/models/fake_ftp/server_spec.rb
Expand Up @@ -324,6 +324,40 @@
client.gets.should == "226 List information transferred\r\n"
end

it "accepts a LIST command with a wildcard argument" do
files = ['test.jpg', 'test-2.jpg', 'test.txt']
files.each do |file|
server.add_file(file, '1234567890')
end

client.puts "LIST *.jpg"
client.gets.should == "150 Listing status ok, about to open data connection\r\n"

data = data_client.read(2048)
data_client.close
data.should == files[0,2].map do |file|
"-rw-r--r--\t1\towner\tgroup\t10\t#{server.file(file).created.strftime('%b %d %H:%M')}\t#{file}"
end.join("\n")
client.gets.should == "226 List information transferred\r\n"
end

it "accepts a LIST command with multiple wildcard arguments" do
files = ['test.jpg', 'test.gif', 'test.txt']
files.each do |file|
server.add_file(file, '1234567890')
end

client.puts "LIST *.jpg *.gif"
client.gets.should == "150 Listing status ok, about to open data connection\r\n"

data = data_client.read(2048)
data_client.close
data.should == files[0,2].map do |file|
"-rw-r--r--\t1\towner\tgroup\t10\t#{server.file(file).created.strftime('%b %d %H:%M')}\t#{file}"
end.join("\n")
client.gets.should == "226 List information transferred\r\n"
end

it "accepts an NLST command" do
server.add_file('some_file', '1234567890')
server.add_file('another_file', '1234567890')
Expand Down