From 010874ffad425de11e2be8111f3fd68042dffb8d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 8 Jan 2014 10:50:28 -0800 Subject: [PATCH] core: has_plugin? uses the new plugin manager class to check for gems --- lib/vagrant.rb | 8 ++++--- lib/vagrant/plugin/v2/manager.rb | 12 ----------- test/unit/vagrant/plugin/v2/manager_test.rb | 12 ----------- test/unit/vagrant_test.rb | 24 ++++++++++++--------- 4 files changed, 19 insertions(+), 37 deletions(-) diff --git a/lib/vagrant.rb b/lib/vagrant.rb index b5b33752c..a2a6e7400 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -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. diff --git a/lib/vagrant/plugin/v2/manager.rb b/lib/vagrant/plugin/v2/manager.rb index 4d45c1035..17e4b7a8c 100644 --- a/lib/vagrant/plugin/v2/manager.rb +++ b/lib/vagrant/plugin/v2/manager.rb @@ -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 diff --git a/test/unit/vagrant/plugin/v2/manager_test.rb b/test/unit/vagrant/plugin/v2/manager_test.rb index 4e5c4fc35..4dca40cb1 100644 --- a/test/unit/vagrant/plugin/v2/manager_test.rb +++ b/test/unit/vagrant/plugin/v2/manager_test.rb @@ -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 diff --git a/test/unit/vagrant_test.rb b/test/unit/vagrant_test.rb index cb8c0f105..80fb0046f 100644 --- a/test/unit/vagrant_test.rb +++ b/test/unit/vagrant_test.rb @@ -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