diff --git a/lib/vagrant/box.rb b/lib/vagrant/box.rb index d0172ec5a..bbc63b96d 100644 --- a/lib/vagrant/box.rb +++ b/lib/vagrant/box.rb @@ -52,6 +52,22 @@ module Vagrant attr_accessor :temp_path class <] + 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. diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index 33dbc3fd4..40c00c8e0 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -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) diff --git a/lib/vagrant/util.rb b/lib/vagrant/util.rb index 6db652c5d..13ca25b1a 100644 --- a/lib/vagrant/util.rb +++ b/lib/vagrant/util.rb @@ -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 ===================================================================== diff --git a/test/vagrant/box_test.rb b/test/vagrant/box_test.rb index 93effccf1..8a0d20a7d 100644 --- a/test/vagrant/box_test.rb +++ b/test/vagrant/box_test.rb @@ -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" diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index d986eb3cf..6ae094f73 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -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"