From cfc87d4ce36ac34103b7740edd35263cff76b7ca Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 5 Jul 2010 19:41:31 +0200 Subject: [PATCH] Halt checks if VM is running. No more error messages --- lib/vagrant/action/vm/halt.rb | 12 ++++++------ templates/strings.yml | 2 -- test/vagrant/action/vm/halt_test.rb | 18 +++++++++--------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/vagrant/action/vm/halt.rb b/lib/vagrant/action/vm/halt.rb index 4958a3e6c..38b806d5c 100644 --- a/lib/vagrant/action/vm/halt.rb +++ b/lib/vagrant/action/vm/halt.rb @@ -7,13 +7,13 @@ module Vagrant end def call(env) - return env.error!(:vm_not_running) unless env["vm"].vm.running? + if env["vm"].vm.running? + env["vm"].system.halt if !env["force"] - env["vm"].system.halt if !env["force"] - - if env["vm"].vm.state(true) != :powered_off - env.logger.info "Forcing shutdown of VM..." - env["vm"].vm.stop + if env["vm"].vm.state(true) != :powered_off + env.logger.info "Forcing shutdown of VM..." + env["vm"].vm.stop + end end @app.call(env) diff --git a/templates/strings.yml b/templates/strings.yml index 674b5c6cf..f2fc149c8 100644 --- a/templates/strings.yml +++ b/templates/strings.yml @@ -250,8 +250,6 @@ Failed to connect to VM! Failed to boot? :vm_base_not_found: |- The specified base VM "<%= name %>" was not found. -:vm_not_running: |- - VM is not running! Nothing to shut down! :vm_not_running_for_suspend: |- The vagrant virtual environment you are trying to suspend must be running to be suspended. :vm_not_suspended: |- diff --git a/test/vagrant/action/vm/halt_test.rb b/test/vagrant/action/vm/halt_test.rb index 4abb92ea9..75641fbae 100644 --- a/test/vagrant/action/vm/halt_test.rb +++ b/test/vagrant/action/vm/halt_test.rb @@ -26,6 +26,15 @@ class HaltVMActionTest < Test::Unit::TestCase @internal_vm.stubs(:state).returns(:powered_off) end + should "do nothing if VM not running" do + @internal_vm.stubs(:running?).returns(false) + @vm.system.expects(:halt).never + @internal_vm.expects(:stop).never + @app.expects(:call).once + + @instance.call(@env) + end + should "halt with the system and NOT force VM to stop if powered off" do @internal_vm.expects(:state).with(true).returns(:powered_off) @vm.system.expects(:halt).once @@ -49,14 +58,5 @@ class HaltVMActionTest < Test::Unit::TestCase @vm.system.expects(:halt).never @instance.call(@env) end - - should "raise an ActionException if VM is not running" do - @internal_vm.stubs(:running?).returns(false) - @internal_vm.expects(:stop).never - @app.expects(:call).never - @instance.call(@env) - assert @env.error? - assert_equal :vm_not_running, @env.error.first - end end end