From e985308e9a2df16fe3281ef335f5223523136f39 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 22 Apr 2014 17:09:44 -0700 Subject: [PATCH] core: MachineIndex::Entry#valid? method and tests --- lib/vagrant/machine_index.rb | 27 +++++++++ plugins/commands/global-status/command.rb | 19 +----- test/unit/vagrant/machine_index_test.rb | 71 +++++++++++++++++++++++ 3 files changed, 99 insertions(+), 18 deletions(-) diff --git a/lib/vagrant/machine_index.rb b/lib/vagrant/machine_index.rb index 0ebd5c780..bbe99f0d7 100644 --- a/lib/vagrant/machine_index.rb +++ b/lib/vagrant/machine_index.rb @@ -403,6 +403,33 @@ module Vagrant @vagrantfile_path = Pathname.new(@vagrantfile_path) if @vagrantfile_path end + # Returns boolean true if this entry appears to be valid. + # The critera for being valid: + # + # * Vagrantfile directory exists + # * Vagrant environment contains a machine with this + # name and provider. + # + # This method is _slow_. It should be used with care. + # + # @param [Pathname] home_path The home path for the Vagrant + # environment. + # @return [Boolean] + def valid?(home_path) + return false if !vagrantfile_path + return false if !vagrantfile_path.directory? + + # Create an environment so we can determine the active + # machines... + env = vagrant_env(home_path) + env.active_machines.each do |name, provider| + return true if name.to_s == self.name.to_s && + provider.to_s == self.provider.to_s + end + + false + end + # Creates a {Vagrant::Environment} for this entry. # # @return [Vagrant::Environment] diff --git a/plugins/commands/global-status/command.rb b/plugins/commands/global-status/command.rb index 94dfd6c63..09c14ca60 100644 --- a/plugins/commands/global-status/command.rb +++ b/plugins/commands/global-status/command.rb @@ -42,7 +42,7 @@ module VagrantPlugins @env.machine_index.each do |entry| # If we're pruning and this entry is invalid, skip it # and prune it later. - if options[:prune] && invalid?(entry) + if options[:prune] && entry.invalid?(@env.home_path) prune << entry next end @@ -95,23 +95,6 @@ module VagrantPlugins # Success, exit status 0 0 end - - protected - - # Tests if a entry is invalid and should be pruned - def invalid?(entry) - return true if !entry.vagrantfile_path.directory? - - # Create an environment so we can determine the active - # machines... - env = entry.vagrant_env(@env.home_path) - env.active_machines.each do |name, provider| - return false if name.to_s == entry.name.to_s && - provider.to_s == entry.provider.to_s - end - - true - end end end end diff --git a/test/unit/vagrant/machine_index_test.rb b/test/unit/vagrant/machine_index_test.rb index 6a7efaa72..fe4a7cd00 100644 --- a/test/unit/vagrant/machine_index_test.rb +++ b/test/unit/vagrant/machine_index_test.rb @@ -265,3 +265,74 @@ describe Vagrant::MachineIndex do end end end + +describe Vagrant::MachineIndex::Entry do + include_context "unit" + + let(:env) { + iso_env = isolated_environment + iso_env.vagrantfile(vagrantfile) + iso_env.create_vagrant_env + } + + let(:vagrantfile) { "" } + + describe "#valid?" do + let(:machine) { env.machine(:default, :dummy) } + + subject do + described_class.new.tap do |e| + e.name = "default" + e.provider = "dummy" + e.vagrantfile_path = env.root_path + end + end + + it "should be valid with a valid entry" do + machine.id = "foo" + expect(subject).to be_valid(env.home_path) + end + + it "should be invalid if no Vagrantfile path is set" do + subject.vagrantfile_path = nil + expect(subject).to_not be_valid(env.home_path) + end + + it "should be invalid if the Vagrantfile path does not exist" do + subject.vagrantfile_path = Pathname.new("/i/should/not/exist") + expect(subject).to_not be_valid(env.home_path) + end + + it "should be invalid if the machine is inactive" do + machine.id = nil + expect(subject).to_not be_valid(env.home_path) + end + + context "with another active machine" do + let(:vagrantfile) do + <<-VF + Vagrant.configure("2") do |config| + config.vm.define "web" + config.vm.define "db" + end + VF + end + + it "should be invalid if the wrong machine is active only" do + m = env.machine(:web, :dummy) + m.id = "foo" + + subject.name = "db" + expect(subject).to_not be_valid(env.home_path) + end + + it "should be valid if the correct machine is active" do + env.machine(:web, :dummy).id = "foo" + env.machine(:db, :dummy).id = "foo" + + subject.name = "db" + expect(subject).to be_valid(env.home_path) + end + end + end +end