From ef5e73e950c81f14684c584de82471ae40437e8a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 15 Mar 2010 19:18:00 -0700 Subject: [PATCH] File box downloader gives a nice error message if the file doesn't exist. --- lib/vagrant/actions/box/download.rb | 3 +++ lib/vagrant/downloaders/base.rb | 3 +++ lib/vagrant/downloaders/file.rb | 10 ++++++++++ test/vagrant/actions/box/download_test.rb | 11 +++++++++++ test/vagrant/downloaders/base_test.rb | 7 +++++++ test/vagrant/downloaders/file_test.rb | 9 +++++++++ 6 files changed, 43 insertions(+) diff --git a/lib/vagrant/actions/box/download.rb b/lib/vagrant/actions/box/download.rb index dfea09410..b35e9460d 100644 --- a/lib/vagrant/actions/box/download.rb +++ b/lib/vagrant/actions/box/download.rb @@ -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! diff --git a/lib/vagrant/downloaders/base.rb b/lib/vagrant/downloaders/base.rb index 5f19cd75c..320a1a3df 100644 --- a/lib/vagrant/downloaders/base.rb +++ b/lib/vagrant/downloaders/base.rb @@ -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 diff --git a/lib/vagrant/downloaders/file.rb b/lib/vagrant/downloaders/file.rb index f63bc89c7..465769056 100644 --- a/lib/vagrant/downloaders/file.rb +++ b/lib/vagrant/downloaders/file.rb @@ -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 diff --git a/test/vagrant/actions/box/download_test.rb b/test/vagrant/actions/box/download_test.rb index f34dec45c..63ed9ffd3 100644 --- a/test/vagrant/actions/box/download_test.rb +++ b/test/vagrant/actions/box/download_test.rb @@ -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 diff --git a/test/vagrant/downloaders/base_test.rb b/test/vagrant/downloaders/base_test.rb index 998eb8faf..6c65720d2 100644 --- a/test/vagrant/downloaders/base_test.rb +++ b/test/vagrant/downloaders/base_test.rb @@ -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") diff --git a/test/vagrant/downloaders/file_test.rb b/test/vagrant/downloaders/file_test.rb index 6e9f783f4..7545c9d2f 100644 --- a/test/vagrant/downloaders/file_test.rb +++ b/test/vagrant/downloaders/file_test.rb @@ -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'