From 647563edaf32c0cab6a13d73c5ea6792a88ebc53 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 3 Feb 2014 09:09:11 +0100 Subject: [PATCH] core: shorthand boxes must be foo/bar formatted, better errors --- lib/vagrant/action/builtin/box_add.rb | 10 +++++++--- lib/vagrant/errors.rb | 4 ++++ templates/locales/en.yml | 11 +++++++++++ test/unit/vagrant/action/builtin/box_add_test.rb | 15 +++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/vagrant/action/builtin/box_add.rb b/lib/vagrant/action/builtin/box_add.rb index baed28050..47bfc6f94 100644 --- a/lib/vagrant/action/builtin/box_add.rb +++ b/lib/vagrant/action/builtin/box_add.rb @@ -37,10 +37,14 @@ module Vagrant # then expand it properly. expanded = false url.each_index do |i| - uri = URI.parse(url[i]) - if !uri.scheme && !File.file?(url[i]) + next if url[i] !~ /^[^\/]+\/[^\/]+$/ + + if !File.file?(url[i]) + server = Vagrant.server_url + raise Errors::BoxServerNotSet if !server + expanded = true - url[i] = "#{Vagrant.server_url}/#{url[i]}" + url[i] = "#{server}/#{url[i]}" end end diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 654621337..01d3219de 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -204,6 +204,10 @@ module Vagrant error_key(:box_remove_multi_version) end + class BoxServerNotSet < VagrantError + error_key(:box_server_not_set) + end + class BoxUnpackageFailure < VagrantError error_key(:untar_failure, "vagrant.actions.box.unpackage") end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 0981aaaa7..5438dab66 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -419,6 +419,17 @@ en: the provider specified. Please double-check and try again. The providers for this are: %{providers} + box_server_not_set: |- + A URL to a Vagrant Cloud server is not set, so boxes cannot + be added with a shorthand ("mitchellh/precise64") format. + You may also be seeing this error if you meant to type in + a path to a box file which doesn't exist locally on your + system. + + To set a URL to a Vagrant Cloud server, set the + `VAGRANT_SERVER_URL` environmental variable. Or, if you + meant to use a file path, make sure the path to the file + is valid. box_update_multi_provider: |- You requested to update the box '%{name}'. This box has multiple providers. You must explicitly select a single diff --git a/test/unit/vagrant/action/builtin/box_add_test.rb b/test/unit/vagrant/action/builtin/box_add_test.rb index ccd52ae58..7c2c2d473 100644 --- a/test/unit/vagrant/action/builtin/box_add_test.rb +++ b/test/unit/vagrant/action/builtin/box_add_test.rb @@ -262,6 +262,21 @@ describe Vagrant::Action::Builtin::BoxAdd do end end + it "raises an error if no Vagrant server is set" do + tf = Tempfile.new("foo") + tf.close + + env[:box_url] = "mitchellh/precise64.json" + + box_collection.should_receive(:add).never + app.should_receive(:call).never + + Vagrant.stub(server_url: nil) + + expect { subject.call(env) }. + to raise_error(Vagrant::Errors::BoxServerNotSet) + end + it "raises an error if shorthand is invalid" do tf = Tempfile.new("foo") tf.close