Remove unnecessary class methods on Box class. Move commands over to use "boxes" on env

This commit is contained in:
Mitchell Hashimoto 2010-09-11 10:27:06 -07:00
parent 761da0de63
commit 2026bb0b1c
3 changed files with 16 additions and 111 deletions

View File

@ -15,25 +15,9 @@ module Vagrant
# The environment which this box belongs to. Although this could # The environment which this box belongs to. Although this could
# actually be many environments, this points to the environment # actually be many environments, this points to the environment
# of a specific instance. # of a specific instance.
attr_accessor :env attr_reader :env
class << self class << self
# Returns an array of all created boxes, as strings.
#
# @return [Array<String>]
def all(env)
results = []
Dir.open(env.boxes_path) do |dir|
dir.each do |d|
next if d == "." || d == ".." || !File.directory?(File.join(env.boxes_path, d))
results << d.to_s
end
end
results
end
# Adds a new box with given name from the given URI. This method # Adds a new box with given name from the given URI. This method
# begins the process of adding a box from a given URI by setting up # begins the process of adding a box from a given URI by setting up
# the {Box} instance and calling {#add}. # the {Box} instance and calling {#add}.
@ -41,22 +25,10 @@ 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(env, name, uri) def add(env, name, uri)
box = new box = new(env, name)
box.name = name
box.uri = uri box.uri = uri
box.env = env
box.add box.add
end end
# Returns the directory to a box of the given name. The name given
# as a parameter is not checked for existence; this method simply
# returns the directory which would be used if the box did exist.
#
# @param [String] name Name of the box whose directory you're interested in.
# @return [String] Full path to the box directory.
def directory(env, name)
File.join(env.boxes_path, name)
end
end end
# Creates a new box instance. Given an optional `name` parameter, # Creates a new box instance. Given an optional `name` parameter,
@ -76,7 +48,7 @@ module Vagrant
# #
# @return [String] # @return [String]
def ovf_file def ovf_file
File.join(directory, env.config.vm.box_ovf) directory.join(env.config.vm.box_ovf)
end end
# Begins the process of adding a box to the vagrant installation. This # Begins the process of adding a box to the vagrant installation. This
@ -101,7 +73,13 @@ module Vagrant
# #
# @return [String] # @return [String]
def directory def directory
self.class.directory(env, self.name) env.boxes_path.join(name)
end
# Implemented for comparison with other boxes.
def <=>(other)
return super if !other.is_a?(self.class)
name <=> other.name
end end
end end
end end

View File

@ -24,9 +24,9 @@ module Vagrant
desc "list", "Lists all installed boxes" desc "list", "Lists all installed boxes"
def list def list
boxes = Box.all(env).sort boxes = env.boxes.sort
return env.ui.warn("vagrant.commands.box.no_installed_boxes", :_prefix => false) if boxes.empty? return env.ui.warn("vagrant.commands.box.no_installed_boxes", :_prefix => false) if boxes.empty?
boxes.each { |b| env.ui.info(b, :_translate => false, :_prefix => false) } boxes.each { |b| env.ui.info(b.name, :_translate => false, :_prefix => false) }
end end
end end
end end

View File

@ -6,53 +6,6 @@ class BoxTest < Test::Unit::TestCase
@env = vagrant_env @env = vagrant_env
end end
context "listing all boxes" do
setup do
Dir.stubs(:open)
File.stubs(:directory?).returns(true)
@boxes_path = "foo"
@env.stubs(:boxes_path).returns(@boxes_path)
end
should "open the boxes directory" do
Dir.expects(:open).with(@env.boxes_path)
Vagrant::Box.all(@env)
end
should "return an array" do
result = Vagrant::Box.all(@env)
assert result.is_a?(Array)
end
should "not return the '.' and '..' directories" do
dir = [".", "..", "..", ".", ".."]
Dir.expects(:open).yields(dir)
result = Vagrant::Box.all(@env)
assert result.empty?
end
should "return the other directories" do
dir = [".", "foo", "bar", "baz"]
Dir.expects(:open).yields(dir)
result = Vagrant::Box.all(@env)
assert_equal ["foo", "bar", "baz"], result
end
should "ignore the files" do
dir = ["foo", "bar"]
files = [true, false]
Dir.expects(:open).yields(dir)
dir_sequence = sequence("directory")
dir.each_with_index do |value, index|
File.expects(:directory?).with(File.join(@boxes_path, value)).returns(files[index]).in_sequence(dir_sequence)
end
result = Vagrant::Box.all(@env)
assert_equal ["foo"], result
end
end
context "adding" do context "adding" do
setup do setup do
@name = "foo" @name = "foo"
@ -61,31 +14,17 @@ class BoxTest < Test::Unit::TestCase
should "create a new instance, set the variables, and add it" do should "create a new instance, set the variables, and add it" do
box = mock("box") box = mock("box")
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).with(@env, @name).returns(box)
Vagrant::Box.add(@env, @name, @uri) Vagrant::Box.add(@env, @name, @uri)
end end
end end
context "box directory" do
setup do
@name = "foo"
@box_dir = File.join(@env.boxes_path, @name)
end
should "return the boxes_path joined with the name" do
assert_equal @box_dir, Vagrant::Box.directory(@env, @name)
end
end
end end
context "instance methods" do context "instance methods" do
setup do setup do
@box = Vagrant::Box.new @box = Vagrant::Box.new(vagrant_env, "foo")
@box.env = vagrant_env
end end
should "execute the Add action when add is called" do should "execute the Add action when add is called" do
@ -94,14 +33,8 @@ class BoxTest < Test::Unit::TestCase
end end
context "box directory" do context "box directory" do
setup do
@box.name = "foo"
end
should "return the boxes_path joined with the name" do should "return the boxes_path joined with the name" do
result = mock("object") assert_equal @box.env.boxes_path.join(@box.name), @box.directory
Vagrant::Box.expects(:directory).with(@box.env, @box.name).returns(result)
assert result.equal?(@box.directory)
end end
end end
@ -125,14 +58,8 @@ class BoxTest < Test::Unit::TestCase
end end
context "ovf file" do context "ovf file" do
setup do
@box.stubs(:directory).returns("foo")
@box.env.config.vm.box_ovf = "foo.ovf"
end
should "be the directory joined with the config ovf file" do should "be the directory joined with the config ovf file" do
assert_equal File.join(@box.directory, @box.env.config.vm.box_ovf), @box.ovf_file assert_equal @box.directory.join(@box.env.config.vm.box_ovf), @box.ovf_file
end end
end end
end end