From 90aaf5cb5e4fe250f0c63307360430dae42a59c3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 15 Sep 2010 08:38:56 -0600 Subject: [PATCH] Command helpers `target_vms` now takes an optional string for the VM name --- lib/vagrant/command/helpers.rb | 12 +++++++----- test/vagrant/command/helpers_test.rb | 5 +++++ 2 files changed, 12 insertions(+), 5 deletions(-) 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) {