Converted Box class to use new environment

This commit is contained in:
Mitchell Hashimoto 2010-03-19 21:31:43 -07:00
parent 5db99c04d4
commit 11780fb618
6 changed files with 56 additions and 37 deletions

View File

@ -60,12 +60,12 @@ module Vagrant
# Returns an array of all created boxes, as strings. # Returns an array of all created boxes, as strings.
# #
# @return [Array<String>] # @return [Array<String>]
def all def all(env)
results = [] results = []
Dir.open(Env.boxes_path) do |dir| Dir.open(env.boxes_path) do |dir|
dir.each do |d| dir.each do |d|
next if d == "." || d == ".." || !File.directory?(File.join(Env.boxes_path, d)) next if d == "." || d == ".." || !File.directory?(File.join(env.boxes_path, d))
results << d.to_s results << d.to_s
end end
end end
@ -79,8 +79,8 @@ module Vagrant
# #
# @param [String] name The name of the box # @param [String] name The name of the box
# @return [Box] Instance of {Box} representing the box found # @return [Box] Instance of {Box} representing the box found
def find(name) def find(env, name)
return nil unless File.directory?(directory(name)) return nil unless File.directory?(directory(env, name))
new(name) new(name)
end end
@ -90,10 +90,11 @@ module Vagrant
# #
# @param [String] name The name of the box # @param [String] name The name of the box
# @param [String] uri URI to the box file # @param [String] uri URI to the box file
def add(name, uri) def add(env, name, uri)
box = new box = new
box.name = name box.name = name
box.uri = uri box.uri = uri
box.env = env
box.add box.add
end end
@ -103,7 +104,7 @@ module Vagrant
# #
# @param [String] name Name of the box whose directory you're interested in. # @param [String] name Name of the box whose directory you're interested in.
# @return [String] Full path to the box directory. # @return [String] Full path to the box directory.
def directory(name) def directory(env, name)
File.join(Env.boxes_path, name) File.join(Env.boxes_path, name)
end end
end end

View File

@ -169,7 +169,7 @@ msg
# which action to take and calls the respective action method # which action to take and calls the respective action method
# (see {box_add} and {box_remove}) # (see {box_add} and {box_remove})
def box(argv) def box(argv)
Environment.load! env = Environment.load!
sub_commands = ["list", "add", "remove"] sub_commands = ["list", "add", "remove"]
@ -177,12 +177,12 @@ msg
error_and_exit(:command_box_invalid) error_and_exit(:command_box_invalid)
end end
send("box_#{argv[0]}", *argv[1..-1]) send("box_#{argv[0]}", env, *argv[1..-1])
end end
# Lists all added boxes # Lists all added boxes
def box_list def box_list(env)
boxes = Box.all.sort boxes = Box.all(env).sort
wrap_output do wrap_output do
if !boxes.empty? if !boxes.empty?
@ -197,13 +197,13 @@ msg
end end
# Adds a box to the local filesystem, given a URI. # Adds a box to the local filesystem, given a URI.
def box_add(name, path) def box_add(env, name, path)
Box.add(name, path) Box.add(env, name, path)
end end
# Removes a box. # Removes a box.
def box_remove(name) def box_remove(env, name)
box = Box.find(name) box = Box.find(env, name)
if box.nil? if box.nil?
error_and_exit(:box_remove_doesnt_exist) error_and_exit(:box_remove_doesnt_exist)
return # for tests return # for tests

View File

@ -75,6 +75,11 @@ module Vagrant
File.join(home_path, "tmp") File.join(home_path, "tmp")
end end
# The path to the Vagrant boxes directory
def boxes_path
File.join(home_path, "boxes")
end
#--------------------------------------------------------------- #---------------------------------------------------------------
# Load Methods # Load Methods
#--------------------------------------------------------------- #---------------------------------------------------------------

View File

