core: has_plugin? uses the new plugin manager class to check for gems

This commit is contained in:
Mitchell Hashimoto 2014-01-08 10:50:28 -08:00
parent dbaa48ff5b
commit 010874ffad
4 changed files with 19 additions and 37 deletions

View File

@ -147,10 +147,12 @@ module Vagrant
# be used from the Vagrantfile to easily branch based on plugin
# availability.
def self.has_plugin?(name)
manager = plugin("2").manager
# We check the plugin names first because those are cheaper to check
return true if plugin("2").manager.registered.any? { |p| p.name == name }
manager.required.any? { |gem_name| gem_name == name } ||
manager.registered.any? { |plugin| plugin.name == name }
# Now check the plugin gem names
require "vagrant/plugin/manager"
Plugin::Manager.instance.installed_specs.any? { |s| s.name == name }
end
# Returns a superclass to use when creating a plugin for Vagrant.

View File

@ -8,12 +8,10 @@ module Vagrant
# those plugins as a single unit.
class Manager
attr_reader :registered
attr_reader :required
def initialize
@logger = Log4r::Logger.new("vagrant::plugin::v2::manager")
@registered = []
@required = []
end
# This returns all the action hooks.
@ -182,20 +180,10 @@ module Vagrant
end
end
# This registers a required plugin. This should _NEVER_ be called by
# the public and should only be called from within Vagrant.
def plugin_required(gem_name)
if !@required.include?(gem_name)
@logger.info("Registered required plugin: #{gem_name}")
@required << gem_name
end
end
# This clears out all the registered plugins. This is only used by
# unit tests and should not be called directly.
def reset!
@registered.clear
@required.clear
end
# This unregisters a plugin so that its components will no longer

View File

@ -205,16 +205,4 @@ describe Vagrant::Plugin::V2::Manager do
instance.synced_folders[:foo].should == ["bar", 10]
instance.synced_folders[:bar].should == ["baz", 50]
end
it "should list the required plugins" do
instance.plugin_required("foo")
instance.plugin_required("bar")
expect(instance.required).to eq(["foo", "bar"])
end
it "should list the required plugins only once" do
instance.plugin_required("foo")
instance.plugin_required("foo")
expect(instance.required).to eq(["foo"])
end
end

View File

@ -48,28 +48,32 @@ describe Vagrant do
end
describe "has_plugin?" do
before(:each) do
Class.new(described_class.plugin("2")) do
name "i_am_installed"
end
manager.plugin_required("plugin_gem")
end
after(:each) do
manager.reset!
end
let(:manager) { described_class.plugin("2").manager }
it "should find the installed plugin by the gem name" do
expect(described_class.has_plugin?("plugin_gem")).to be_true
end
it "should find the installed plugin by the registered name" do
Class.new(described_class.plugin(Vagrant::Config::CURRENT_VERSION)) do
name "i_am_installed"
end
expect(described_class.has_plugin?("i_am_installed")).to be_true
end
it "should return false if the plugin is not installed" do
expect(described_class.has_plugin?("i_dont_exist")).to be_false
end
it "finds plugins by gem name" do
specs = [Gem::Specification.new]
specs[0].name = "foo"
Vagrant::Plugin::Manager.instance.stub(:installed_specs => specs)
expect(described_class.has_plugin?("foo")).to be_true
expect(described_class.has_plugin?("bar")).to be_false
end
end
describe "require_version" do