`vagrant down` command and associated actions now use the new environment

This commit is contained in:
Mitchell Hashimoto 2010-03-19 16:08:29 -07:00
parent 59ae16998d
commit b5daf5ae86
8 changed files with 52 additions and 10 deletions

View File

@ -15,7 +15,7 @@ module Vagrant
end
def depersist
Env.depersist_vm(@runner)
@runner.env.depersist_vm
end
end
end

View File

@ -51,7 +51,9 @@ module Vagrant
# only be used internally.
attr_accessor :temp_path
# The environment which this box belongs to
# 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
class <<self

View File

@ -90,9 +90,9 @@ msg
# This command requires that an instance already be brought up with
# `vagrant up`.
def down
Env.load!
Env.require_persisted_vm
Env.persisted_vm.destroy
env = Environment.load!
env.require_persisted_vm
env.vm.destroy
end
# Reload the environment. This is almost equivalent to the {up} command

View File

@ -164,7 +164,7 @@ module Vagrant
File.open(dotfile_path) do |f|
@vm = Vagrant::VM.find(f.read)
@vm.env = self
@vm.env = self if @vm
end
rescue Errno::ENOENT
@vm = nil
@ -232,7 +232,7 @@ module Vagrant
def require_persisted_vm
require_root_path
error_and_exit(:environment_not_created) if !persisted_vm
error_and_exit(:environment_not_created) if !vm
end
end
end

View File

@ -138,6 +138,7 @@ class Test::Unit::TestCase
mock_vm.stubs(:invoke_callback)
mock_vm.stubs(:invoke_around_callback).yields
mock_vm.stubs(:actions).returns([action])
mock_vm.stubs(:env).returns(mock_environment)
[mock_vm, vm, action]
end

View File

@ -3,7 +3,6 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
class DestroyActionTest < Test::Unit::TestCase
setup do
@runner, @vm, @action = mock_action(Vagrant::Actions::VM::Destroy)
mock_config
end
context "executing" do
@ -30,7 +29,7 @@ class DestroyActionTest < Test::Unit::TestCase
context "depersisting" do
should "call depersist_vm on Env" do
Vagrant::Env.expects(:depersist_vm).with(@runner).once
@runner.env.expects(:depersist_vm).once
@action.depersist
end
end

View File

@ -8,6 +8,11 @@ class CommandsTest < Test::Unit::TestCase
@persisted_vm.stubs(:execute!)
Vagrant::Env.stubs(:persisted_vm).returns(@persisted_vm)
Vagrant::Env.stubs(:require_persisted_vm)
@env = mock_environment
@env.stubs(:vm).returns(@persisted_vm)
@env.stubs(:require_persisted_vm)
Vagrant::Environment.stubs(:load!).returns(@env)
end
context "init" do
@ -82,8 +87,13 @@ class CommandsTest < Test::Unit::TestCase
@persisted_vm.stubs(:destroy)
end
should "load the current environment" do
Vagrant::Environment.expects(:load!).once.returns(@env)
Vagrant::Commands.down
end
should "require a persisted VM" do
Vagrant::Env.expects(:require_persisted_vm).once
@env.expects(:require_persisted_vm).once
Vagrant::Commands.down
end

View File

@ -358,6 +358,17 @@ class EnvironmentTest < Test::Unit::TestCase
assert_equal vm, @env.vm
end
should "not set the environment if the VM is nil" do
filemock = mock("filemock")
filemock.expects(:read).returns("foo")
Vagrant::VM.expects(:find).with("foo").returns(nil)
File.expects(:open).with(@env.dotfile_path).once.yields(filemock)
File.expects(:file?).with(@env.dotfile_path).once.returns(true)
assert_nothing_raised { @env.load_vm! }
assert_nil @env.vm
end
should "do nothing if the root path is nil" do
File.expects(:open).never
@env.stubs(:root_path).returns(nil)
@ -449,6 +460,25 @@ class EnvironmentTest < Test::Unit::TestCase
@env.require_root_path
end
end
context "requiring a persisted VM" do
setup do
@env.stubs(:vm).returns("foo")
@env.stubs(:require_root_path)
end
should "require a root path" do
@env.expects(:require_root_path).once
@env.expects(:error_and_exit).never
@env.require_persisted_vm
end
should "error and exit if the VM is not set" do
@env.expects(:vm).returns(nil)
@env.expects(:error_and_exit).once
@env.require_persisted_vm
end
end
end
context "managing VM" do