diff --git a/lib/vagrant/action/builtin/box_remove.rb b/lib/vagrant/action/builtin/box_remove.rb index 8ee9cd7f6..26924612b 100644 --- a/lib/vagrant/action/builtin/box_remove.rb +++ b/lib/vagrant/action/builtin/box_remove.rb @@ -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| diff --git a/lib/vagrant/box.rb b/lib/vagrant/box.rb index 2dca80953..7daef43a0 100644 --- a/lib/vagrant/box.rb +++ b/lib/vagrant/box.rb @@ -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] + 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. # diff --git a/test/unit/vagrant/box_test.rb b/test/unit/vagrant/box_test.rb index 4d40bb9ce..07af56acb 100644 --- a/test/unit/vagrant/box_test.rb +++ b/test/unit/vagrant/box_test.rb @@ -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|