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,6 +4,7 @@ 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
@target_vms ||= begin
if env.multivm? if env.multivm?
return env.vms if !self.name return env.vms if !self.name
vm = env.vms[self.name.to_sym] vm = env.vms[self.name.to_sym]
@ -18,3 +19,4 @@ module Vagrant
end end
end end
end end
end

View File

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

View File

@ -17,6 +17,12 @@ class CommandHelpersTest < Test::Unit::TestCase
@env = mock_environment @env = mock_environment
end 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 context "without multivm" do
setup do setup do
@env.stubs(:vms).returns({ :one => 1 }) @env.stubs(:vms).returns({ :one => 1 })