Added `vagrant box list` command to list added boxes

This commit is contained in:
Mitchell Hashimoto 2010-03-07 21:34:53 -08:00
parent 93d3b00fa9
commit efe98df4b0
5 changed files with 102 additions and 1 deletions

View File

@ -52,6 +52,22 @@ module Vagrant
attr_accessor :temp_path
class <<self
# Returns an array of all created boxes, as strings.
#
# @return [Array<String>]
def all
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
# Finds a box with the given name. This method searches for a box
# with the given name, returning `nil` if none is found or returning
# a {Box} instance otherwise.

View File

@ -130,7 +130,7 @@ error
def box(argv)
Env.load!(:suppress_errors => true)
sub_commands = ["add", "remove"]
sub_commands = ["list", "add", "remove"]
if !sub_commands.include?(argv[0])
error_and_exit(<<-error)
@ -145,6 +145,22 @@ error
send("box_#{argv[0]}", *argv[1..-1])
end
# Lists all added boxes
def box_list
boxes = Box.all.sort
wrap_output do
if !boxes.empty?
puts "Installed Vagrant Boxes:\n\n"
boxes.each do |box|
puts box
end
else
puts "No Vagrant Boxes Added!"
end
end
end
# Adds a box to the local filesystem, given a URI.
def box_add(name, path)
Box.add(name, path)

View File

@ -4,6 +4,12 @@ module Vagrant
base.extend Vagrant::Util
end
def wrap_output
puts "====================================================================="
yield
puts "====================================================================="
end
def error_and_exit(error)
abort <<-error
=====================================================================

View File

@ -2,6 +2,53 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper')
class BoxTest < Test::Unit::TestCase
context "class methods" do
context "listing all boxes" do
setup do
Dir.stubs(:open)
File.stubs(:directory?).returns(true)
@boxes_path = "foo"
Vagrant::Env.stubs(:boxes_path).returns(@boxes_path)
end
should "open the boxes directory" do
Dir.expects(:open).with(Vagrant::Env.boxes_path)
Vagrant::Box.all
end
should "return an array" do
result = Vagrant::Box.all
assert result.is_a?(Array)
end
should "not return the '.' and '..' directories" do
dir = [".", "..", "..", ".", ".."]
Dir.expects(:open).yields(dir)
result = Vagrant::Box.all
assert result.empty?
end
should "return the other directories" do
dir = [".", "foo", "bar", "baz"]
Dir.expects(:open).yields(dir)
result = Vagrant::Box.all
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 |dir, index|
File.expects(:directory?).with(File.join(@boxes_path, dir)).returns(files[index]).in_sequence(dir_sequence)
end
result = Vagrant::Box.all
assert_equal ["foo"], result
end
end
context "finding" do
setup do
@dir = "foo"

View File

@ -219,6 +219,22 @@ class CommandsTest < Test::Unit::TestCase
end
end
context "box list" do
setup do
@boxes = ["foo", "bar"]
Vagrant::Box.stubs(:all).returns(@boxes)
Vagrant::Commands.stubs(:puts)
end
should "call all on box and sort the results" do
@all = mock("all")
@all.expects(:sort).returns(@boxes)
Vagrant::Box.expects(:all).returns(@all)
Vagrant::Commands.box_list
end
end
context "box add" do
setup do
@name = "foo"