Command helpers `target_vms` now takes an optional string for the VM name

This commit is contained in:
Mitchell Hashimoto 2010-09-15 08:38:56 -06:00
parent d5fbf29ec0
commit 90aaf5cb5e
2 changed files with 12 additions and 5 deletions

View File

@ -11,14 +11,16 @@ module Vagrant
# This returns an array of {VM} objects depending on the arguments # This returns an array of {VM} objects depending on the arguments
# given to the command. # given to the command.
def target_vms def target_vms(name=nil)
name ||= self.name
@target_vms ||= begin @target_vms ||= begin
if env.multivm? if env.multivm?
return env.vms.values if !self.name return env.vms.values if !name
vm = env.vms[self.name.to_sym] vm = env.vms[name.to_sym]
raise Errors::VMNotFoundError.new(:name => self.name) if !vm raise Errors::VMNotFoundError.new(:name => name) if !vm
else else
raise Errors::MultiVMEnvironmentRequired.new if self.name raise Errors::MultiVMEnvironmentRequired.new if name
vm = env.vms.values.first vm = env.vms.values.first
end end

View File

@ -71,6 +71,11 @@ class CommandHelpersTest < Test::Unit::TestCase
assert_equal @env.vms[:one], instance.target_vms.first assert_equal @env.vms[:one], instance.target_vms.first
end 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 should "raise an exception if an invalid name is given" do
instance = command(["foo"], @env) instance = command(["foo"], @env)
assert_raises(Vagrant::Errors::VMNotFoundError) { assert_raises(Vagrant::Errors::VMNotFoundError) {