@ -2,36 +2,40 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper')
class BoxTest < Test::Unit::TestCase class BoxTest < Test::Unit::TestCase
context "class methods" do context "class methods" do
setup do
@env = mock_environment
end
context "listing all boxes" do context "listing all boxes" do
setup do setup do
Dir.stubs(:open) Dir.stubs(:open)
File.stubs(:directory?).returns(true) File.stubs(:directory?).returns(true)
@boxes_path = "foo" @boxes_path = "foo"
Vagrant::Env.stubs(:boxes_path).returns(@boxes_path) @env.stubs(:boxes_path).returns(@boxes_path)
end end
should "open the boxes directory" do should "open the boxes directory" do
Dir.expects(:open).with(Vagrant::Env.boxes_path) Dir.expects(:open).with(@env.boxes_path)
Vagrant::Box.all Vagrant::Box.all(@env)
end end
should "return an array" do should "return an array" do
result = Vagrant::Box.all result = Vagrant::Box.all(@env)
assert result.is_a?(Array) assert result.is_a?(Array)
end end
should "not return the '.' and '..' directories" do should "not return the '.' and '..' directories" do
dir = [".", "..", "..", ".", ".."] dir = [".", "..", "..", ".", ".."]
Dir.expects(:open).yields(dir) Dir.expects(:open).yields(dir)
result = Vagrant::Box.all result = Vagrant::Box.all(@env)
assert result.empty? assert result.empty?
end end
should "return the other directories" do should "return the other directories" do
dir = [".", "foo", "bar", "baz"] dir = [".", "foo", "bar", "baz"]
Dir.expects(:open).yields(dir) Dir.expects(:open).yields(dir)
result = Vagrant::Box.all result = Vagrant::Box.all(@env)
assert_equal ["foo", "bar", "baz"], result assert_equal ["foo", "bar", "baz"], result
end end
@ -44,7 +48,7 @@ class BoxTest < Test::Unit::TestCase
File.expects(:directory?).with(File.join(@boxes_path, dir)).returns(files[index]).in_sequence(dir_sequence) File.expects(:directory?).with(File.join(@boxes_path, dir)).returns(files[index]).in_sequence(dir_sequence)
end end
result = Vagrant::Box.all result = Vagrant::Box.all(@env)
assert_equal ["foo"], result assert_equal ["foo"], result
end end
end end
@ -53,17 +57,17 @@ class BoxTest < Test::Unit::TestCase
setup do setup do
@dir = "foo" @dir = "foo"
@name = "bar" @name = "bar"
Vagrant::Box.stubs(:directory).with(@name).returns(@dir) Vagrant::Box.stubs(:directory).with(@env, @name).returns(@dir)
end end
should "return nil if the box doesn't exist" do should "return nil if the box doesn't exist" do
File.expects(:directory?).with(@dir).once.returns(false) File.expects(:directory?).with(@dir).once.returns(false)
assert_nil Vagrant::Box.find(@name) assert_nil Vagrant::Box.find(@env, @name)
end end
should "return a box object with the proper name set" do should "return a box object with the proper name set" do
File.expects(:directory?).with(@dir).once.returns(true) File.expects(:directory?).with(@dir).once.returns(true)
result = Vagrant::Box.find(@name) result = Vagrant::Box.find(@env, @name)
assert result assert result
assert_equal @name, result.name assert_equal @name, result.name
end end
@ -79,20 +83,21 @@ class BoxTest < Test::Unit::TestCase
box = mock("box") box = mock("box")
box.expects(:name=).with(@name) box.expects(:name=).with(@name)
box.expects(:uri=).with(@uri) box.expects(:uri=).with(@uri)
box.expects(:env=).with(@env)
box.expects(:add).once box.expects(:add).once
Vagrant::Box.expects(:new).returns(box) Vagrant::Box.expects(:new).returns(box)
Vagrant::Box.add(@name, @uri) Vagrant::Box.add(@env, @name, @uri)
end end
end end
context "box directory" do context "box directory" do
setup do setup do
@name = "foo" @name = "foo"
@box_dir = File.join(Vagrant::Env.boxes_path, @name) @box_dir = File.join(@env.boxes_path, @name)
end end
should "return the boxes_path joined with the name" do should "return the boxes_path joined with the name" do
assert_equal @box_dir, Vagrant::Box.directory(@name) assert_equal @box_dir, Vagrant::Box.directory(@env, @name)
end end
end end
end end

View File

@ -251,7 +251,7 @@ class CommandsTest < Test::Unit::TestCase
end end
should "load the environment" do should "load the environment" do
Vagrant::Environment.expects(:load!).once Vagrant::Environment.expects(:load!).once.returns(@env)
Vagrant::Commands.box(["add"]) Vagrant::Commands.box(["add"])
end end
@ -271,7 +271,7 @@ class CommandsTest < Test::Unit::TestCase
end end
should "forward any additional arguments" do should "forward any additional arguments" do
Vagrant::Commands.expects(:box_add).with(1,2,3).once Vagrant::Commands.expects(:box_add).with(@env, 1,2,3).once
Vagrant::Commands.box(["add",1,2,3]) Vagrant::Commands.box(["add",1,2,3])
end end
end end
@ -287,8 +287,8 @@ class CommandsTest < Test::Unit::TestCase
should "call all on box and sort the results" do should "call all on box and sort the results" do
@all = mock("all") @all = mock("all")
@all.expects(:sort).returns(@boxes) @all.expects(:sort).returns(@boxes)
Vagrant::Box.expects(:all).returns(@all) Vagrant::Box.expects(:all).with(@env).returns(@all)
Vagrant::Commands.box_list Vagrant::Commands.box_list(@env)
end end
end end
@ -299,8 +299,8 @@ class CommandsTest < Test::Unit::TestCase
end end
should "execute the add action with the name and path" do should "execute the add action with the name and path" do
Vagrant::Box.expects(:add).with(@name, @path).once Vagrant::Box.expects(:add).with(@env, @name, @path).once
Vagrant::Commands.box_add(@name, @path) Vagrant::Commands.box_add(@env, @name, @path)
end end
end end
@ -312,14 +312,14 @@ class CommandsTest < Test::Unit::TestCase
should "error and exit if the box doesn't exist" do should "error and exit if the box doesn't exist" do
Vagrant::Box.expects(:find).returns(nil) Vagrant::Box.expects(:find).returns(nil)
Vagrant::Commands.expects(:error_and_exit).with(:box_remove_doesnt_exist).once Vagrant::Commands.expects(:error_and_exit).with(:box_remove_doesnt_exist).once
Vagrant::Commands.box_remove(@name) Vagrant::Commands.box_remove(@env, @name)
end end
should "call destroy on the box if it exists" do should "call destroy on the box if it exists" do
@box = mock("box") @box = mock("box")
Vagrant::Box.expects(:find).with(@name).returns(@box) Vagrant::Box.expects(:find).with(@env, @name).returns(@box)
@box.expects(:destroy).once @box.expects(:destroy).once
Vagrant::Commands.box_remove(@name) Vagrant::Commands.box_remove(@env, @name)
end end
end end
end end

View File

@ -119,6 +119,14 @@ class EnvironmentTest < Test::Unit::TestCase
assert_equal File.join("foo", "tmp"), @env.tmp_path assert_equal File.join("foo", "tmp"), @env.tmp_path
end end
end end
context "boxes path" do
should "return the home path joined with 'tmp'" do
home_path = "foo"
@env.stubs(:home_path).returns(home_path)
assert_equal File.join("foo", "boxes"), @env.boxes_path
end
end
end end
context "loading" do context "loading" do