From ccad6af8cfa7724126f0f3747c20a4666051b8db Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 24 Aug 2010 17:50:41 -0700 Subject: [PATCH] Only calculate the target VMs once --- lib/vagrant/command/helpers.rb | 20 +++++++++++--------- lib/vagrant/command/status.rb | 5 ++--- test/vagrant/command/helpers_test.rb | 6 ++++++ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/vagrant/command/helpers.rb b/lib/vagrant/command/helpers.rb index 36554ea91..d56640b6c 100644 --- a/lib/vagrant/command/helpers.rb +++ b/lib/vagrant/command/helpers.rb @@ -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 diff --git a/lib/vagrant/command/status.rb b/lib/vagrant/command/status.rb index 5468de18e..7232ec7ed 100644 --- a/lib/vagrant/command/status.rb +++ b/lib/vagrant/command/status.rb @@ -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 diff --git a/test/vagrant/command/helpers_test.rb b/test/vagrant/command/helpers_test.rb index ece67bc30..606b269eb 100644 --- a/test/vagrant/command/helpers_test.rb +++ b/test/vagrant/command/helpers_test.rb @@ -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 })