From 38248c240c26c6c7f3ebaf9754bac8c0a1de511d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 24 Jan 2014 11:24:06 -0800 Subject: [PATCH] core: BoxAdd errors if name doesn't match metadata --- lib/vagrant/action/builtin/box_add.rb | 6 ++++ lib/vagrant/errors.rb | 4 +++ templates/locales/en.yml | 9 +++++ .../vagrant/action/builtin/box_add_test.rb | 35 +++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/lib/vagrant/action/builtin/box_add.rb b/lib/vagrant/action/builtin/box_add.rb index 407e1a8e2..f35b3f5ec 100644 --- a/lib/vagrant/action/builtin/box_add.rb +++ b/lib/vagrant/action/builtin/box_add.rb @@ -97,6 +97,12 @@ module Vagrant metadata_path.delete if metadata_path && metadata_path.file? end + if env[:box_name] && metadata.name != env[:box_name] + raise Errors::BoxAddNameMismatch, + actual_name: metadata.name, + requested_name: env[:box_name] + end + metadata_version = metadata.version( version || ">= 0", provider: provider) if !metadata_version diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 4474854b8..54a63ab26 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -120,6 +120,10 @@ module Vagrant error_key(:batch_multi_error) end + class BoxAddNameMismatch < VagrantError + error_key(:box_add_name_mismatch) + end + class BoxAddNameRequired < VagrantError error_key(:box_add_name_required) end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index accb87fa0..4caa5da4f 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -289,6 +289,15 @@ en: Name: %{name} Provider: %{provider} Version: %{version} + box_add_name_mismatch: |- + The box you're adding has a name different from the name you + requested. For boxes with metadata, you cannot override the name. + If you're adding a box using `vagrant box add`, don't specify + the `--name` parameter. If the box is being added via a Vagrantfile, + change the `config.vm.box` value to match the name below. + + Requested name: %{requested_name} + Actual name: %{actual_name} box_add_name_required: |- A name is required when adding a box file directly. Please pass the `--name` parameter to `vagrant box add`. See diff --git a/test/unit/vagrant/action/builtin/box_add_test.rb b/test/unit/vagrant/action/builtin/box_add_test.rb index f761bb35d..e821d25ff 100644 --- a/test/unit/vagrant/action/builtin/box_add_test.rb +++ b/test/unit/vagrant/action/builtin/box_add_test.rb @@ -558,6 +558,41 @@ describe Vagrant::Action::Builtin::BoxAdd do subject.call(env) end + it "raises an exception if the name doesn't match a requested name" do + box_path = iso_env.box2_file(:virtualbox) + tf = Tempfile.new("vagrant").tap do |f| + f.write(<<-RAW) + { + "name": "foo/bar", + "versions": [ + { + "version": "0.5" + }, + { + "version": "0.7", + "providers": [ + { + "name": "virtualbox", + "url": "#{box_path}" + } + ] + } + ] + } + RAW + f.close + end + + env[:box_name] = "foo" + env[:box_url] = tf.path + + box_collection.should_receive(:add).never + app.should_receive(:call).never + + expect { subject.call(env) }. + to raise_error(Vagrant::Errors::BoxAddNameMismatch) + end + it "raises an exception if no matching version" do box_path = iso_env.box2_file(:vmware) tf = Tempfile.new("vagrant").tap do |f|