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/* !templates/*
*.org *.org
.yardoc/ .yardoc/
doc/ doc/
*~

View File

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

View File

@ -3,6 +3,10 @@ module Vagrant
# "Downloads" a file to a temporary file. Basically, this downloader # "Downloads" a file to a temporary file. Basically, this downloader
# simply does a file copy. # simply does a file copy.
class File < Base class File < Base
def self.match?(uri)
::File.exists?(uri)
end
def prepare(source_url) def prepare(source_url)
if !::File.file?(source_url) if !::File.file?(source_url)
raise Actions::ActionException.new(:downloader_file_doesnt_exist, :source_url => 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 # ANSI escape code to clear lines from cursor to end of line
CL_RESET = "\r\e[0K" 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) def download!(source_url, destination_file)
Net::HTTP.get_response(URI.parse(source_url)) do |response| Net::HTTP.get_response(URI.parse(source_url)) do |response|
total = response.content_length total = response.content_length
@ -44,4 +49,4 @@ module Vagrant
end end
end end
end end
end end

View File

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

View File

@ -23,4 +23,11 @@ class FileDownloaderTest < Test::Unit::TestCase
@downloader.download!(@uri, @tempfile) @downloader.download!(@uri, @tempfile)
end end
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 end

View File

@ -34,6 +34,13 @@ class HttpDownloaderTest < Test::Unit::TestCase
end end
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 context "reporting progress" do
# TODO: Testing for this, probably # TODO: Testing for this, probably
end end