diff --git a/lib/vagrant/box.rb b/lib/vagrant/box.rb index 37acdad39..437e05fc9 100644 --- a/lib/vagrant/box.rb +++ b/lib/vagrant/box.rb @@ -15,25 +15,9 @@ module Vagrant # The environment which this box belongs to. Although this could # actually be many environments, this points to the environment # of a specific instance. - attr_accessor :env + attr_reader :env class << self - # Returns an array of all created boxes, as strings. - # - # @return [Array] - 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 # begins the process of adding a box from a given URI by setting up # the {Box} instance and calling {#add}. @@ -41,22 +25,10 @@ module Vagrant # @param [String] name The name of the box # @param [String] uri URI to the box file def add(env, name, uri) - box = new - box.name = name + box = new(env, name) box.uri = uri - box.env = env box.add 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 # Creates a new box instance. Given an optional `name` parameter, @@ -76,7 +48,7 @@ module Vagrant # # @return [String] def ovf_file - File.join(directory, env.config.vm.box_ovf) + directory.join(env.config.vm.box_ovf) end # Begins the process of adding a box to the vagrant installation. This @@ -101,7 +73,13 @@ module Vagrant # # @return [String] 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 diff --git a/lib/vagrant/command/box.rb b/lib/vagrant/command/box.rb index 76f77320b..b8398483e 100644 --- a/lib/vagrant/command/box.rb +++ b/lib/vagrant/command/box.rb @@ -24,9 +24,9 @@ module Vagrant desc "list", "Lists all installed boxes" 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? - 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 diff --git a/test/vagrant/box_test.rb b/test/vagrant/box_test.rb index c2ddaa74f..64704c11e 100644 --- a/test/vagrant/box_test.rb +++ b/test/vagrant/box_test.rb @@ -6,53 +6,6 @@ class BoxTest < Test::Unit::TestCase @env = vagrant_env 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 setup do @name = "foo" @@ -61,31 +14,17 @@ class BoxTest < Test::Unit::TestCase should "create a new instance, set the variables, and add it" do box = mock("box") - box.expects(:name=).with(@name) box.expects(:uri=).with(@uri) - box.expects(:env=).with(@env) 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) 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 context "instance methods" do setup do - @box = Vagrant::Box.new - @box.env = vagrant_env + @box = Vagrant::Box.new(vagrant_env, "foo") end should "execute the Add action when add is called" do @@ -94,14 +33,8 @@ class BoxTest < Test::Unit::TestCase 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.env, @box.name).returns(result) - assert result.equal?(@box.directory) + assert_equal @box.env.boxes_path.join(@box.name), @box.directory end end @@ -125,14 +58,8 @@ class BoxTest < Test::Unit::TestCase end 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 - 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