core: Box#in_use? as an API
This commit is contained in:
parent
4fb9832589
commit
1a08c4def5
|
@ -73,21 +73,8 @@ module Vagrant
|
|||
|
||||
# Verify that this box is not in use by an active machine,
|
||||
# otherwise warn the user.
|
||||
users = []
|
||||
env[:machine_index].each do |entry|
|
||||
box_data = entry.extra_data["box"]
|
||||
next if !box_data
|
||||
|
||||
# If all the data matches AND the entry is a seemingly
|
||||
# valid entry, then track it.
|
||||
if box_data["name"] == box.name &&
|
||||
box_data["provider"] == box.provider.to_s &&
|
||||
box_data["version"] == box.version.to_s &&
|
||||
entry.valid?(env[:home_path])
|
||||
users << entry
|
||||
end
|
||||
end
|
||||
|
||||
users = box.in_use?(env[:machine_index]) || []
|
||||
users = users.find_all { |u| u.valid?(env[:home_path]) }
|
||||
if !users.empty?
|
||||
# Build up the output to show the user.
|
||||
users = users.map do |entry|
|
||||
|
|
|
@ -85,6 +85,33 @@ module Vagrant
|
|||
return true
|
||||
end
|
||||
|
||||
# Checks if this box is in use according to the given machine
|
||||
# index and returns the entries that appear to be using the box.
|
||||
#
|
||||
# The entries returned, if any, are not tested for validity
|
||||
# with {MachineIndex::Entry#valid?}, so the caller should do that
|
||||
# if the caller cares.
|
||||
#
|
||||
# @param [MachineIndex] index
|
||||
# @return [Array<MachineIndex::Entry>]
|
||||
def in_use?(index)
|
||||
results = []
|
||||
index.each do |entry|
|
||||
box_data = entry.extra_data["box"]
|
||||
next if !box_data
|
||||
|
||||
# If all the data matches, record it
|
||||
if box_data["name"] == self.name &&
|
||||
box_data["provider"] == self.provider.to_s &&
|
||||
box_data["version"] == self.version.to_s
|
||||
results << entry
|
||||
end
|
||||
end
|
||||
|
||||
return nil if results.empty?
|
||||
results
|
||||
end
|
||||
|
||||
# Loads the metadata URL and returns the latest metadata associated
|
||||
# with this box.
|
||||
#
|
||||
|
|
|
@ -195,6 +195,36 @@ describe Vagrant::Box do
|
|||
end
|
||||
end
|
||||
|
||||
context "#in_use?" do
|
||||
let(:index) { [] }
|
||||
|
||||
def new_entry(name, provider, version)
|
||||
Vagrant::MachineIndex::Entry.new.tap do |entry|
|
||||
entry.extra_data["box"] = {
|
||||
"name" => name,
|
||||
"provider" => provider,
|
||||
"version" => version,
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
it "returns nil if the index has no matching entries" do
|
||||
index << new_entry("foo", "bar", "1.0")
|
||||
index << new_entry("foo", "baz", "1.2")
|
||||
|
||||
expect(subject).to_not be_in_use(index)
|
||||
end
|
||||
|
||||
it "returns matching entries if they exist" do
|
||||
matching = new_entry(name, provider.to_s, version)
|
||||
index << new_entry("foo", "bar", "1.0")
|
||||
index << matching
|
||||
index << new_entry("foo", "baz", "1.2")
|
||||
|
||||
expect(subject.in_use?(index)).to eq([matching])
|
||||
end
|
||||
end
|
||||
|
||||
context "#load_metadata" do
|
||||
let(:metadata_url) do
|
||||
Tempfile.new("vagrant").tap do |f|
|
||||
|
|
Loading…
Reference in New Issue