`vagrant box remove` functionality is in

This commit is contained in:
Mitchell Hashimoto 2010-02-22 21:27:57 -08:00
parent f502587931
commit 546db294ca
6 changed files with 114 additions and 7 deletions

View File

@ -21,7 +21,7 @@ msg
end
def box_dir
File.join(Env.boxes_path, @runner.name)
@runner.directory
end
def decompress

View File

@ -5,16 +5,38 @@ module Vagrant
attr_accessor :temp_path
class <<self
# Finds a box by the given name
def find(name)
return nil unless File.directory?(directory(name))
new(name)
end
def add(name, uri)
box = new
box.name = name
box.uri = uri
box.add
end
def directory(name)
File.join(Env.boxes_path, name)
end
end
def initialize(name=nil)
@name = name
end
def add
execute!(Actions::Box::Add)
end
def destroy
FileUtils.rm_rf(directory)
end
def directory
self.class.directory(self.name)
end
end
end

View File

@ -161,6 +161,19 @@ error
Box.add(name, path)
end
# Removes a box.
def box_remove(name)
box = Box.find(name)
if box.nil?
error_and_exit(<<-error)
The box you're attempting to remove does not exist!
error
return # for tests
end
box.destroy
end
private
def act_on_vm(&block)

View File

@ -20,12 +20,10 @@ class UnpackageBoxActionTest < Test::Unit::TestCase
end
context "box directory" do
setup do
@box_dir = File.join(Vagrant::Env.boxes_path, @runner.name)
end
should "return the boxes_path joined with the name" do
assert_equal @box_dir, @action.box_dir
should "return the runner directory" do
result = mock("object")
@runner.expects(:directory).once.returns(result)
assert result.equal?(@action.box_dir)
end
end

View File

@ -2,6 +2,26 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper')
class BoxTest < Test::Unit::TestCase
context "class methods" do
context "finding" do
setup do
@dir = "foo"
@name = "bar"
Vagrant::Box.stubs(:directory).with(@name).returns(@dir)
end
should "return nil if the box doesn't exist" do
File.expects(:directory?).with(@dir).once.returns(false)
assert_nil Vagrant::Box.find(@name)
end
should "return a box object with the proper name set" do
File.expects(:directory?).with(@dir).once.returns(true)
result = Vagrant::Box.find(@name)
assert result
assert_equal @name, result.name
end
end
context "adding" do
setup do
@name = "foo"
@ -17,6 +37,17 @@ class BoxTest < Test::Unit::TestCase
Vagrant::Box.add(@name, @uri)
end
end
context "box directory" do
setup do
@name = "foo"
@box_dir = File.join(Vagrant::Env.boxes_path, @name)
end
should "return the boxes_path joined with the name" do
assert_equal @box_dir, Vagrant::Box.directory(@name)
end
end
end
context "instance methods" do
@ -28,5 +59,29 @@ class BoxTest < Test::Unit::TestCase
@box.expects(:execute!).with(Vagrant::Actions::Box::Add).once
@box.add
end
context "box directory" do
setup do
@box.name = "foo"
end
should "return the boxes_path joined with the name" do
result = mock("object")
Vagrant::Box.expects(:directory).with(@box.name).returns(result)
assert result.equal?(@box.directory)
end
end
context "destroying" do
setup do
@dir = mock("directory")
@box.stubs(:directory).returns(@dir)
end
should "rm_rf the directory" do
FileUtils.expects(:rm_rf).with(@dir).once
@box.destroy
end
end
end
end

View File

@ -210,4 +210,23 @@ class CommandsTest < Test::Unit::TestCase
Vagrant::Commands.box_add(@name, @path)
end
end
context "box remove" do
setup do
@name = "foo"
end
should "error and exit if the box doesn't exist" do
Vagrant::Box.expects(:find).returns(nil)
Vagrant::Commands.expects(:error_and_exit).once
Vagrant::Commands.box_remove(@name)
end
should "call destroy on the box if it exists" do
@box = mock("box")
Vagrant::Box.expects(:find).with(@name).returns(@box)
@box.expects(:destroy).once
Vagrant::Commands.box_remove(@name)
end
end
end