From 8a6bdbf710318688669c945eb277959489045953 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 14 Mar 2014 15:10:21 -0700 Subject: [PATCH] core: Environment#machine_index --- lib/vagrant.rb | 1 + lib/vagrant/environment.rb | 20 +++++++++++++++++--- test/unit/vagrant/environment_test.rb | 23 +++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/lib/vagrant.rb b/lib/vagrant.rb index c714e0c55..26ed01820 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -97,6 +97,7 @@ module Vagrant autoload :Guest, 'vagrant/guest' autoload :Host, 'vagrant/host' autoload :Machine, 'vagrant/machine' + autoload :MachineIndex, 'vagrant/machine_index' autoload :MachineState, 'vagrant/machine_state' autoload :Plugin, 'vagrant/plugin' autoload :UI, 'vagrant/ui' diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 8a2efbb6e..771c6234f 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -121,6 +121,7 @@ module Vagrant @data_dir = @home_path.join("data") @gems_path = @home_path.join("gems") @tmp_path = @home_path.join("tmp") + @machine_index_dir = @data_dir.join("machine-index") # Prepare the directories setup_home_path @@ -417,6 +418,13 @@ module Vagrant name, provider, boxes, machine_data_path, self) end + # The {MachineIndex} to store information about the machines. + # + # @return [MachineIndex] + def machine_index + @machine_index ||= MachineIndex.new(@machine_index_dir) + end + # This returns a list of the configured machines for this environment. # Each of the names returned by this method is valid to be used with # the {#machine} method. @@ -508,9 +516,15 @@ module Vagrant # Setup the list of child directories that need to be created if they # don't already exist. - dirs = [@home_path] - subdirs = ["boxes", "data", "gems", "rgloader", "tmp"] - dirs += subdirs.collect { |subdir| @home_path.join(subdir) } + dirs = [ + @home_path, + @home_path.join("rgloader"), + @boxes_path, + @data_dir, + @gems_path, + @tmp_path, + @machine_index_dir, + ] # Go through each required directory, creating it if it doesn't exist dirs.each do |dir| diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index f32f497ea..eeac9ea99 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -558,6 +558,29 @@ VF end end + describe "#machine_index" do + it "returns a machine index" do + expect(subject.machine_index).to be_kind_of(Vagrant::MachineIndex) + end + + it "caches the result" do + result = subject.machine_index + expect(subject.machine_index).to equal(result) + end + + it "uses a directory within the home directory by default" do + klass = double("machine_index") + stub_const("Vagrant::MachineIndex", klass) + + klass.should_receive(:new).with do |path| + expect(path.to_s.start_with?(subject.home_path.to_s)).to be_true + true + end + + subject.machine_index + end + end + describe "active machines" do it "should be empty if the machines folder doesn't exist" do folder = instance.local_data_path.join("machines")