diff --git a/lib/vagrant/command/helpers.rb b/lib/vagrant/command/helpers.rb index 05f80f3d6..3873edbee 100644 --- a/lib/vagrant/command/helpers.rb +++ b/lib/vagrant/command/helpers.rb @@ -11,14 +11,16 @@ module Vagrant # This returns an array of {VM} objects depending on the arguments # given to the command. - def target_vms + def target_vms(name=nil) + name ||= self.name + @target_vms ||= begin if env.multivm? - return env.vms.values if !self.name - vm = env.vms[self.name.to_sym] - raise Errors::VMNotFoundError.new(:name => self.name) if !vm + return env.vms.values if !name + vm = env.vms[name.to_sym] + raise Errors::VMNotFoundError.new(:name => name) if !vm else - raise Errors::MultiVMEnvironmentRequired.new if self.name + raise Errors::MultiVMEnvironmentRequired.new if name vm = env.vms.values.first end diff --git a/test/vagrant/command/helpers_test.rb b/test/vagrant/command/helpers_test.rb index 8651c6cbf..381f4e05a 100644 --- a/test/vagrant/command/helpers_test.rb +++ b/test/vagrant/command/helpers_test.rb @@ -71,6 +71,11 @@ class CommandHelpersTest < Test::Unit::TestCase assert_equal @env.vms[:one], instance.target_vms.first end + should "return only the specified VM if name is given in the arg" do + instance = command([], @env) + assert_equal @env.vms[:two], instance.target_vms("two").first + end + should "raise an exception if an invalid name is given" do instance = command(["foo"], @env) assert_raises(Vagrant::Errors::VMNotFoundError) {