fix for uri parsing issues on windows with a small refactor thrown in on the side

This commit is contained in:
John Bender 2010-03-22 23:48:58 -07:00 committed by unknown
parent c25ff5300a
commit 2067f000f5
7 changed files with 54 additions and 29 deletions

3
.gitignore vendored
View File

@ -8,5 +8,4 @@ _site/*
!templates/*
*.org
.yardoc/
doc/
*~
doc/

View File

@ -14,14 +14,11 @@ module Vagrant
attr_reader :downloader
def prepare
# Parse the URI given and prepare a downloader
uri = URI.parse(@runner.uri)
uri_map = [[URI::HTTP, Downloaders::HTTP], [URI::Generic, Downloaders::File]]
uri_map.find do |uri_type, downloader_klass|
if uri.is_a?(uri_type)
logger.info "#{uri_type} for URI, downloading via #{downloader_klass}..."
@downloader = downloader_klass.new
# Check the URI given and prepare a downloader
[Downloaders::HTTP, Downloaders::File].each do |dler|
if dler.match?(@runner.uri)
logger.info "Downloading via #{dler}..."
@downloader = dler.new
end
end
@ -51,14 +48,13 @@ module Vagrant
def with_tempfile
logger.info "Creating tempfile for storing box file..."
# create, write only, fail if the file exists
File.open(file_temp_path, File::WRONLY | File::EXCL | File::CREAT) do |tempfile|
# create, write only, fail if the file exists
File.open(box_temp_path, File::WRONLY|File::EXCL|File::CREAT) do |tempfile|
yield tempfile
end
end
def file_temp_path
def box_temp_path
File.join(@runner.env.tmp_path, BASENAME + Time.now.to_i.to_s)
end

View File

@ -3,6 +3,10 @@ module Vagrant
# "Downloads" a file to a temporary file. Basically, this downloader
# simply does a file copy.
class File < Base
def self.match?(uri)
::File.exists?(uri)
end
def prepare(source_url)
if !::File.file?(source_url)
raise Actions::ActionException.new(:downloader_file_doesnt_exist, :source_url => source_url)

View File

@ -6,6 +6,11 @@ module Vagrant
# ANSI escape code to clear lines from cursor to end of line
CL_RESET = "\r\e[0K"
def self.match?(uri)
# URI.parse barfs on '<drive letter>:\\files \on\ windows'
URI.extract(uri).first.include?(uri)
end
def download!(source_url, destination_file)
Net::HTTP.get_response(URI.parse(source_url)) do |response|
total = response.content_length
@ -44,4 +49,4 @@ module Vagrant
end
end
end
end
end

View File

@ -13,17 +13,14 @@ class DownloadBoxActionTest < Test::Unit::TestCase
context "preparing" do
setup do
@uri = mock("uri")
@uri.stubs(:is_a?).returns(false)
URI.stubs(:parse).returns(@uri)
@downloader = mock("downloader")
Vagrant::Downloaders::File.any_instance.stubs(:prepare)
Vagrant::Downloaders::HTTP.any_instance.stubs(:prepare)
end
should "raise an exception if no URI type is matched" do
@uri.stubs(:is_a?).returns(false)
should "raise an exception if no URI type is matched" do\
Vagrant::Downloaders::File.expects(:match?).returns(false)
Vagrant::Downloaders::HTTP.expects(:match?).returns(false)
assert_raises(Vagrant::Actions::ActionException) {
@action.prepare
}
@ -32,21 +29,31 @@ class DownloadBoxActionTest < Test::Unit::TestCase
should "call #prepare on the downloader" do
@downloader.expects(:prepare).with(@runner.uri).once
Vagrant::Downloaders::File.expects(:new).returns(@downloader)
@uri.stubs(:is_a?).with(URI::Generic).returns(true)
expect_file
@action.prepare
end
should "set the downloader to file if URI is generic" do
@uri.stubs(:is_a?).with(URI::Generic).returns(true)
should "set the downloader to file if the uri provided is a file" do
expect_file
@action.prepare
assert @action.downloader.is_a?(Vagrant::Downloaders::File)
end
should "set the downloader to HTTP if URI is HTTP" do
@uri.stubs(:is_a?).with(URI::HTTP).returns(true)
should "set the downloader to HTTP if the uri provided is a valid url" do
expect_http
@action.prepare
assert @action.downloader.is_a?(Vagrant::Downloaders::HTTP)
end
def expect_file
Vagrant::Downloaders::File.expects(:match?).returns(true)
Vagrant::Downloaders::HTTP.expects(:match?).returns(false)
end
def expect_http
Vagrant::Downloaders::File.expects(:match?).returns(false)
Vagrant::Downloaders::HTTP.expects(:match?).returns(true)
end
end
context "executing" do
@ -81,9 +88,9 @@ class DownloadBoxActionTest < Test::Unit::TestCase
context "tempfile" do
should "create a tempfile in the vagrant tmp directory" do
File.expects(:open).with { |name, bitmask|
name =~ /#{Vagrant::Actions::Box::Download::BASENAME}/ && name =~ /#{@runner.env.tmp_path}/
}.once
File.expects(:open).with { |name, bitmask|
name =~ /#{Vagrant::Actions::Box::Download::BASENAME}/ && name =~ /#{@runner.env.tmp_path}/
}.once
@action.with_tempfile
end

View File

@ -23,4 +23,11 @@ class FileDownloaderTest < Test::Unit::TestCase
@downloader.download!(@uri, @tempfile)
end
end
context "matching a uri" do
should "return true if the File exists on the file system" do
File.expects(:exists?).with('foo').returns(true)
assert Vagrant::Downloaders::File.match?('foo')
end
end
end

View File

@ -34,6 +34,13 @@ class HttpDownloaderTest < Test::Unit::TestCase
end
end
context "matching the uri" do
should "use extract to verify that the string is in fact a uri" do
URI.expects(:extract).returns(['foo'])
assert Vagrant::Downloaders::HTTP.match?('foo')
end
end
context "reporting progress" do
# TODO: Testing for this, probably
end