File box downloader gives a nice error message if the file doesn't exist.

This commit is contained in:
Mitchell Hashimoto 2010-03-15 19:18:00 -07:00
parent 132aa05f94
commit ef5e73e950
6 changed files with 43 additions and 0 deletions

View File

@ -26,6 +26,9 @@ module Vagrant
end
raise ActionException.new("Unknown URI type for box download.") unless @downloader
# Prepare the downloader
@downloader.prepare(@runner.uri)
end
def execute!

View File

@ -5,6 +5,9 @@ module Vagrant
class Base
include Vagrant::Util
# Called prior to execution so any error checks can be done
def prepare(source_url); end
# Downloads the source file to the destination file. It is up to
# implementors of this class to handle the logic.
def download!(source_url, destination_file); end

View File

@ -3,6 +3,16 @@ module Vagrant
# "Downloads" a file to a temporary file. Basically, this downloader
# simply does a file copy.
class File < Base
def prepare(source_url)
if !::File.file?(source_url)
raise Actions::ActionException.new(<<-msg)
The given box does not exist on the file system:
#{source_url}
msg
end
end
def download!(source_url, destination_file)
FileUtils.cp(source_url, destination_file.path)
end

View File

@ -16,6 +16,10 @@ class DownloadBoxActionTest < Test::Unit::TestCase
@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
@ -25,6 +29,13 @@ class DownloadBoxActionTest < Test::Unit::TestCase
}
end
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)
@action.prepare
end
should "set the downloader to file if URI is generic" do
@uri.stubs(:is_a?).with(URI::Generic).returns(true)
@action.prepare

View File

@ -11,6 +11,13 @@ class BaseDownloaderTest < Test::Unit::TestCase
end
should "implement prepare which does nothing" do
assert_nothing_raised do
assert @base.respond_to?(:prepare)
@base.prepare("source")
end
end
should "implement download! which does nothing" do
assert_nothing_raised do
assert @base.respond_to?(:download!)
@base.download!("source", "destination")

View File

@ -6,6 +6,15 @@ class FileDownloaderTest < Test::Unit::TestCase
@uri = "foo.box"
end
context "preparing" do
should "raise an exception if the file does not exist" do
File.expects(:file?).with(@uri).returns(false)
assert_raises(Vagrant::Actions::ActionException) {
@downloader.prepare(@uri)
}
end
end
context "downloading" do
should "cp the file" do
path = '/path'