core: MachineIndex::Entry#valid? method and tests
This commit is contained in:
parent
2660252ede
commit
e985308e9a
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue