diff --git a/lib/vagrant/box.rb b/lib/vagrant/box.rb index e93d9ba92..11edf495e 100644 --- a/lib/vagrant/box.rb +++ b/lib/vagrant/box.rb @@ -60,12 +60,12 @@ module Vagrant # Returns an array of all created boxes, as strings. # # @return [Array] - def all + def all(env) results = [] - Dir.open(Env.boxes_path) do |dir| + Dir.open(env.boxes_path) do |dir| 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 end end @@ -79,8 +79,8 @@ module Vagrant # # @param [String] name The name of the box # @return [Box] Instance of {Box} representing the box found - def find(name) - return nil unless File.directory?(directory(name)) + def find(env, name) + return nil unless File.directory?(directory(env, name)) new(name) end @@ -90,10 +90,11 @@ module Vagrant # # @param [String] name The name of the box # @param [String] uri URI to the box file - def add(name, uri) + def add(env, name, uri) box = new box.name = name box.uri = uri + box.env = env box.add end @@ -103,7 +104,7 @@ module Vagrant # # @param [String] name Name of the box whose directory you're interested in. # @return [String] Full path to the box directory. - def directory(name) + def directory(env, name) File.join(Env.boxes_path, name) end end diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index 3e5d42cd7..e77bdfd2e 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -169,7 +169,7 @@ msg # which action to take and calls the respective action method # (see {box_add} and {box_remove}) def box(argv) - Environment.load! + env = Environment.load! sub_commands = ["list", "add", "remove"] @@ -177,12 +177,12 @@ msg error_and_exit(:command_box_invalid) end - send("box_#{argv[0]}", *argv[1..-1]) + send("box_#{argv[0]}", env, *argv[1..-1]) end # Lists all added boxes - def box_list - boxes = Box.all.sort + def box_list(env) + boxes = Box.all(env).sort wrap_output do if !boxes.empty? @@ -197,13 +197,13 @@ msg end # Adds a box to the local filesystem, given a URI. - def box_add(name, path) - Box.add(name, path) + def box_add(env, name, path) + Box.add(env, name, path) end # Removes a box. - def box_remove(name) - box = Box.find(name) + def box_remove(env, name) + box = Box.find(env, name) if box.nil? error_and_exit(:box_remove_doesnt_exist) return # for tests diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index c108eef77..4a4ef1b9b 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -75,6 +75,11 @@ module Vagrant File.join(home_path, "tmp") end + # The path to the Vagrant boxes directory + def boxes_path + File.join(home_path, "boxes") + end + #--------------------------------------------------------------- # Load Methods #--------------------------------------------------------------- diff --git a/test/vagrant/box_test.rb b/test/vagrant/box_test.rb index 8a0d20a7d..b2fce27a7 100644 --- a/test/vagrant/box_test.rb +++ b/test/vagrant/box_test.rb @@ -2,36 +2,40 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper') class BoxTest < Test::Unit::TestCase context "class methods" do + setup do + @env = mock_environment + end + 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) + @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 + Dir.expects(:open).with(@env.boxes_path) + Vagrant::Box.all(@env) end should "return an array" do - result = Vagrant::Box.all + 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 + 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 + result = Vagrant::Box.all(@env) assert_equal ["foo", "bar", "baz"], result 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) end - result = Vagrant::Box.all + result = Vagrant::Box.all(@env) assert_equal ["foo"], result end end @@ -53,17 +57,17 @@ class BoxTest < Test::Unit::TestCase setup do @dir = "foo" @name = "bar" - Vagrant::Box.stubs(:directory).with(@name).returns(@dir) + Vagrant::Box.stubs(:directory).with(@env, @name).returns(@dir) end should "return nil if the box doesn't exist" do File.expects(:directory?).with(@dir).once.returns(false) - assert_nil Vagrant::Box.find(@name) + assert_nil Vagrant::Box.find(@env, @name) end should "return a box object with the proper name set" do File.expects(:directory?).with(@dir).once.returns(true) - result = Vagrant::Box.find(@name) + result = Vagrant::Box.find(@env, @name) assert result assert_equal @name, result.name end @@ -79,20 +83,21 @@ class BoxTest < Test::Unit::TestCase 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.add(@name, @uri) + Vagrant::Box.add(@env, @name, @uri) end end context "box directory" do setup do @name = "foo" - @box_dir = File.join(Vagrant::Env.boxes_path, @name) + @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(@name) + assert_equal @box_dir, Vagrant::Box.directory(@env, @name) end end end diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index 0f028f1a8..40415951d 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -251,7 +251,7 @@ class CommandsTest < Test::Unit::TestCase end should "load the environment" do - Vagrant::Environment.expects(:load!).once + Vagrant::Environment.expects(:load!).once.returns(@env) Vagrant::Commands.box(["add"]) end @@ -271,7 +271,7 @@ class CommandsTest < Test::Unit::TestCase end 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]) end end @@ -287,8 +287,8 @@ class CommandsTest < Test::Unit::TestCase 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 + Vagrant::Box.expects(:all).with(@env).returns(@all) + Vagrant::Commands.box_list(@env) end end @@ -299,8 +299,8 @@ class CommandsTest < Test::Unit::TestCase end should "execute the add action with the name and path" do - Vagrant::Box.expects(:add).with(@name, @path).once - Vagrant::Commands.box_add(@name, @path) + Vagrant::Box.expects(:add).with(@env, @name, @path).once + Vagrant::Commands.box_add(@env, @name, @path) end end @@ -312,14 +312,14 @@ class CommandsTest < Test::Unit::TestCase should "error and exit if the box doesn't exist" do Vagrant::Box.expects(:find).returns(nil) Vagrant::Commands.expects(:error_and_exit).with(:box_remove_doesnt_exist).once - Vagrant::Commands.box_remove(@name) + Vagrant::Commands.box_remove(@env, @name) end should "call destroy on the box if it exists" do @box = mock("box") - Vagrant::Box.expects(:find).with(@name).returns(@box) + Vagrant::Box.expects(:find).with(@env, @name).returns(@box) @box.expects(:destroy).once - Vagrant::Commands.box_remove(@name) + Vagrant::Commands.box_remove(@env, @name) end end end diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index 723426dcb..c3a33ff38 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -119,6 +119,14 @@ class EnvironmentTest < Test::Unit::TestCase assert_equal File.join("foo", "tmp"), @env.tmp_path 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 context "loading" do