Only calculate the target VMs once

This commit is contained in:
Mitchell Hashimoto 2010-08-24 17:50:41 -07:00
parent 2eb09c7aa2
commit ccad6af8cf
3 changed files with 19 additions and 12 deletions

View File

@ -4,16 +4,18 @@ module Vagrant
# This returns an array of {VM} objects depending on the arguments
# given to the command.
def target_vms
if env.multivm?
return env.vms if !self.name
vm = env.vms[self.name.to_sym]
raise VMNotFoundError.new("A VM by the name of `#{self.name}` was not found.") if !vm
else
raise MultiVMEnvironmentRequired.new("A multi-vm environment is required for name specification to a command.") if self.name
vm = env.vms.values.first
end
@target_vms ||= begin
if env.multivm?
return env.vms if !self.name
vm = env.vms[self.name.to_sym]
raise VMNotFoundError.new("A VM by the name of `#{self.name}` was not found.") if !vm
else
raise MultiVMEnvironmentRequired.new("A multi-vm environment is required for name specification to a command.") if self.name
vm = env.vms.values.first
end
[vm]
[vm]
end
end
end
end

View File

@ -10,9 +10,8 @@ module Vagrant
end
def route
vms = target_vms
show_multivm if vms.length > 1
show_single(vms.first)
show_multivm if target_vms.length > 1
show_single(target_vms.first)
end
protected

View File

@ -17,6 +17,12 @@ class CommandHelpersTest < Test::Unit::TestCase
@env = mock_environment
end
should "only calculate the result once" do
instance = command([], @env)
result = instance.target_vms
assert instance.target_vms.equal?(result)
end
context "without multivm" do
setup do
@env.stubs(:vms).returns({ :one => 1 })