Skip to content

Commit

Permalink
adding support for file: protocol
Browse files Browse the repository at this point in the history
and raising an exception on unknown protocols
  • Loading branch information
flavorjones committed Jan 6, 2016
1 parent f0ebb1e commit c2e7062
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/mini_portile2/mini_portile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,18 +393,20 @@ def output(text = "")
def download_file(url, full_path, count = 3)
return if File.exist?(full_path)
uri = URI.parse(url)
begin
case uri.scheme.downcase
when /ftp/
download_file_ftp(uri, full_path)
when /http|https/
download_file_http(url, full_path, count)
end
rescue Exception => e
File.unlink full_path if File.exist?(full_path)
output "ERROR: #{e.message}"
raise "Failed to complete download task"

case uri.scheme.downcase
when /ftp/
download_file_ftp(uri, full_path)
when /http|https/
download_file_http(url, full_path, count)
when /file/
download_file_file(uri, full_path)
else
raise ArgumentError.new("Unsupported protocol for #{url}")
end
rescue Exception => e
File.unlink full_path if File.exist?(full_path)
raise e
end

def download_file_http(url, full_path, count = 3)
Expand Down Expand Up @@ -448,6 +450,11 @@ def download_file_http(url, full_path, count = 3)
end
end

def download_file_file(uri, full_path)
FileUtils.mkdir_p File.dirname(full_path)
FileUtils.cp uri.path, full_path
end

def download_file_ftp(uri, full_path)
filename = File.basename(uri.path)
with_tempfile(filename, full_path) do |temp_file|
Expand Down
1 change: 1 addition & 0 deletions test/assets/test-download-archive.tar.gz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST FILE
1 change: 1 addition & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'minitest/autorun'
require 'minitest/unit'
require 'minitest/spec'
require 'minitest/hooks/test'
require 'webrick'
require 'fileutils'
Expand Down
71 changes: 71 additions & 0 deletions test/test_download.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require File.expand_path('../helper', __FILE__)

cert_name = [['CN', 'localhost', OpenSSL::ASN1::PRINTABLESTRING]]

describe "recipe download" do
include Minitest::Hooks

attr :recipe

def server_must_receive_connection &block
request_count = 0

server = TCPServer.open('localhost', TestCase::HTTP_PORT)
thread = Thread.new do
conn = server.accept
request_count += 1
conn.puts "CONNECTION SUCESSFULLY MADE"
conn.close
end

block.call

thread.kill
server.close

request_count.must_be :>, 0
end

before do
@request_count = 0
@recipe = MiniPortile.new("test-download", "1.1.1")
end

describe "urls" do
it "ftp" do
@recipe.files << "ftp://localhost:#{TestCase::HTTP_PORT}/foo"
server_must_receive_connection do
@recipe.download
end
end

it "handles http" do
@recipe.files << "http://localhost:#{TestCase::HTTP_PORT}/foo"
server_must_receive_connection do
@recipe.download
end
end

it "handles https" do
@recipe.files << "https://localhost:#{TestCase::HTTP_PORT}/foo"
server_must_receive_connection do
@recipe.download
end
end

it "file" do
dest = "ports/archives/test-download-archive.tar.gz"
FileUtils.rm_f dest
path = File.expand_path(File.join(File.dirname(__FILE__), "assets", "test-download-archive.tar.gz"))
@recipe.files << "file://#{path}"
@recipe.download
assert File.exist?(dest)
Digest::MD5.file(dest).hexdigest.must_equal "5deffb997041bbb5f11bdcafdbb47975"
end

it "other" do
@recipe.files << "foo://foo"
proc { @recipe.download }.must_raise ArgumentError
end
end
end

0 comments on commit c2e7062

Please sign in to comment.