From 9e33d16ca19c679951e5e7f910ce263a73ac3370 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 1 Jul 2012 21:46:16 -0700 Subject: [PATCH] Adding a box that already exists should result in an exception. --- lib/vagrant/box_collection2.rb | 12 ++++++++++-- test/unit/vagrant/box_collection2_test.rb | 13 +++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/box_collection2.rb b/lib/vagrant/box_collection2.rb index eca92d5b2..63a0966a9 100644 --- a/lib/vagrant/box_collection2.rb +++ b/lib/vagrant/box_collection2.rb @@ -23,6 +23,9 @@ module Vagrant # This adds a new box to the system. # # There are some exceptional cases: + # * BoxAlreadyExists - The box you're attempting to add already exists. + # * BoxProviderDoesntMatch - If the given box provider doesn't match the + # actual box provider in the untarred box. # # Preconditions: # * File given in `path` must exist. @@ -33,9 +36,14 @@ module Vagrant # will be verified with the `metadata.json` file in the box and is # meant as a basic check. def add(path, name, provider) + @logger.debug("Adding box: #{name} (#{provider}) from #{path}") + if find(name, provider) + @logger.error("Box already exists, can't add: #{name} #{provider}") + raise Errors::BoxAlreadyExists, :name => name, :provider => provider + end + box_dir = @directory.join(name, provider.to_s) - @logger.debug("Adding box: #{path}") - @logger.debug("Box directory: #{box_dir}") + @logger.debug("New box directory: #{box_dir}") # Create the directory that'll store our box box_dir.mkpath diff --git a/test/unit/vagrant/box_collection2_test.rb b/test/unit/vagrant/box_collection2_test.rb index e7701040c..c2e962fd1 100644 --- a/test/unit/vagrant/box_collection2_test.rb +++ b/test/unit/vagrant/box_collection2_test.rb @@ -24,6 +24,19 @@ describe Vagrant::BoxCollection2 do box.should_not be_nil end + it "should raise an exception if the box already exists" do + prev_box_name = "foo" + prev_box_provider = :virtualbox + + # Create the box we're adding + environment.box2(prev_box_name, prev_box_provider) + + # Attempt to add the box with the same name + box_path = environment.box2_file(prev_box_provider) + expect { instance.add(box_path, prev_box_name, prev_box_provider) }. + to raise_error(Vagrant::Errors::BoxAlreadyExists) + end + it "should raise an exception and not add the box if the provider doesn't match" do box_name = "foo" good_provider = :virtualbox