From 8ad8f738460a8437767f6ae628fc4a06ef0e0620 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 21 Jan 2013 11:31:20 -0600 Subject: [PATCH] Raise an exception if a non-MachineState is returned --- lib/vagrant/errors.rb | 4 ++++ lib/vagrant/machine.rb | 4 +++- templates/locales/en.yml | 6 ++++++ test/unit/vagrant/machine_test.rb | 10 ++++++++-- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index f081000bd..8b9e8833a 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -248,6 +248,10 @@ module Vagrant error_key(:machine_not_found) end + class MachineStateInvalid < VagrantError + error_key(:machine_state_invalid) + end + class MultiVMEnvironmentRequired < VagrantError status_code(5) error_key(:multi_vm_required) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index 605179a45..cc7802790 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -277,7 +277,9 @@ module Vagrant # # @return [Symbol] def state - @provider.state + result = @provider.state + raise Errors::MachineStateInvalid if !result.is_a?(MachineState) + result end protected diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 3c00b3a9a..40c98bccc 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -145,6 +145,12 @@ en: machine_not_found: |- The machine with the name '%{name}' was not found configured for this Vagrant environment. + machine_state_invalid: |- + An internal error has occurred! The provider of the machine you're + trying to work with reported an invalid state. This is a bug with + the provider you're using, and not with Vagrant itself or with + any configuration you may have done. Please report this bug to + the proper location. multi_vm_required: |- A multi-vm environment is required for name specification to this command. multi_vm_target_required: |- diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index aa60ab8b4..1009fbfb9 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -385,10 +385,16 @@ describe Vagrant::Machine do describe "state" do it "should query state from the provider" do - state = :running + state = Vagrant::MachineState.new(:id, "short", "long") provider.should_receive(:state).and_return(state) - instance.state.should == state + instance.state.id.should == :id + end + + it "should raise an exception if a MachineState is not returned" do + provider.should_receive(:state).and_return(:old_school) + expect { instance.state }. + to raise_error(Vagrant::Errors::MachineStateInvalid) end end end