From c952fdaa2407acbd4cbfd7b689332bdd393a81b8 Mon Sep 17 00:00:00 2001 From: John Bender Date: Wed, 3 Mar 2010 23:47:16 -0800 Subject: [PATCH] check if a box exists before beginning add action --- lib/vagrant/actions/box/add.rb | 6 +++++- test/vagrant/actions/box/add_test.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/vagrant/actions/box/add.rb b/lib/vagrant/actions/box/add.rb index 98a119b81..426a68e77 100644 --- a/lib/vagrant/actions/box/add.rb +++ b/lib/vagrant/actions/box/add.rb @@ -3,10 +3,14 @@ module Vagrant module Box class Add < Base def prepare + if File.exists?(@runner.directory) + raise ActionException.new("A box with the name '#{@runner.name}' already exists, please use another name or use `vagrant box remove #{@runner.name}`") + end + @runner.add_action(Download) @runner.add_action(Unpackage) end end end end -end \ No newline at end of file +end diff --git a/test/vagrant/actions/box/add_test.rb b/test/vagrant/actions/box/add_test.rb index 0eef239e1..c99197c48 100644 --- a/test/vagrant/actions/box/add_test.rb +++ b/test/vagrant/actions/box/add_test.rb @@ -3,6 +3,8 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') class AddBoxActionTest < Test::Unit::TestCase setup do @runner, @vm, @action = mock_action(Vagrant::Actions::Box::Add) + @runner.stubs(:directory).returns("foo") + File.stubs(:exists?).returns(false) mock_config end @@ -23,4 +25,15 @@ class AddBoxActionTest < Test::Unit::TestCase @action.prepare end end + + context "providing a name for a base that exists" do + should "result in an action exception" do + File.expects(:exists?).once.returns(true) + @runner.expects(:name).twice.returns('foo') + @runner.expects(:add_action).never + assert_raise Vagrant::Actions::ActionException do + @action.prepare + end + end + end end