diff --git a/.gitignore b/.gitignore index 53b43e7a6..935e70502 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,4 @@ _site/* !templates/* *.org .yardoc/ -doc/ -*~ \ No newline at end of file +doc/ \ No newline at end of file diff --git a/lib/vagrant/actions/box/download.rb b/lib/vagrant/actions/box/download.rb index 385ae451d..5692afb3e 100644 --- a/lib/vagrant/actions/box/download.rb +++ b/lib/vagrant/actions/box/download.rb @@ -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 diff --git a/lib/vagrant/downloaders/file.rb b/lib/vagrant/downloaders/file.rb index 9260bc646..86369201c 100644 --- a/lib/vagrant/downloaders/file.rb +++ b/lib/vagrant/downloaders/file.rb @@ -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) diff --git a/lib/vagrant/downloaders/http.rb b/lib/vagrant/downloaders/http.rb index 1207e688f..4f1dd6968 100644 --- a/lib/vagrant/downloaders/http.rb +++ b/lib/vagrant/downloaders/http.rb @@ -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 ':\\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 \ No newline at end of file +end diff --git a/test/vagrant/actions/box/download_test.rb b/test/vagrant/actions/box/download_test.rb index f5c4f40f7..9df977b05 100644 --- a/test/vagrant/actions/box/download_test.rb +++ b/test/vagrant/actions/box/download_test.rb @@ -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 diff --git a/test/vagrant/downloaders/file_test.rb b/test/vagrant/downloaders/file_test.rb index 7545c9d2f..1933aa501 100644 --- a/test/vagrant/downloaders/file_test.rb +++ b/test/vagrant/downloaders/file_test.rb @@ -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 diff --git a/test/vagrant/downloaders/http_test.rb b/test/vagrant/downloaders/http_test.rb index b58fa2060..88dfdf663 100644 --- a/test/vagrant/downloaders/http_test.rb +++ b/test/vagrant/downloaders/http_test.rb @@ -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